# Side-by-Side plots with ggplot2

**Methods – finnstats**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

library(ggplot2) library(patchwork)

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 »

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

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() plot3

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

library("gridExtra") grid.arrange(plot1, plot2, ncol = 2)

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

LSTM Network in R » Recurrent Neural network »

The post Side-by-Side plots with ggplot2 appeared first on finnstats.

**leave a comment**for the author, please follow the link and comment on their blog:

**Methods – finnstats**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.