How to Use the ggarrange() Function in R


Often you may want to arrange multiple ggplot objects on the same page in a specific layout.

One of the easiest ways to do so is by using the ggarrange() function from the egg package in R.

This function uses the following basic syntax:

ggarrange(…, nrow=NULL, ncol=NULL, widths=NULL, heights=NULL, …)

where:

  • : A list of ggplot objects
  • nrow: Number of rows to use in layout
  • ncol: Number of columns to use in layout
  • widths: List of widths to use
  • heights: List of heights to use

Note: The egg package is an extension of the ggplot2 package in R. When you load the egg package, you do not need to also load the ggplot2 package to produce ggplot objects.

The following example shows how to use the ggarrange() function in practice to arrange multiple ggplot objects on one page in a variety of different formats.

Example: How to Use ggarrange() Function in R

For this example we will use the built-in mtcars dataset in R, which contains measurements on 11 different attributes for 32 different cars.

We can use the head() function to view the first few rows of this dataset:

#view first 6 rows of mtcars dataset
head(mtcars)

                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Suppose that we would like to create the following three scatterplots using ggplot2:

  • Plot 1: mpg vs. wt
  • Plot 2: mpg vs. disp
  • Plot 3: mpg vs. hp

Suppose that we would like to display all of these scatterplots on a single page.

We can use the following syntax with the ggarrange() function from the egg package to do so:

library(egg)

#generate three scatterplots
plot1 <- ggplot(mtcars, aes(mpg, wt)) +
  geom_point()

plot2 <- ggplot(mtcars, aes(mpg, disp)) +
  geom_point()

plot3 <- ggplot(mtcars, aes(mpg, hp)) +
  geom_point()

#display all three scatterplots
ggarrange(plot1, plot2, plot3)

This produces the following result:

ggarrange example in R

Note that all three scatterplots that we created are displayed on a single page in one column.

Since we didn’t specify a certain number of rows or columns to use in the layout, the default is to simply display all of the plots stacked on top of each other in a single column.

If we’d like, we can use the nrow and ncol arguments of the ggarrange() function to specify a certain number of rows or columns to use.

For example, we can use the following syntax to specify that we’d like to display the scatterplots using three columns:

library(egg)

#generate three scatterplots
plot1 <- ggplot(mtcars, aes(mpg, wt)) +
  geom_point()

plot2 <- ggplot(mtcars, aes(mpg, disp)) +
  geom_point()

plot3 <- ggplot(mtcars, aes(mpg, hp)) +
  geom_point()

#display all three scatterplots using three total columns
ggarrange(plot1, plot2, plot3, ncol=3)

This produces the following result:

ggarrange example in R with specific number of columns

Notice that this produces all three ggplot objects in three total columns, just as we specified.

Feel free to use whatever values you’d like for the nrow and ncol arguments to display your ggplot objects in any layout that you would like.

Additional Resources

The following tutorials explain how to perform other common tasks in ggplot2:

How to Use scale_y_continuous in ggplot2
How to Rotate Axis Labels in ggplot2
How to Change Legend Labels in ggplot2

Featured Posts

Leave a Reply

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