Achal Neupane
Achal Neupane

Reputation: 5719

do.call with options in r to arrange ggplot list

I have my dummy data as:

x=1:7
y=1:7
df = data.frame(x=x,y=y)
bp <- vector("list", length = 4)
for (i in 1:4) {
 bp[[i]] <- ggplot(df,aes(x,y))+geom_point()
}

I have my ggplot objects in a list called bp with which I can generate a four-plot-grid as:

figure <- ggarrange(bp[[1]], bp[[2]], bp[[3]], bp[[4]],
                    labels = c("A", "B", "C", "D"),
                    ncol = 2, nrow = 2) 

Now, I don't want to type bp[[1]]..bp[[4]] and want to use something like

do.call(ggarrange, bp) as you would do with function grid.arrange like do.call(grid.arrange, bp). do.call(grid.arrange, bp) doesn't show the panel labels so I would like to use ggarrange and still would want to pass these arguments:

labels = c("A", "B", "C", "D"),
                    ncol = 2, nrow = 2

How can I use do.call in this case? Is there a better way to do this?

Upvotes: 4

Views: 1001

Answers (1)

akrun
akrun

Reputation: 887691

The option is to place it in a named list and concatenate the list elements

library(ggpubr)
do.call(ggarrange, c(bp, list(labels = c("A", "B", "C", "D"),
                 ncol = 2, nrow = 2) ))

Here, the. OP's request is about using do.call with ggarrange. Of course, it can be done without do.call

enter image description here

Upvotes: 1

Related Questions