How to combine plots and table made with ggplot (or grid graphics) in R

[This article was first published on Roman Luštrik not Inc. - R, 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.

In the last few years, a number of options of how to combine grid graphics (incl. ggplot2) have emerged. If you can’t remember all functions to do this, this post may serve as a reference guide, but it’s mostly for me because I keep forgetting the functionalities provided by cowplot and patchwork packages (thank you Jaap). Here appearing in order I encountered them.


# prepare some figures
fig1 <- ggplot(mtcars, aes(x = mpg, y = disp, color = as.factor(gear))) +
  theme_bw() +
  theme(legend.position = "top") +
  scale_color_discrete(name = "gear") +

fig2 <- ggplot(mtcars, aes(x = gear, y = disp, fill = as.factor(gear))) +
  theme_bw() +
  theme(legend.position = "top") +
  scale_fill_discrete(name = "gear") +


Here is oldie but goldie from Baptiste's gridExtra package. You can, for example, specify the layout matrix or specify number of columns. The below code chunk is using ggplot2::ggsave which saves the last subplot only, which is why I saved the result of grid.arrange into a new variable (but see cowplot below).


fig.combined1 <- grid.arrange(fig1, fig2, ncol = 2)
ggsave(plot = fig.combined1, filename = "grid.arrange.png",
       width = 8, height = 4, units = "in", dpi = 96)

oldie but goldie grid arrange


Next is cowplot, which comes with nice vignettes. What I like about this package is the elegant adding of letters to subplots, e.g.


plot_grid(fig1, fig2, labels = "AUTO")
cowplot::ggsave(filename = "plotgrid.png", width = 8,
                height = 4, units = "in", dpi = 96)

See the vignettes for more information. Package also packs its own ggplot which masks the function ggplot2::ggsave (read: ggsave from package ggplot2). The beauty of it is that it saves the gridded plot, not the last (sub)plot.

cowplot's grid_plot


Thomas did a great job of making combining of plots trivially easy. The GitHub repository of the package is a rich source of information and I could never do it justice. Here's my botched attempt of just an example of what the package functionality provides.


fig1 + fig2

patchwork plus

To leave a comment for the author, please follow the link and comment on their blog: Roman Luštrik not Inc. - R. 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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)