Blog Home

R for Data Science 10.3.1 Exercises

From R for Data Science

Exercises 10.3.1

10.3.1 Exercises

1.What geom would you use to draw a line chart? A boxplot? A histogram? An area chart?
  • Line chart would be geom_smooth()
  • Box plot is geom_boxplot()
  • Histogram is geom_histogram()
  • Area chart geom_area()
2.Earlier in this chapter we used show.legend without explaining it:
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_smooth(aes(color = drv), show.legend = FALSE)
What does show.legend = FALSE do here? What happens if you remove it? Why do you think we used it earlier?

It removes the legend. I think it was used earlier to conserve space for the chart

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

It sets the confidence interval around smooth, and is TRUE by default

4.Recreate the R code necessary to generate the following graphs. Note that wherever a categorical variable is used in the plot, it’s drv.
ggplot(mpg, aes(x = displ, y= hwy)) + 
  geom_point(size = 3) + 
  geom_smooth()

ggplot(mpg, aes(x = displ, y= hwy)) + 
  geom_point(size = 3) + 
  geom_smooth(aes(group = drv))

ggplot(mpg, aes(x = displ, y= hwy)) + 
  geom_point(size = 3, aes(color=drv)) + 
  geom_smooth(aes(group = drv, color = drv))

ggplot(mpg, aes(x = displ, y= hwy)) + 
  geom_point(size = 3, aes(color=drv)) + 
  geom_smooth()

ggplot(mpg, aes(x = displ, y= hwy)) + 
  geom_point(size = 3, aes(color=drv)) + 
  geom_smooth(aes(linetype = drv))

ggplot(mpg, aes(x = displ, y= hwy)) + 
  geom_point(size = 7, color="white") +
  geom_point(size = 3, aes(color=drv))