store arrangeGrob to object, does not create printable object

血红的双手。 提交于 2019-11-26 21:35:27

问题


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?


回答1:


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)

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



来源:https://stackoverflow.com/questions/31458051/store-arrangegrob-to-object-does-not-create-printable-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!