Search code examples
rggplot2gridextragrob

Unable to pass textGrob as main to do.call("arrangeGrob")


While

test <- do.call("arrangeGrob", c(plots.list[1:2],ncol=2,main=("test")) 

works just fine,

test <- do.call("arrangeGrob", c(plots.list[1:2],ncol=2,main=textGrob("test")))

gives the following error:

"Error in arrangeGrob(list(grobs = list(list(x = 0.5, y = 0.5, width = 1, : input must be grobs!"

I need the main to be a textGrob in order to set font size and font face. Anyone has an idea what I am doing wrong?


Solution

  • The problem comes from the fact that the list of parameters for do.call is not correct,

    c(list(1, 2), ncol=1, textGrob("a"))
    

    "exposes" the contents of textGrob, while you really want to append two lists,

    c(list(1, 2), list(ncol=1, textGrob("a")))
    

    Applied to your question, this becomes

    do.call("grid.arrange", c(plots.list[1:2],list(ncol=2, main=textGrob("test")))) 
    

    but note that the upcoming version of gridExtra (>= 2.0.0) no longer recognises main, you should use top instead

    do.call("grid.arrange", c(plots.list[1:2],list(ncol=2, top=textGrob("test")))) 
    

    and, since arrangeGrob gained a new grobs argument, you don't need do.call anymore,

    grid.arrange(grobs=plots.list[1:2], ncol=2, top=textGrob("test"))