Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with grid.arrange:input must be grobs

Tags:

r

ggplot2

I have the following R-Script:

    library(ggplot2)
    library(gridExtra)
    Sys.setenv(LANG ="en")
    c1 <- ggplot(mtcars, aes(factor(cyl))) + geom_bar()
    c2 <- ggplot(mtcars, aes(factor(cyl))) + geom_bar() + coord_flip()
    grid.arrange(c1, c2, ncols=1)

I get the following error with grid.arrange:

Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main, : input must be grobs!

I can't figure out what causes the problem.

Here are my used versions:

sessionInfo() R version 3.0.2 (2013-09-25) Platform: x86_64-apple-darwin10.8.0 (64-bit)

    locale:
    [1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8

    attached base packages:
    [1] grid      stats     graphics  grDevices utils     datasets  methods   base     

    other attached packages:
    [1] gridExtra_0.9.1 ggplot2_0.9.3.1

    loaded via a namespace (and not attached):
     [1] colorspace_1.2-4 digest_0.6.4     gtable_0.1.2     labeling_0.2     MASS_7.3-31      munsell_0.4.2    plyr_1.8.1       proto_0.3-10     Rcpp_0.11.1      reshape2_1.2.2  
    [11] scales_0.2.4     stringr_0.6.2    tools_3.0.2             
like image 682
JerryWho Avatar asked Apr 24 '14 06:04

JerryWho


1 Answers

I liked this particular error, this is a stealthy one. Long story short, the parameter is ncol, not ncols. In your code, 1 is treated as a plot object, so that's why it fails, not because ggplots are invalid. The error message is not very helpful, which obscures the situation.

# same error as with ncols=1
grid.arrange(c1, c2, blah=1)
# fine
grid.arrange(c1, c2, ncol=1)
like image 100
tonytonov Avatar answered Oct 01 '22 18:10

tonytonov