Open In App

Show multiple plots from ggplot on one page in R

Last Updated : 09 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

gh
Show multiple plots from ggplot on one page in R

Customizing 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:

gh
Show multiple plots from ggplot on one page in R

Method 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:

gh
Show multiple plots from ggplot on one page in R

Method 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:

gh
Show multiple plots from ggplot on one page in R

Method 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:

gh
Show multiple plots from ggplot on one page in R

Conclusion

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.


Next Article

Similar Reads