Laying out multiple plots on a page

Baptiste Auguié

2019-07-13

An individual ggplot object contains multiple pieces – axes, plot panel(s), titles, legends –, and their layout is defined and enforced via the gtable package, itself built around the lower-level grid package. Plots themselves become graphical objects, which can be arranged on a page using e.g. the gridExtra or egg packages, which provide helper functions for such multi-object layouts. The following schematic illustrates the main relations between these packages.

Schematic illustration of the links between packages ggplot2, gtable, grid, egg and gridExtra.

Schematic illustration of the links between packages ggplot2, gtable, grid, egg and gridExtra.

Arranging multiple plots on a page

To begin, we’ll create four example plots that we can experiment with.

library(ggplot2)
p1 <- qplot(mpg, wt, data = mtcars, colour = cyl)
p2 <- qplot(mpg, data = mtcars) + ggtitle("title")
p3 <- qplot(mpg, data = mtcars, geom = "dotplot")
p4 <-
  p1 + facet_wrap( ~ carb, nrow = 1) + theme(legend.position = "none") +
  ggtitle("facetted plot")