How to Combine Multiple ggplot2 Plots Use Patchwork
Last Updated :
24 Apr, 2025
In this article, we are going to learn how to combine multiple ggplot2 plots using patchwork in the R programming language.
ggplot2 is a popular data visualization package in R that is used to create complex and beautiful plots. However, sometimes we may want to combine multiple plots for comparison or create a more detailed visualization. This is where the patchwork package helps, as it is used to easily combine multiple ggplot2 plots into a single layout.
Prerequisites concepts
- ggplot2: A data visualization package in R that is used to create a wide variety of plots.
- Patchwork: An R package that provides a simple and flexible way to combine multiple plots created with ggplot2 library.
- Plot layout: The arrangement of multiple plots in a single visualization, including the number of rows and columns, the size of each plot, and the spacing between them.
Stepwise implementation of combining multiple plots
To combine multiple ggplot2 plots using patchwork, we have to follow these steps as listed below:
Step 1: Install the necessary packages.
install.packages("ggplot2")
install.packages("patchwork")
Step 2: Import the required libraries which are ggplot2 and patchwork.
library(ggplot2)
library(patchwork)
Step 3: Create the individual plots with the help of ggplot(), geom_point(), labs() and geom_bar() functions of ggplot2 library.
p1 <- ggplot(mtcars, aes(x = mpg, y = wt)) +
geom_point() +
labs(title = "Scatterplot of mpg and wt")
p2 <- ggplot(mtcars, aes(x = factor(cyl), fill = factor(am))) +
geom_bar(position = "dodge") +
labs(title = "Barplot of cyl and am")
Step 4: Combine the plots using the + operator that combine two plots horizontally. We can chain multiple + operators to combine more than two plots. On other hand, we can use the / operator to combine two plots vertically. We can use parentheses to group plots together and specify different layouts.
p1 + p2
Step 5: Customize the plot layout as needed using the plot_layout() function. For example, to arrange the plots vertically with a 2:1 aspect ratio.
p1 + p2 + plot_layout(ncol = 1, heights = c(2, 1))
Example 1: Combine multiple ggplot2 plots into a single visualization
Here's an example of how to use patchwork to combine multiple ggplot2 plots into a single visualization. In this example, we've combined a scatterplot of mpg and wt with a barplot of cyl and am into a single visualization using patchwork. We've arranged the plots vertically with a 2:1 aspect ratio, which makes it easier to compare the two plots.
R
# Import required libraries
library(ggplot2)
library(patchwork)
# Create the individual ggplot2 plots
p1 <- ggplot(mtcars, aes(x = mpg, y = wt)) +
geom_point() +
labs(title = "Scatterplot of mpg and wt")
p2 <- ggplot(mtcars, aes(x = factor(cyl), fill = factor(am))) +
geom_bar(position = "dodge") +
labs(title = "Barplot of cyl and am")
# Combine the plots using patchwork
p1 + p2 + plot_layout(ncol = 1, heights = c(2, 1))
Output:
 Example 2: Combine multiple plots with a common x-axis
In this example, we've created three scatterplots of mpg against different variables and combined them using patchwork with a common x-axis. Here we have used the "/" operator which is used to combine two plots vertically. We can use parentheses to group plots together and specify different layouts as shown in the below example.
R
# Import required libraries
library(ggplot2)
library(patchwork)
# Create the individual ggplot2 plots
p1 <- ggplot(mtcars, aes(x = mpg, y = wt)) +
geom_point() +
labs(title = "Scatterplot of mpg and wt")
p2 <- ggplot(mtcars, aes(x = mpg, y = qsec)) +
geom_point() +
labs(title = "Scatterplot of mpg and qsec")
p3 <- ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point() +
labs(title = "Scatterplot of mpg and hp")
# Combine the plots using patchwork
(p1 / p2 / p3) + plot_layout(ncol = 1)
Output:
 Example 3: Combine multiple plots with a common legend
In this example, we've created two scatterplots of mpg against different variables, with the points colored by the number of cylinders. We've combined the plots using patchwork, added a common title, and used the plot_spacer() function to add some vertical space between the plots. We've also used plot_layout(guides = "collect") to collect the legends from both plots into a single legend.
R
# Import required libraries
library(ggplot2)
library(patchwork)
# Create the individual ggplot2 plots
p1 <- ggplot(mtcars, aes(x = mpg,
y = wt,
color = factor(cyl))) +
geom_point() +
labs(title = "Scatterplot of mpg and wt")
p2 <- ggplot(mtcars, aes(x = mpg,
y = qsec,
color = factor(cyl))) +
geom_point() +
labs(title = "Scatterplot of mpg and qsec")
# Combine the plots using patchwork
(p1 / p2) + plot_layout(ncol = 1) +
plot_spacer() +
plot_annotation(title = "Scatterplots of mpg and wt/qsec",
theme = theme(plot.title = element_text(hjust = 0.5))) +
plot_layout(guides = "collect")
Output:
 Example 4: Combine multiple plots with different sizes
