Reputation: 1292
I have lots of plot that I want to put them on one page, ggarrange
does a good work on this, however, it seems like I have to put each of those plots in the list in which they are stored as input of this ggarrange
function, other than put the list as input directly, see following for details:
A naive example:
p1 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
geom_point()
p2 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
geom_point() + facet_wrap( ~ cyl, ncol=2, scales = "free") +
guides(colour="none") +
theme()
plot_list = list(p1,p2)
What I can do for now:
ggarrange(p1,p2, widths = c(2,1), labels = c("a", "b"))
What I really want but failed to do:
ggarrange(plot_list, widths = c(2,1), labels = c("a", "b"))
Anyone know how? this could save a lot of time if the number of plots is large and may change from time to time. The sample is not mine, copied from here.
======= EDIT ========
According to the excellent answers below, there are at least two options available:
do.call(ggarrange, c(plot_list[1:2], widths = c(2, 1), labels = c("a", "b")))
To pass argument to function ggarrange
, c()
worked for me but as.list()
did not.Upvotes: 23
Views: 21543
Reputation: 21
egg::ggarrange(plots = plot_list, widths = c(2,1), labels = c("a", "b"))
Upvotes: 1
Reputation: 206486
Check out the help file for ?ggarrange
. It has a plotlist=
parameter. Just pass your list there.
ggarrange(plotlist=plot_list, widths = c(2,1), labels = c("a", "b"))
Upvotes: 25