Knowledge Quiz: Tutorial 3

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

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 just geom_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 mpg drv/cyl/class examples.

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 ggplot2 object, including one built with geom_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 explicit print()) is needed to actually display it.
Back to top