What is the key difference between facet_wrap(~x) and facet_grid(x ~ y)?
- Wrong: neither faceting function changes which data is plotted; both only change how it’s split across panels.
- Wrong:
facet_grid()works with any geometry, not justgeom_bar(). - Wrong: the two produce different panel layouts (and
facet_grid()reserves empty panels), so they are not interchangeable. - Correct: this is exactly the distinction demonstrated with the
mpgdrv/cyl/classexamples.
Which of the following statements about saving ggplot2 plots are TRUE? (Select all that apply.)
- FALSE:
ggsave()supports PDF, JPEG, PNG, and several other formats via the same file-extension mechanism. - TRUE:
ggsave()infers the output format from the file extension you provide. - TRUE: forgetting
dev.off()leaves the graphics device open and the file incomplete. - TRUE: this is the main convenience
ggsave()adds over the base R device functions.
You run p1 <- ggplot(mpg) + geom_bar(aes(x = class)) in an R script (not interactively at the console) and no plot appears. What is the best explanation?
- Wrong:
grid.arrange()is only one way to display saved plots; printing a single saved plot works the same way as any other R object. - Wrong: the code is valid; nothing has failed.
- Wrong: any
ggplot2object, including one built withgeom_bar(), can be assigned to a variable. - Correct: assignment (
<-) stores the object silently, exactly like any other R object; a top-level expression (or an explicitprint()) is needed to actually display it.