Hengrui Jiang
Hengrui Jiang

Reputation: 881

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?

Upvotes: 0

Views: 1097

Answers (2)

baptiste
baptiste

Reputation: 77116

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"))

Upvotes: 2

Hengrui Jiang
Hengrui Jiang

Reputation: 881

After hours of googling, I found the answer directly after having posted the question.....

The following works:

test <- do.call("grid.arrange",c(plots.list, ncol=2, main =substitute(textGrob("test"),env = parent.frame())))

Upvotes: 0

Related Questions