Chapter 29 Multiple plots on a page

29.1 Faceting is not a panacea

Faceting is useful for constructing an array of similar plots where each panel corresponds to a level of a factor or a combination of levels from multiple factors. Pragmatic reshaping can make lots of graphing problems have this “look”. But this does not cover the entire spectrum of multi-plot needs in the real world. Sometimes you need to get more than one plot on a virtual page and the plots are rather disconnected. How do you do that?

29.2 Meet the gridExtra package

Under the hood, ggplot2 uses the grid package to create figures. The gridExtra packages provides some extra goodies and we will draw on them to place multiple ggplot2 plots on a single virtual page.

You may need to install gridExtra and you will certainly need to load it.

29.3 Load gapminder and ggplot2

29.5 Use the multiplot() function

In the Graphs chapter of his Cookbook for R, Winston Chang uses the grid package to define the multiplot() function:

Assuming that the plot objects p1, p2, p3, and p4 are pre-defined, call the function like this:

Visit Multiple graphs on one page (ggplot2) to see a complete worked example.

29.6 Use the cowplot package

The cowplot package (CRAN; GitHub) does (at least) two things:

  • Provides a publication-ready theme for ggplot2.
  • Helps combine multiple plots into one figure.

Check out the vignette to see it in action.