Knowledge Quiz

Note
  • Click the check-mark button to check your answer.
  • Click the question-mark button to see an explanation of the solution.

You run ggplot(data = mpg) on its own and see a blank grey rectangle with no points, bars, or lines on it. Which of the following, if added, would fix this?

  • Wrong: theme_bw() only changes background styling; it doesn’t add any visual marks.
  • Wrong: labs() only changes text labels; it doesn’t add any visual marks.
  • Correct: a geometry layer tells ggplot2 how to draw the mapped data, which is exactly what’s missing.
  • Wrong: an aesthetic mapping only tells ggplot2 which variables to use and how; without a geom, there is still nothing telling it how to draw them. Meta-information

You run the following code:

ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point() +
geom_smooth()

What best describes the resulting plot?

  • Wrong: ggplot2 plots can combine as many geometry layers as needed in a single plot.
  • Wrong: geoms inherit the x/y mapping from ggplot() unless they specify their own, so geom_smooth() works here without an extra aes() call.
  • Wrong: adding a layer with + never removes an earlier layer; layers stack rather than replace one another.
  • Correct: each geom layer adds its own visual marks on top of the existing plot, so both the points and the smoothed line appear together.

Which statements correctly describe labs() and theme_*() functions in ggplot2? (Select all that apply.)

  • FALSE: themes never change what data is mapped to which aesthetic; that is controlled entirely by aes().
  • TRUE: this is exactly what a theme controls: appearance, not the underlying mappings or geometry.
  • FALSE: + layers are additive; a saved plot object already contains its data, mapping, and geometry, and a theme layer is simply appended on top.
  • TRUE: labs() accepts arguments matching any aesthetic or title role you want to relabel.
Back to top