0

A can make a unique image with 4 graphs using a code like:

g1<-ggplot( data1, aes(x=data1$dados ) ) + ...
g2<-ggplot( data2, aes(x=data2$dados ) ) + ...
g3<-ggplot( data3, aes(x=data3$dados ) ) + ...
g4<-ggplot( data4, aes(x=data4$dados ) ) + ...
grid.arrange(g1, g2, g3, g4, nrow=2, ncol=2)

and get something like the image below: enter image description here

However, I would like to automatize the number of graphs in the image without having to add another variable for each new graph. Is it possible to construct images from a "vector of graphs", instead of telling variable names for grid.arrange( g1, g2, ..., gn, nrow=x, ncol=y) ?

Denio Mariz
  • 1,065
  • 1
  • 10
  • 12
  • 2
    You can load it in a list `do.call("grid.arrange", c(mget(ls(pattern = "^g\\d+$")), list(nrow = x, ncol = y)))` Or better would be to do all the computations in the `list` instead of creating `data1', 'data2', and `g1`, `g2` etc. i.e. `library(purrr);library(readr); gs <- map(files, ~read_csv(.x) %>% ggplot(aes(x = dados)) + ...)) – akrun Oct 13 '19 at 15:49
  • The libraries `cowplot` and `patchwork` (probably a few others that I'm less familiar with) can take a list of plots of variable length and arrange them. Also [don't use `$` inside `aes`](https://stackoverflow.com/questions/32543340/issue-when-passing-variable-with-dollar-sign-notation-to-aes-in-combinatio) – camille Oct 13 '19 at 16:22
  • @Denio: see this too https://stackoverflow.com/a/52045613/786542 – Tung Oct 13 '19 at 22:07

0 Answers0