ggplot2 – Multiple Plots in One Graph Using gridExtra

I’ve been using ggplot2’s facet_wrap and facet_grid feature mostly because multiplots I’ve had to plot thus far were in one way or the other related. However, I needed to plot a multiplot consisting of four (4) distinct plot datasets. In the past, when working with R base graphics, I used the layout() function to achive this [1].
A quick search online initially resulted in this [2] tutorial that involves creating a new function; and interesting these [3, 4] blog posts that propose using the gridExtra package [5].

Install gridExtra

install.packages("gridExtra")

Generate individual plots and arrange in grid

# define individual plots
p1 = ggplot(...)
p2 = ggplot(...)
p3 = ggplot(...)
p4 = ggplot(...)
# arrange plots in grid
grid.arrange(p1, p2, p3, p4, ncol=2)

Sample output is below.

gridExtra Sample

gridExtra Sample

[1] http://lightonphiri.org/blog/r-graphical-representation-multiple-plots-in-one-graph
[2] http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)
[3] http://mcfromnz.wordpress.com/2011/06/09/gridextra-multiple-plots-from-ggplot2
[4] http://www.imachordata.com/extra-extra-get-your-gridextra
[5] http://cran.r-project.org/web/packages/gridExtra

Categories: Research
Tags: , ,
  • lola

    install.packageS()

  • pietrop

    Thank you!

  • ScottRobinett

    thanks

  • Super John

    Thanks for this awesome resource! My follow up question is how did you get those legends in the chart space?

  • Nitish

    Thanks

  • Gandalf

    it says cant find grid.arrange. I installed the grid extra package.

  • Gandalf

    Nevermind I had to add library(gridExtra). Sorry im a noob.

    • Santi L

      add these two lines before using grid.arrange.
      library(gridExtra)
      library(grid)