Data Into Insights (Spring 2021)/R4DS Chapter 3 Exercises

From CommunityData

There are a lot of exercises in R4DS. For Chapter 3, these are the ones that we will go over in class.

You should create a new R Markdown file (File > New File > R Markdown) for this and future R assignments.

This will give you a new example R Markdown file. You can delete everything after

   ```{r setup, include=FALSE}
   knitr::opts_chunk$set(echo = TRUE)
   ```

and replace it with your own answers.

As a reminder, you can just write text in the main part of the document. To create code chunks you push Insert > R or type Ctrl + Alt + I (Cmd + Option + I on Mac).



3.2

2. How many rows are in mpg? How many columns?

3. What does the drv variable describe? Read the help for ?mpg to find out.

4. Make a scatterplot of hwy vs cyl.

5. What happens if you make a scatterplot of class vs drv? Why is the plot not useful?


3.3

1. What’s gone wrong with this code? Why are the points not blue?

2. Which variables in mpg are categorical? Which variables are continuous? (Hint: type ?mpg to read the documentation for the dataset). How can you see this information when you run mpg?

3. Map a continuous variable to color, size, and shape. How do these aesthetics behave differently for categorical vs. continuous variables?

4. What happens if you map the same variable to multiple aesthetics?

5. What does the stroke aesthetic do? What shapes does it work with? (Hint: use ?geom_point)

6. What happens if you map an aesthetic to something other than a variable name, like aes(colour = displ < 5)? Note, you’ll also need to specify x and y.


3.5

2. What do the empty cells in plot with facet_grid(drv ~ cyl) mean? How do they relate to this plot?

3. What plots does the following code make? What does . do?

4. What are the advantages to using faceting instead of the colour aesthetic? What are the disadvantages? How might the balance change if you had a larger dataset?


3.6

1. What geom would you use to draw a line chart? A boxplot? A histogram? An area chart?

4. What does the se argument to geom_smooth() do?

6. Recreate the R code necessary to generate the following graphs.


I am leaving out 3.7+ for this week. I still suggest that you read these sections, but focus for now on really understanding the basics of creating plots and use these sections for future reference.