0

I'm in need of a sanity check. I am struggling to see why the result of plot_grid (cowplot) of N plots in my code is producing N identical plots. From the list I provide, I've taken out each data frame to verify that each plot should be different, however, when I pass in the complete list to plot_grid they all look identical.

p <- vector("list",length(dataList))
for(i in 1:length(dataList)) {
  df <- dataList[[i]]
  p[[i]] <- ggplot(df, aes(df$base)) + geom_bar()
}

multi <- plot_grid(plotlist=p, align="hv")

save_plot(paste("data_freqs.tiff",sep=""), multi, dpi=300, base_aspect_ratio=1.5)

For example, when type the following I can see the data is different:

a<-dataList[[1]]
b<-dataList[[2]]
sum(a$base=="T")
>1245
sum(b$base=="T")
>1034

However, I end up with multiple plots of identical T values (all fixed to 1245).

halfer
  • 19,824
  • 17
  • 99
  • 186
Anthony Nash
  • 834
  • 1
  • 9
  • 26
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Note that ggplot plot objects are lazy, their arguments are not evaluated till they are `print()`-ed. All your objected are defined the exact same way `ggplot(df, aes(df$base)) + geom_bar()` and at the end of the loop, there will only be one `df` variable that ever existed and it will have the last value of the loop. – MrFlick Aug 02 '18 at 16:00
  • Related: https://stackoverflow.com/questions/36627085/for-loop-with-ggplots-produces-graphs-with-identical-values-but-different-headin – MrFlick Aug 02 '18 at 16:01
  • Related: https://stackoverflow.com/questions/49183067/trying-to-make-a-list-of-ggplot-objects-in-a-for-loop-all-items-in-list-are-wri?noredirect=1&lq=1 – MrFlick Aug 02 '18 at 16:03
  • Related: https://stackoverflow.com/questions/31993704/storing-ggplot-objects-in-a-list-from-within-loop-in-r – MrFlick Aug 02 '18 at 16:04

0 Answers0