How to Use the patchwork Package in R (With Examples)


Often you may want to combine several different ggplot objects into one single graphic.

One of the easiest ways to do so is by using the patchwork package in R.

This package allows you to easily combine different ggplot objects with simple syntax.

Before using this package, you may first need to install it by using the following command:

install.packages('patchwork')

Once the package is installed, you can then use it with the ggplot2 package to arrange plots.

The following example shows how to use the patchwork function in practice to arrange multiple ggplot objects in one graphic in a variety of different formats.

Example: How to Use the patchwork Package in R

For this example we will use the built-in mtcars dataset in R, which contains measurements on 11 different attributes for 32 different cars.

We can use the head() function to view the first few rows of this dataset:

#view first 6 rows of mtcars dataset
head(mtcars)

                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Suppose that we would like to create the following three scatterplots using ggplot2:

  • Plot 1: mpg vs. wt
  • Plot 2: mpg vs. disp
  • Plot 3: mpg vs. hp

Suppose that we would like to display all of these scatterplots on a single page.

We can use the following syntax with the patchwork package to do so:

library(ggplot2)
library(patchwork)

#generate three scatterplots
plot1 <- ggplot(mtcars, aes(mpg, wt)) +
  geom_point()

plot2 <- ggplot(mtcars, aes(mpg, disp)) +
  geom_point()

plot3 <- ggplot(mtcars, aes(mpg, hp)) +
  geom_point()

#display all three scatterplots in same graphic
plot1 + plot2 + plot3

This produces the following result:

patchwork package in R example

All three scatterplots that we created are displayed on one page in a single row.

By using a plus sign ( + ) in between each plot name, we are able to specify that we would like to add each consective plot to the same row.

Note that we can achieve the same result by using a straight line ( | ) in between each plot name as well:

library(ggplot2)
library(patchwork)

#generate three scatterplots
plot1 <- ggplot(mtcars, aes(mpg, wt)) +
  geom_point()

plot2 <- ggplot(mtcars, aes(mpg, disp)) +
  geom_point()

plot3 <- ggplot(mtcars, aes(mpg, hp)) +
  geom_point()

#display all three scatterplots in same graphic
plot1 | plot2 | plot3

This produces the following result:

patchwork package in R example

Once again, all three scatterplots that we created are displayed on one page in a single row.

We can also use a forward slash ( / ) to indicate that we would like specific plots placed into a single column:

library(ggplot2)
library(patchwork)

#generate three scatterplots
plot1 <- ggplot(mtcars, aes(mpg, wt)) +
  geom_point()

plot2 <- ggplot(mtcars, aes(mpg, disp)) +
  geom_point()

plot3 <- ggplot(mtcars, aes(mpg, hp)) +
  geom_point()

#display all three scatterplots in same graphic
plot1 | plot2 / plot3

This produces the following result:

R patchwork package example with rows and columns

Notice that plot2 and plot3 are now displayed in a single column.

Feel free to use any combination of the straight lines ( | ) and forward slashes ( / ) to display the ggplot objects in any layout you would like.

Note: You can find the complete documentation for the patchwork package in R here.

Additional Resources

The following tutorials explain how to perform other common tasks in ggplot2:

How to Use the ggarrange() Function in R
How to Use scale_y_continuous in ggplot2
How to Rotate Axis Labels in ggplot2
How to Change Legend Labels in ggplot2

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *