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 to options available:
1, See the accepted answer,
2, Which is come from a deleted answer with little modification by me
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.
Using the results from split() function, we can create a list of plots, ggplot objects, using map() function in purrr R package. In this example, map() makes a scatter plot for each species.
Allowed values are one of c("top", "bottom", "left", "right", "none"). To remove the legend use legend = "none".
To arrange multiple ggplot2 graphs on the same page, the standard R functions - par() and layout() - cannot be used. The basic solution is to use the gridExtra R package, which comes with the following functions: grid. arrange() and arrangeGrob() to arrange multiple ggplots on one page.
Currently, the only way to change the space between multiple plot is ggarrange() is to change the margins of each plot (#58). An option to change the spacing in the ggarrange() itself would improve the user experience.
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"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With