3.10 Exercise solutions

3.10.1 Exercise 1

  • What geoms would you use to draw each of the following named plots?
    • scatterplot = geom_point()
    • line chart = geom_line()
    • histogram = geom_histogram()
    • bar chart = geom_bar() or geom_col()
    • pie chart = geom_bar() with coord_polar()
ggplot(data_diamond_count, aes(cut, count)) +
  geom_col()

ggplot(diamonds, aes(x = factor(1), fill = factor(cut))) +
  geom_bar(width = 1) +
  coord_polar(theta = "y")

3.10.2 Exercise 2

  • geom_path() connects points in order of appearance. geom_line connects points from left to right.
p + geom_path()

  • geom_polygon() draws polygons which are filled paths.
p + geom_polygon() 

  • geom_line() connects points from left to right.
p + geom_line() 

3.10.3 Exercise 3

  • What low-level geoms are used to draw geom_smooth()?
    • geom_smooth() fits a smoother to data, displaying the smooth and its standard error, allowing you to see a dominant pattern within a scatterplot with a lot of “noise”. The low level geom for geom_smooth() are geom_path(), geom_area() and geom_point().
ggplot(mpg, aes(displ, hwy)) + 
  geom_point() + 
  geom_smooth()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

  • What low-level geoms are used to draw geom_boxplot()?
    • Box plots are used to summarize the distribution of a set of points using summary statistics. The low level geom for geom_boxplot() are geom_rect(), geom_line() and geom_point().
ggplot(mpg, aes(drv, hwy)) + 
  geom_boxplot()

  • What low-level geoms are used to draw geom_violin()?
    • Violin plots show a compact representation of the density of the distribution highlighting the areas where most of the points are found. The low level geom for geom_violin() are geom_area() and geom_path().
ggplot(mpg, aes(drv, hwy)) + 
  geom_violin()