Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggarrange plot all plots in a list

Tags:

r

ggplot2

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.

like image 315
Jason Goal Avatar asked Oct 29 '18 15:10

Jason Goal


People also ask

Can you make a list of plots in R?

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.

How do you get rid of the legend on Ggarrange?

Allowed values are one of c("top", "bottom", "left", "right", "none"). To remove the legend use legend = "none".

How would you make multiple plots onto a single page in R?

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.

How do you increase space between plots in Ggarrange?

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.


1 Answers

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"))
like image 107
MrFlick Avatar answered Sep 18 '22 17:09

MrFlick