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
ggplot2how to draw the mapped data, which is exactly what’s missing. - Wrong: an aesthetic mapping only tells
ggplot2which 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?
- 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.
- Wrong: adding a layer with
+never removes an earlier layer; layers stack rather than replace one another. - Wrong: geoms inherit the
x/ymapping fromggplot()unless they specify their own, sogeom_smooth()works here without an extraaes()call. - Wrong:
ggplot2plots can combine as many geometry layers as needed in a single plot.
Which statements correctly describe labs() and theme_*() functions in ggplot2? (Select all that apply.)
- TRUE:
labs()accepts arguments matching any aesthetic or title role you want to relabel. - 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: this is exactly what a theme controls: appearance, not the underlying mappings or geometry.
- FALSE: themes never change what data is mapped to which aesthetic; that is controlled entirely by
aes().