Combine ggplots with patchwork

data science visualization ggplot2 R

How to combine arbitrary ggplots

Matti Vuorre https://vuorre.netlify.com (University of Oxford)https://www.oii.ox.ac.uk/people/matti-vuorre/
2018-12-13

ggplot2 is the best R package for data visualization, and has powerful features for “facetting” plots into small multiples based on categorical variables.

Facetting figures into small multiples

This “facetting” is useful for showing the same figure, e.g. a bivariate relationship, at multiple levels of some other variable

library(tidyverse)
ggplot(mtcars, aes(mpg, disp)) +
  geom_point() +
  facet_wrap("cyl")

But if you would like to get a figure that consists of multiple panels of unrelated plots—with different variables on the X and Y axes, potentially from different data sources—things become more complicated.

Combining arbitrary ggplots

Say you have these three figures

p <- ggplot(mtcars)
  
a <- p +
  aes(mpg, disp, col = as.factor(vs)) +
  geom_smooth(se = F) +
  geom_point()

b <- p + 
  aes(disp, gear, group = gear) +
  ggstance::geom_boxploth()

c <- p +
  aes(hp) +
  stat_density(geom = "area") +
  coord_cartesian(expand = 0)

How would you go about combining them? There are a few options, such as grid.arrange() in the gridExtra package, and plot_grid() in the cowplot package. Today, I’ll point out a newer package that introduces a whole new syntax for combining together, patchwork.

Patchwork

patchwork is not yet on CRAN, so install it from GitHub:

# install.packages("devtools")
devtools::install_github("thomasp85/patchwork")

Once you load the package, you can add ggplots together by adding them with +:

library(patchwork)
a + b + c

Basically, you can add ggplots together as if they were geoms inside a single ggplot. However, there’s more. | specifies side-by-side addition

a | c

And / is for adding plots under the previous plot

b / c

These operators can be used to flexibly compose figures from multiple components, using parentheses to group plots and +, |, and / to add the groups together

(a | b) / c

Use plot_annotation() to add tags, and & to pass theme elements to all plot elements in a composition

(a | b) / c + 
  plot_annotation(tag_levels = "A") & 
  theme(legend.position = "none")
Tweak this a little bit and throw it in a manuscript.

Figure 1: Tweak this a little bit and throw it in a manuscript.

There are many more examples on patchwork’s GitHub page. I’ve found this package more useful in composing figures out of multiple plots than its alternatives, mainly because of the concise but powerful syntax.

Support this work

Software used

The following software packages were used: R [Version 4.0.3; R Core Team (2020)] and the R-packages dplyr [Version 1.0.5; Wickham et al. (2021)], forcats [Version 0.5.1; Wickham (2021a)], ggplot2 [Version 3.3.3; Wickham (2016)], knitr [Version 1.31; Xie (2015)], patchwork [Version 1.1.1; Pedersen (2020)], purrr [Version 0.3.4; Henry and Wickham (2020)], readr [Version 1.4.0; Wickham and Hester (2020)], stringr [Version 1.4.0; Wickham (2019)], tibble [Version 3.1.0; Müller and Wickham (2021)], tidyr [Version 1.1.3; Wickham (2021b)], and tidyverse [Version 1.3.0; Wickham et al. (2019)].

Henry, Lionel, and Hadley Wickham. 2020. Purrr: Functional Programming Tools. https://CRAN.R-project.org/package=purrr.
Müller, Kirill, and Hadley Wickham. 2021. Tibble: Simple Data Frames. https://CRAN.R-project.org/package=tibble.
Pedersen, Thomas Lin. 2020. Patchwork: The Composer of Plots. https://CRAN.R-project.org/package=patchwork.
R Core Team. 2020. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.
———. 2019. Stringr: Simple, Consistent Wrappers for Common String Operations. https://CRAN.R-project.org/package=stringr.
———. 2021a. Forcats: Tools for Working with Categorical Variables (Factors). https://CRAN.R-project.org/package=forcats.
———. 2021b. Tidyr: Tidy Messy Data. https://CRAN.R-project.org/package=tidyr.
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686.
Wickham, Hadley, Romain François, Lionel Henry, and Kirill Müller. 2021. Dplyr: A Grammar of Data Manipulation. https://CRAN.R-project.org/package=dplyr.
Wickham, Hadley, and Jim Hester. 2020. Readr: Read Rectangular Text Data. https://CRAN.R-project.org/package=readr.
Xie, Yihui. 2015. Dynamic Documents with R and Knitr. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC. https://yihui.org/knitr/.

References

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. Source code is available at https://github.com/mvuorre/mvuorre.github.io, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

Citation

For attribution, please cite this work as

Vuorre (2018, Dec. 13). Sometimes I R: Combine ggplots with patchwork. Retrieved from https://mvuorre.github.io/posts/2018-12-13-rpihkal-combine-ggplots-with-patchwork/

BibTeX citation

@misc{vuorre2018combine,
  author = {Vuorre, Matti},
  title = {Sometimes I R: Combine ggplots with patchwork},
  url = {https://mvuorre.github.io/posts/2018-12-13-rpihkal-combine-ggplots-with-patchwork/},
  year = {2018}
}