Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I arrange an arbitrary number of ggplots using grid.arrange?
    primarykey
    data
    text
    <p><strong><em>This is cross-posted on the ggplot2 google group</em></strong></p> <p>My situation is that I'm <a href="https://github.com/briandk/granova/blob/dev/R/granova.contr.ggplot.R" rel="noreferrer" title="Click here to see the development repository for our graphical analysis of variance package in R">working on a function</a> that outputs an arbitrary number of plots (depending upon the input data supplied by the user). The function returns a list of n plots, and I'd like to lay those plots out in 2 x 2 formation. I'm struggling with the simultaneous problems of:</p> <ol> <li>How can I allow the flexibility to be handed an arbitrary (n) number of plots?</li> <li>How can I also specify I want them laid out 2 x 2</li> </ol> <p>My current strategy uses <code>grid.arrange</code> from the <code>gridExtra</code> package. It's probably not optimal, especially since, and this is key, <em>it totally doesn't work</em>. Here's my commented sample code, experimenting with three plots:</p> <pre><code>library(ggplot2) library(gridExtra) x &lt;- qplot(mpg, disp, data = mtcars) y &lt;- qplot(hp, wt, data = mtcars) z &lt;- qplot(qsec, wt, data = mtcars) # A normal, plain-jane call to grid.arrange is fine for displaying all my plots grid.arrange(x, y, z) # But, for my purposes, I need a 2 x 2 layout. So the command below works acceptably. grid.arrange(x, y, z, nrow = 2, ncol = 2) # The problem is that the function I'm developing outputs a LIST of an arbitrary # number plots, and I'd like to be able to plot every plot in the list on a 2 x 2 # laid-out page. I can at least plot a list of plots by constructing a do.call() # expression, below. (Note: it totally even surprises me that this do.call expression # DOES work. I'm astounded.) plot.list &lt;- list(x, y, z) do.call(grid.arrange, plot.list) # But now I need 2 x 2 pages. No problem, right? Since do.call() is taking a list of # arguments, I'll just add my grid.layout arguments to the list. Since grid.arrange is # supposed to pass layout arguments along to grid.layout anyway, this should work. args.list &lt;- c(plot.list, "nrow = 2", "ncol = 2") # Except that the line below is going to fail, producing an "input must be grobs!" # error do.call(grid.arrange, args.list) </code></pre> <p>As I am wont to do, I humbly huddle in the corner, eagerly awaiting the sagacious feedback of a community far wiser than I. Especially if I'm making this harder than it needs to be.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload