How to Combine Multiple ggplot2 Plots Use Patchwork
Last Updated :
17 Apr, 2023
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
library (ggplot2)
library (patchwork)
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" )
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
library (ggplot2)
library (patchwork)
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" )
(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
library (ggplot2)
library (patchwork)
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" )
(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
library (ggplot2)
library (patchwork)
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" )
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:
Share your thoughts in the comments
Please Login to comment...