In this example, we've created a scatterplot of mpg against wt, and two bar plots of the number of cylinders by transmission type. We've combined the plots using patchwork, with the scatterplot on the left and the two bar plots on the right. We've also used plot_layout() to specify the widths and heights of the individual plots and added a common title using plot_annotation().
R
# Import required libraries
library(ggplot2)
library(patchwork)
# Create the individual ggplot2 plots
p1 <- ggplot(mtcars, aes(x = mpg, y = wt)) +
geom_point() +
labs(title = "Scatterplot of mpg and wt")
p2 <- ggplot(mtcars, aes(x = factor(cyl),
fill = factor(am))) +
geom_bar(position = "dodge") +
labs(title = "Barplot of cyl and am")
p3 <- ggplot(mtcars, aes(x = factor(cyl),
fill = factor(am))) +
geom_bar(position = "fill") +
labs(title = "Stacked barplot of cyl and am")
# Combine the plots using patchwork
p1 + (p2 / p3) +
plot_layout(ncol = 2,
widths = c(3, 2),
heights = c(2, 1)) +
plot_annotation(title = "Scatterplot and Barplots of cyl and am",
theme = theme(plot.title = element_text(hjust = 0.5)))
Output:
Â
Similar Reads
How to Combine Multiple ggplot2 Plots in R?
In this article, we will discuss how to combine multiple ggplot2 plots in the R programming language. Combining multiple ggplot2 plots using '+' sign to the final plot In this method to combine multiple plots, here the user can add different types of plots or plots with different data to a single p
2 min read
How To Join Multiple ggplot2 Plots with cowplot?
In this article, we are going to see how to join multiple ggplot2 plots with cowplot. To join multiple ggplot2 plots, we use the plot_grid() function of the cowplot package of R Language. Syntax: plot_grid(plot1,plot2,label=<label-vector>, ncol, nrow) Parameters: plot1 and plot2 are plots that
3 min read
Show multiple plots from ggplot on one page in R
When working with data visualization in R using the ggplot2 package, there are times when you need to display multiple plots on the same page or grid. This is useful when you want to compare different plots or display multiple visualizations in a report or presentation. R offers several ways to achi
3 min read
Draw Multiple ggplot2 plots Side-by-Side
In the R programming language, we can analyze data by creating different graphs or plot out of them. Sometimes for the analysis, we need to see two or more plots at the same time side by side. In that case, we use gridExtra package of R language that divides the frame into grids, and you can add mul
2 min read
How to create a plot using ggplot2 with Multiple Lines in R ?
In this article, we will discuss how to create a plot using ggplot2 with multiple lines in the R programming language. Method 1: Using geom_line() function In this approach to create a ggplot with multiple lines, the user need to first install and import the ggplot2 package in the R console and then
3 min read
Combine Multiple ggplot2 Legend in R
In this article, we will see how to combine multiple ggplot2 Legends in the R programming language. Installation First, load the ggplot2 package by using the library() function. If you have not installed it yet, you can simply install it by writing the below command in R Console. install.packages("g
2 min read
How To Make Dumbbell Plot in R with ggplot2?
The Dumbbell Plot shows the change between two points in our dataset. It is named so because of its Dumbbell shape. It helps us to understand the span of data categorically. To make Dumbbell Plot in R using ggplot2, Â we use the geom_dumbbell() function. Syntax: geom_dumbbell(data, aes(y, x, xend), s
2 min read
Draw Composition of Plots Using the patchwork Package in R
In this article, we will discuss how to draw a composition of plots using the patchwork package in R programming language. The patchwork package makes it easier to plot different ggplots in a single graph. We will require both ggplot2 packages for plotting our data and the patchwork package to combi
4 min read
How to change plot area margins using ggplot2 in R?
In the R programming language, ggplot2 is a popular library for creating data visualizations. One of the key benefits of using ggplot2 is the ability to customize the appearance of plots in a variety of ways. In this article, we will explore some of the ways you can customize the appearance of your
3 min read
How to Plot a Smooth Line using ggplot2 in R ?
In this article, we will learn how to plot a smooth line using ggplot2 in R Programming Language. We will be using the "USArrests" data set as a sample dataset for this article. Murder Assault UrbanPop Rape Alabama 13.2 236 58 21.2 Alaska 10.0 263 48 44.5 Arizona 8.1 294 80 31.0 Arkansas 8.8 190 50
3 min read