Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

r - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...