• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / R / ggplot2 / combine plots patchwork / 7 Tips to Combine Multiple ggplots Using Patchwork

7 Tips to Combine Multiple ggplots Using Patchwork

January 7, 2020 by cmdlinetips

Patchwork to combine plotsPatchwork, the R package that lets you combine multiple figures made by ggplot2, got a big update late last year and it is on CRAN now.

If you have not heard of Patchwork, it is an R package made by the awesome Thomas Lin Pedersen. Patchwork lets you combine separate plots made by ggplot to make a single figure that is publication quality. Patchwork is like two other options out there gridExtra and cowplot.

Finally got a chance to try out patchwork for combining plots under most common scenario. And here are the tips to get started with patchwork.

Let us first install the latest version of patchwork (https://github.com/thomasp85/patchwork). One can install patchwork directly from CRAN using

install.packages('patchwork')

Let us load all the R packages needed to get started. We will use gapminder dataset for making plots and combining them with patwork.

# load tidyverse
library(tidyverse)
# load gapminder
library(gapminder)
# load patchwork
library(patchwork)
# set ggplot theme
theme_set(theme_bw())

Let us make two plots: a scatter plot and a boxplot using gapminder data with ggplot2. We save each plot to a variable and will reuse the variables throughout the post.

p1 <- gapminder %>% 
  filter(year==1952) %>%
  ggplot(aes(x=gdpPercap, y=lifeExp))+
  geom_point() + scale_x_log10() +
  labs(subtitle="1952")
p2 <- gapminder %>% 
  filter(year==1952) %>%
  ggplot(aes(x=continent, y=lifeExp, group=continent))+
  geom_boxplot() +
  geom_jitter(width=0.2, alpha=0.4)+
  labs(subtitle="1952")

How to Combine Two Plots?

Combining two plots is simple, use plus sign to combine two ggplot2 objects.

p1 + p2
Combine two plots made with ggplot using Patchwork
How To Combine two ggplots using Patchwork

How to Combine Two Plots Side by Side?

Sometimes you may want to combine two plots in a specific way. For example, to combine to plots so that both the plots are side by side, you use “|” the pipe symbol instead of “plus” symbol.

# two plots side by side
p1|p2

You get a combined plot where two plots are side by side.

Combine Two Plots Side By Side
Combine Two Plots Side By Side: Patchwork

How to Combine Two Plots one over the other?

If you want to combine two plots such that one is on top of the other, i.e. vertically, use “/” between the two ggplot2 objects.

# two plots one over the other
p1/p2

The first plot object will be on top of the second object.

Combine Two Plots Vertically
Combine Two Plots One on Top of Another: Patchwork

How to Combine Multiple Plots in a Grid?

What if you want to combine more than two plots. We can combine multiple plots into a single plot in a grid using the combination of the “+” and “/”symbols.

For example to have two plots in the first row and third plot in the second row you would use

p3 <- gapminder %>% 
  ggplot(aes(x=lifeExp, fill=continent))+
  geom_density(alpha=0.4) 
(p1 + p2)/p3

Now you have a grid with three plots combined in a single patched plot.

Combine Multiple Plots in a Grid with Patchwork
Combine Multiple Plots in a Grid with Patchwork

How to Place Legends in a Common Place?

When combining multiple plots together, sometimes you might want to put legends in a common place instead of right next to each plot.

We can place all legends in a common place using plot_layout() function as shown below. Here, we have combined two plots side by side and placed the legends in a common place.

p1 + p2 + plot_layout(guides="collect")
ggsave("combine_two_plots_legends_in_common_place_patchwork.jpg")
Place Legends in a Common Plac
Place Legends in a Common Plac

6. How to Tag each plot in a combined plot with patchwork?

When we combine multiple plots adding a name or tag to each plot is extremely useful to refer a specific plot from the combined plot in a document. Patchwork’s plot_annotation() function can add tags automatically by specifying what type of tags we want to add.

For example to add upper case letters, we use plot_annotation(tag_levels = ‘A’) and ‘1’ for Arabic numerals, ‘a’ for lowercase Latin letters, ‘I’ for uppercase Roman numerals, and ‘i’ for lowercase Roman numerals.

Let us add tags with upper case letters for the patched plot.

patched <- (p1 + p2)/p3
patched + plot_annotation(tag_levels = 'A')

Here we have combined three plots and added A, B, and C as tags to each plot.

Add Tags to combined plot: Patchwork
Add Tags to combined plot: Patchwork

We can also adjust the font size of the tags easily by using element_text() function within theme as shown below.

patched +
 plot_annotation(tag_levels = 'A') & 
  theme(plot.tag = element_text(size = 8))
Change Tag Size: Patchwork
Change Tag Size: Patchwork

7. How to Combine a ggplot and table?

We can also combine a plot easily with a table. Here we create a table with dplyr functions and use gridExtra to create a table and then combine it with a plot.

p1 + gridExtra::tableGrob(gapminder %>% 
                            group_by(continent) %>% 
                            summarize(lifeExp_Ave=mean(lifeExp)))

Now we have both the plot and table in a single figure.

Combine Plots with a table: Patchwork
Combine Plots with a table: Patchwork

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow to Collapse Multiple Columns in Pandas? Groupby with Dictionary split apply combine examplePandas GroupBy: Introduction to Split-Apply-Combine Scatter plot tips: Color & Shape by variable9 Tips to Make Better Scatter Plots with ggplot2 in R Boxplot with jittered Data Points in RHow to Make Boxplot in R with ggplot2?

Filed Under: combine plots patchwork, R Tagged With: patchwork, R

Reader Interactions

Trackbacks

  1. How to Combine Multiple Plots? Use: Patchwork - Data Viz with Python and R says:
    January 9, 2020 at 5:42 am

    […] out here for more […]

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version