Chapter 47 Multiple Plots in R

When generating reports for a data science project, there is often more than one plot that you want to display at a time. To accomplish this in R, we will use the R package patchwork from Thomas Lin Pedersen, which simplifies this process.

47.0.1 Installing patchwork

As with other R packages you’ve used, you will first need to install this package. As this package is relatively new, however, it is not on CRAN so install.packages('patchwork') will not work. Instead, this package has to be installed using devtools. If you don’t have devtools installed yet, do that first. If you already have that library installed, you can simply load devtools and then install the patchwork package from GitHub.

# install.packages("devtools")

library(devtools)
devtools::install_github("thomasp85/patchwork")

47.0.2 Basic plotting using patchwork

Patchwork allows any plots generated from ggplot2 to be combined simply. For example, if you were interested in plotting data from the mtcars dataset in R but wanted to combine two different plots, you could use patchwork. Below, you’ll see code for combining two plots. The first plot examines the relationship between how many miles per gallon (mpg) a car a gets and the weight of that car in 1000lb (wt). This is assigned to object p1. The second plot looks at the relationship between how many mpgs a car gets and the number of forward gears that car has (p2). While you could plot each individually, you may want to see them side by side. With patchwork, you can do this my simply using a plus sign (p1+p2). Using this very simple code, you’ll obtain side by side plots, which is exactly what you were looking to achieve!

library(ggplot2)
library(patchwork)

p1 <- ggplot(mtcars) + geom_point(aes(wt, mpg))
p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, mpg, group = gear))

p1 + p2

Two plots side-by-side

47.0.3 Altering the layout

If you don’t want these plots side-by-side, but rather one on top of the other, this can be controlled with plot_layout(), where you can define the number of columns (ncol) or number of rows (nrow) you would like to display.

p1 + p2 + plot_layout(ncol = 1)

two plots one on top of the other

Similarly, if you want these on top of one another but would rather the first plot be larger, this can be controlled within plot_layout() using the heights argument. The ‘3’ in the code below specifies that you would like the top plot to be 3 times larger than the bottom plot (whose relative height is defined as ‘1’).

p1 + p2 + plot_layout(ncol = 1, heights = c(3, 1))

two plots one on top of the other of unequal heights

47.0.4 Nesting plots

Finally, sometimes you want a more complicated layout. Patchwork can handle this too. To nest a plot within another plot, the formatting is slightly more complicated, you can use parentheses ‘()’ or brackets ‘{}’ around the plots you want to nest.

For example, in the code below, plot p4 is plotted first and the whole plot is defined to have one column. Then, plot p1 is added. Finally,p2 and p3 are combined into a single column plot before this combined plot (p2 + p3) is nested into the overall plot. The brackets {} are placed carefully around the plots to determine the achieve the desired plotting format.

p3 <- ggplot(mtcars) + geom_smooth(aes(disp, qsec))
p4 <- ggplot(mtcars) + geom_bar(aes(carb))

p4 + (
  p1 + (
    p2 +
    p3 +
    plot_layout(ncol = 1)
  )
) +
  plot_layout(ncol = 1)

Nested Plots

47.0.5 Additional operators

So far we have only added plots to one another using ‘+’. While this operator can accomplish most multi-plotting goals, we will briefly highlight a few additional operators within patchwork.

47.0.5.1 | and /

If you are simply adding plots beside one another or on top of one another, this can be accomplished using | and /. | tells patchwork to put the plots next to one another from left to right. / tells patchwork you want those plots on top of each other. For example, below we see that p1, p2, and p3 should all be next to one another. These are grouped together because of the parentheses. Then these three plots should all be atop p4.

(p1 | p2 | p3) /
      p4

Three plots together horizontally over another plot vertically

47.0.5.2 & and *

ggpplot2 allows for flexibility in theme in many different ways, as demonstrated in a previous lesson. To apply the same theme to all plots in the multi-plot image, you will want to use &. Below you’ll see that with the use of &, theme_bw has been applied to all the plots.

p1 + (p2 + p3) + p4 + plot_layout(ncol = 1) & theme_bw()

All four plots changed to theme_bw()

If you only wanted this theme to be applied to the plots in the current nesting level, you would use *.

(p1 + (p2 + p3) + p4 + plot_layout(ncol = 1)) * theme_bw()

Plots in main nesting level have theme_bw, but p2 and p3 remain unchanged

47.0.5.3 -

Thus far, we have only focused on adding plots together using +. There is also an - operator. Think of this as a hyphen, rather than as a subtraction sign, because we won’t be removing any plots. This operator puts what is on the left and right side of the - on the same nesting level. In the code below, the overall plot layout defines that there should be one column. That means that p2 and p3 are on the same nesting level (because of the -). This forces p1 and p2 to be side by side. This operator is slightly less intuitive, and thus is included here for completeness but included at the end because you can likely compose all the multiplot images you need without fully understanding the - operator.

p1 + p2 - p3 + plot_layout(ncol = 1)

Using the - operator

47.0.6 Additional Resources

Patchwork GitHub