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
1.1k views
in Technique[技术] by (71.8m points)

r - grid.arrange from gridExtras exiting with "only 'grobs' allowed in 'gList'" after update

I just updated R, R Studio, and a bunch of packages including ggplot2 and gridExtras on my Mac. Now gridExtras is failing in basic plotting with the error:

"only 'grobs' allowed in "gList""

Here's some code that should work but does not:

library(ggplot2)
p1 = qplot(1:10,rnorm(10))
p2 = qplot(1:10,rnorm(10))
library(gridExtra)
grid.arrange(p1, p2, ncol=2, main = "Main title")

This dumps out the following error:

Error in gList(list(grobs = list(list(x = 0.5, y = 0.5, width = 1, height = 1,  : 
  only 'grobs' allowed in "gList"
In addition: Warning message:
In grob$wrapvp <- vp : Coercing LHS to a list

Any help is appreciated!

question from:https://stackoverflow.com/questions/34838870/grid-arrange-from-gridextras-exiting-with-only-grobs-allowed-in-glist-afte

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

1 Reply

0 votes
by (71.8m points)

It is because grid.arrange does not have a main parameter anymore (seems to have been eliminated around Summer 2015 with the 2.0.0 release) and thus thinks that the main= parameter must be a grob. To replace the main, you can use the top parameter now (as well as a bottom, left, and right).

So this works for example:

library(ggplot2)
p1 = qplot(1:10,rnorm(10))
p2 = qplot(1:10,rnorm(10))

library(gridExtra)
grid.arrange(p1, p2, ncol=2,top="Main Title")

The message is a bit confusing, that is because it looks at all the parameters it does not know and assumes they might be grobs (graphical objects) that it can plot. A confusing error message like this is the price you pay for that flexibility.

Note: - if you have a lot of grobs you should consider packing them all into a list and use the form:

grid.arrange( grobs = list(p1,p2,...),...

Here is what that above code results in:

enter image description here


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

...