Combine Multiple Plots with cowplot


The cowplot package in R is an extension of the ggplot2 package, designed to simplify the process of combining multiple plots into a single layout.
This post showcases the key features of cowplot and provides a set of graph examples using the package.

Documentation

{cowplot}


Warning! The cowplot package is very similar to the patchwork package. Patchwork is easier to use (intuitive syntax) and provides the same main features!


Quick start


The cowplot package in R is an extension of the ggplot2 package, designed to simplify the process of combining multiple plots into a single layout.

It offers a set of functions that make it easy to arrange plots in a grid, add labels, and create publication-ready figures.

✍️ author β†’ Claus O. Wilke

πŸ“˜ documentation β†’ wilkelab.org/cowplot

⭐️ more than 700 stars on github

Installation


To get started with cowplot, you can install it directly from CRAN using the install.packages function:

install.packages("cowplot")

Basic usage


The cowplot package allows you to combine multiple plots using the plot_grid() function.

Here’s a basic example:

library(ggplot2)
library(cowplot)

p1 <- ggplot(mtcars) +
   geom_point(aes(mpg, disp))
p2 <- ggplot(mtcars) +
   geom_boxplot(aes(gear, disp, group = gear))
plot_grid(p1, p2)

Key features



β†’ Combining Plots

You can combine multiple plots using the plot_grid() function.

Example:

plot_grid(p1, p2, p1, ncol = 3)


β†’ Arranging Plots

Use the nrow and ncol arguments to control the layout.

Example:

plot_grid(p1, p2, nrow = 2)


β†’ Adding Labels

You can add labels to your plots using the labels argument.

Example:

plot_grid(p1, p2, labels = c("A", "B"))


β†’ Adjusting Relative Sizes

Control the relative sizes of plots with the rel_widths and rel_heights arguments.

Example:

plot_grid(p1, p2, rel_widths = c(2, 1))





❀️ 10 best R tricks ❀️

πŸ‘‹ After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! πŸ”₯