Show multiple plots from ggplot on one page in R
Last Updated :
09 Oct, 2024
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 achieve this, such as using gridExtra
, cowplot
, or patchwork
packages. In this article, we will explore different methods for showing multiple ggplot2
plots on one page using R Programming Language.
Setting Up Your Environment
Before starting, ensure you have the necessary packages installed. You can install the required packages with the following commands:
# Install ggplot2 and supporting packages if not installed
install.packages("ggplot2")
install.packages("gridExtra")
install.packages("cowplot")
install.packages("patchwork")
library(ggplot2)
library(gridExtra)
library(cowplot)
library(patchwork)
Creating Sample ggplot2 Plots
Let's create a few simple ggplot2
plots that we will later combine onto one page:
R
# Sample data
data(mtcars)
# First plot: Scatter plot
plot1 <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point(color = "blue") +
labs(title = "Scatter plot of MPG vs Weight")
# Second plot: Boxplot
plot2 <- ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +
geom_boxplot(fill = "lightgreen") +
labs(title = "Boxplot of MPG by Cylinders")
# Third plot: Histogram
plot3 <- ggplot(mtcars, aes(x = mpg)) +
geom_histogram(binwidth = 2, fill = "orange") +
labs(title = "Histogram of MPG")
# Fourth plot: Density plot
plot4 <- ggplot(mtcars, aes(x = mpg)) +
geom_density(fill = "lightblue") +
labs(title = "Density plot of MPG")
Method 1: Using gridExtra::grid.arrange()
The gridExtra
package provides the grid.arrange()
function, which can be used to arrange multiple ggplot2
plots on the same page.
R
# Arrange plots in a 2x2 grid
grid.arrange(plot1, plot2, plot3, plot4, ncol = 2)
Output:
Show multiple plots from ggplot on one page in RCustomizing the Layout
You can also adjust the number of rows and columns to display the plots in different configurations.
R
# Arrange plots in a 1x4 layout (1 row, 4 columns)
grid.arrange(plot1, plot2, plot3, plot4, ncol = 4)
Output:
Show multiple plots from ggplot on one page in RMethod 2: Using cowplot::plot_grid()
The cowplot
package is specifically designed for arranging ggplot2
plots. It provides more flexibility, including aligning plots, scaling, and adding annotations.
R
# Combine plots with plot_grid from cowplot
combined_plot <- plot_grid(plot1, plot2, plot3, plot4, ncol = 2)
combined_plot
Output:
Show multiple plots from ggplot on one page in RMethod 3: Using patchwork
patchwork
is another package that simplifies combining multiple ggplot2
plots. It uses a simple intuitive syntax for arranging plots.
R
# Combine plots using patchwork
combined_plot_patchwork <- (plot1 + plot2) / (plot3 + plot4)
combined_plot_patchwork
Output:
Show multiple plots from ggplot on one page in RMethod 4: Using facet_wrap()
or facet_grid()
If your plots share the same x and y axes, you can use facet_wrap()
or facet_grid()
to display multiple plots. This is useful when visualizing multiple subsets of the same data.
R
# Faceted plot using facet_wrap
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
facet_wrap(~ cyl) +
labs(title = "Scatter plot of MPG vs Weight for Different Cylinder Counts")
Output:
Show multiple plots from ggplot on one page in RConclusion
Displaying multiple plots on one page in R using ggplot2
can be achieved in several ways, depending on the level of customization you need. The methods discussed here – gridExtra::grid.arrange()
, cowplot::plot_grid()
, and patchwork
– provide versatile solutions for arranging and combining plots. You can choose the method that best fits your requirements based on ease of use and flexibility.
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
R ggplot2 - Multi Panel Plots In this article, we are going to see how to plot Multi Panel Plots using ggplot2 in R Programming language. Plots are one of the most important aspects of data visualization. They help us to quickly identify trends and relationships in the raw data. But sometimes one plot is not enough to derive the
2 min read
How to Combine Multiple ggplot2 Plots Use Patchwork 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
5 min read
Multiple Line Plots or Time Series Plots with ggplot2 in R In this article, we will discuss how to plot Multiple Line Plots or Time Series Plots with the ggplot2 package in the R Programming Language. We can create a line plot using the geom_line() function of the ggplot2 package. Syntax: ggplot( df, aes( x, y ) ) + geom_line() where, df: determines the da
2 min read
How to Export Multiple Plots to PDF in R? In this article, we will learn how to export multiple plots to a PDF in the R Programming Language. Save Multiple plots on different pages on PDF file: To save multiple plots in pdf we use the pdf() function to create and open a pdf file in the R Language. Â After this, whatever we do in the R consol
2 min read