Side-by-Side plots with ggplot2

How to Create Side-by-Side Plots in ggplot2?. Using the ggplot2 package in R, you can often construct two plots side by side.

Fortunately, with the patchwork and gridExtra packages, this is simple to accomplish.

Side-by-Side plots with ggplot2

Let’s start with the packages.


This lesson walks you through various examples of how to generate side-by-side plots with these packages.

Approach 1: Two Side-by-Side Plots

Let’s start with a density plot

How to find the Mean Deviation? MD Vs MAD-Quick Guide »

data1 <- data.frame(x = rnorm(700))
plot1 <- ggplot(data1, aes(x = x)) +       

Create a scatter plot

data2 <- data.frame(x = rnorm(2000),y = rnorm(2000))
plot2 <- ggplot(data2, aes(x = x, y = y))+ geom_point()

Display plots side by side

Coefficient of Variation Example » Acceptable | Not Acceptable»

plot1 + plot2

Approach 2: Three Side-by-Side Plots

If you wanted to make a three-by-three plot, you might use the same procedure.

Create a box plot

plot3 <- ggplot(data2, aes(x = x, y = y)) +  geom_boxplot()
 plot1+ plot2+ plot3 

Approach 3: Two Stacked Plots

We can also make a stacked plot using the same approach.

Measures of Central Tendency » Mean, Median & Mode »

Display plots stacked on top of each other

plot1 / plot2

Approach 4: Customization

If you want to add titles, subtitles, and captions to the plots, lets join the plot and use plot_annotation function.

How to clean the datasets in R? » janitor Data Cleansing »

combined <- plot1 + plot2
combined + plot_annotation(
title = 'Combined Plot,
subtitle = 'This is a subtitle that describes more information about the plots',
caption = 'This is a caption')

Approach 5: Customization

We can make use of gridextra package also

grid.arrange(plot1, plot2, ncol = 2) 
grid.arrange(plot1, plot2, nrow = 2)    

LSTM Network in R » Recurrent Neural network »

You may also like...

Leave a Reply

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

15 − five =