Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

store arrangeGrob to object, does not create printable object

I want to save, but not print (for now), a bunch of ggplot()s into a grid (via arrangeGrob(), correct?), then print and retrieve them later.

This is a reboot of an existing question. Strangely, that answer does not work, and I have no idea why. I am using the exact same code.

  library(ggplot2)
  p1 <- ggplot(mtcars, aes(x=factor(cyl), y=mpg)) + geom_boxplot()
  p2 <- ggplot(mtcars, aes(x=factor(cyl), y=wt)) + geom_boxplot()
  library(gridExtra)
  y <- arrangeGrob(p1, p2)
  class(y)
  y

Strangely, that does not (as in the above answer) yield the grid of plots, but:

> class(y)
[1] "gtable" "grob"   "gDesc" 
> y
TableGrob (2 x 1) "arrange": 2 grobs
  z     cells    name           grob
1 1 (1-1,1-1) arrange gtable[layout]
2 2 (2-2,1-1) arrange gtable[layout]

What is going on here?

like image 797
maxheld Avatar asked Jul 16 '15 15:07

maxheld


1 Answers

The gridExtra package has been updated recently thereby changing how arrangeGrob works internally and what kind of object it returns (now a gtable).

You need to call grid.draw:

grid.draw(y)

resulting plot

Edit: do not use plot() as initially suggested; it will add a grey background, and is only meant to be used for debugging gtables.

like image 98
Roland Avatar answered Oct 28 '22 14:10

Roland