Combine ggplots with patchwork

data science visualization ggplot2 R

How to combine arbitrary ggplots

Matti Vuorre (University of Oxford)

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

ggplot(mtcars, aes(mpg, disp)) +
  geom_point() +

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) +

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

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 is not yet on CRAN, so install it from GitHub:

# install.packages("devtools")

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

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.
Müller, Kirill, and Hadley Wickham. 2021. Tibble: Simple Data Frames.
Pedersen, Thomas Lin. 2020. Patchwork: The Composer of Plots.
R Core Team. 2020. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York.
———. 2019. Stringr: Simple, Consistent Wrappers for Common String Operations.
———. 2021a. Forcats: Tools for Working with Categorical Variables (Factors).
———. 2021b. Tidyr: Tidy Messy Data.
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.
Wickham, Hadley, Romain François, Lionel Henry, and Kirill Müller. 2021. Dplyr: A Grammar of Data Manipulation.
Wickham, Hadley, and Jim Hester. 2020. Readr: Read Rectangular Text Data.
Xie, Yihui. 2015. Dynamic Documents with R and Knitr. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC.



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


Text and figures are licensed under Creative Commons Attribution CC BY 4.0. Source code is available at, 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 ...".


For attribution, please cite this work as

Vuorre (2018, Dec. 13). Sometimes I R: Combine ggplots with patchwork. Retrieved from

BibTeX citation

  author = {Vuorre, Matti},
  title = {Sometimes I R: Combine ggplots with patchwork},
  url = {},
  year = {2018}