0

I am using gridExtra to display multiple plots and I found that somehow ggplot objects are not saved properly. Take a look at this simple code:

Working version:

gs <- list()
for(i in 1:4){
  gs[[i]] <- qplot(rnorm(100), geom = "histogram", main = i)
}
grid.arrange(grobs = gs, ncol = 2)

Problem version:

gs <- list()
for(i in 1:4){
  x <- rnorm(100)
  gs[[i]] <- qplot(x, geom = "histogram", main = i)
}
grid.arrange(grobs = gs, ncol = 2)

The second version produces identical plots in my R workspace. The version I am using is ggplot2-v2.2.1 and gridExtra-v2.3

aosmith
  • 34,856
  • 9
  • 84
  • 118
ywx
  • 147
  • 2
  • 7
  • See some of the explanation [here](https://stackoverflow.com/a/39800861/2461552), as what's happening is `qplot` seems related. You could do `qplot(x, geom = "histogram", data = data.frame(x = x), main = i)`. – aosmith Apr 23 '18 at 22:48
  • That link explains exactly the problem. Thanks for pointing it out. – ywx Apr 24 '18 at 16:18

0 Answers0