Skip to main content
v2026.11,986 entries · CC-BY 4.0

patchwork in R: Combine ggplot2 Plots into One Figure

How to use the patchwork R package to combine ggplot2 plots into one figure: install, operators, layouts, panel tags, shared legends.

Written and maintained by CASRAI Editorial Board

Published

On this page

patchwork is an R package that combines separate ggplot2 plots into one graphic using arithmetic-style operators. Its documentation describes the aim as making it "ridiculously simple to combine separate ggplots into the same graphic". This page is for researchers and analysts who need a multi-panel figure (for example a Figure 1 with panels A to D) and want the commands, the layout controls and the common stumbling blocks in one place. Version 1.3.2 was the current CRAN release when this page was checked on 2026-11-08.

What patchwork is used for in research

patchwork assembles several ggplot2 objects into a single figure while keeping the panels aligned. The package site says it offers "rich support for arbitrarily complex layouts with full alignment". Typical research uses are a multi-panel manuscript figure, a shared legend across panels, and automatic panel labels such as A, B, C. It works on ggplot2 plots only; if you are new to the plotting layer itself, see ggplot themes, ggplot titles and the introduction to using RStudio.

Install or access

The official CRAN listing (checked 2026-11-08) gives version 1.3.2, published 2025-08-25, under an MIT + file LICENSE. It imports ggplot2 (3.0.0 or later), gtable (0.3.6 or later), rlang (1.0.0 or later), cli and farver.

install.packages("patchwork")
library(patchwork)

The development version is installed from GitHub with devtools::install_github("thomasp85/patchwork"), as stated on the package site.

Step-by-step: build a multi-panel figure

  1. Create the individual plots and store each in a variable (the example uses R’s built-in mtcars data).
  2. Combine them with operators. p1 + p2 combines plots, | places plots side by side, / stacks them, and parentheses nest groups.
  3. Control the layout with plot_layout().
  4. Label and title the whole figure with plot_annotation().
library(ggplot2)
library(patchwork)

p1 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point()
p2 <- ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot()
p3 <- ggplot(mtcars, aes(mpg)) + geom_histogram(binwidth = 2)

# two plots side by side, one underneath
(p1 | p2) / p3

Layout with plot_layout()

According to the layout guide, plot_layout() accepts ncol, widths, heights (relative numbers or a vector of units), design, guides, and the axes and axis_titles options (which can be set to 'collect').

p1 + p2 + p3 + plot_layout(ncol = 3)
p1 + p2 + plot_layout(widths = c(2, 1))
p1 + plot_spacer() + p2

plot_spacer() inserts an empty cell. For irregular grids, design takes a text string in which each letter is a plot (assigned in the order the plots were added) and # marks an empty cell; the guide’s example is:

layout <- "
##BBBB
AACCDD
##CCDD
"
p1 + p2 + p3 + p4 + plot_layout(design = layout)

Alternatively, area(t, l, b, r) defines each plot’s rectangle by top, left, bottom and right position, and wrap_plots() takes plots plus a design.

Panel tags, titles and a shared legend

(p1 | p2) / p3 +
  plot_layout(guides = 'collect') +
  plot_annotation(
    title = 'Fuel economy and weight',
    tag_levels = 'A'
  )

guides = 'collect' gathers legends into one (the default is 'auto'; 'keep' leaves them with each plot). plot_annotation() takes title, subtitle, caption and tag_levels, where tag_levels accepts '1', 'A', 'a', 'I' or 'i', or a vector such as c('A', '1') for sub-tagging. tag_prefix, tag_sep and tag_suffix adjust the label text.

Output and how to read it

The result of combining plots is a single patchwork object that draws as one graphic in the active graphics device. Each panel keeps its own axes, and patchwork aligns the panels across the figure. With tag_levels = 'A', panels are labelled in the order they were added, so the order of +, | and / in your expression determines which panel is A, B and C. Check that your manuscript text refers to panels in that order.

Troubleshooting

  • Title or tags appear on the wrong level. The annotation guide states that annotations apply only to the top-level patchwork. A nested group needs wrap_elements(), which, per the same guide, breaks alignment for that group.
  • Theme change affects only some elements. Using & applies a modification to all subplots, for example patchwork & theme(text = element_text('mono')), while plot_annotation(theme = ...) styles the patchwork itself.
  • Several repeated legends. Use plot_layout(guides = 'collect').
  • Install fails on dependencies. The CRAN page lists the minimum versions of ggplot2, gtable and rlang; update those packages first.
  • Grid shape. Set ncol in plot_layout(), or use design for exact placement.

Free alternatives

The CRAN description of patchwork names two other approaches for arranging ggplots. This table lists only what that description states.

Package Relationship to patchwork Source
patchwork Combines ggplots with operators such as +, | and / CRAN and package site
gridExtra Named on the patchwork CRAN page as another approach CRAN, patchwork description
cowplot Named on the patchwork CRAN page as another approach CRAN, patchwork description

FAQ

What does patchwork do in R?

It combines multiple ggplot2 plots into one graphic, using operators to place and nest them and keeping the panels aligned.

How do I put two ggplots side by side?

Use p1 | p2. To stack them use p1 / p2; to do both, (p1 | p2) / p3.

How do I label panels A, B, C?

Add plot_annotation(tag_levels = 'A') to the top-level patchwork.

How do I share one legend?

Add plot_layout(guides = 'collect').

Is patchwork free?

It is released under an MIT + file LICENSE according to its CRAN page.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about patchwork in R: Combine ggplot2 Plots into One Figure

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →