-1

I have a list of 12 dataframes:

[1] X2016_kvish_1_10t
[2] X2015_kvish_1_10t
[3] X2014_kvish_1_10t
[4] X2013_kvish_1_10t
[5] X2012_kvish_1_10t
[6] X2011_kvish_1_10t     
# with 6 more ...    

and I want to plot them with the multiplot function (ggplot2). for example, this is a single plot:

ggplot(data = X2015_kvish_1_10t) +
geom_line(mapping = aes(
x = date, y = X2015_kvish_1_10t$nefah), colour = "blue") + 
ylab("Traffic Counts (quantity)")+ ggtitle("Traffic Counts")+
geom_point(mapping = aes(
x = date, y = X2015_kvish_1_10t$day_mean , color = "blue"))

how can I plot them together so I can see the 12 at the same time and I won't need to write the same functions 12 times ?

Michael Spector
  • 113
  • 1
  • 7
  • You could plot them separately and then plot all of them on the same window using the 'grid.arrange' function from gridExtra – P.R Jul 12 '17 at 22:49
  • Possible duplicate of [Side-by-side plots with ggplot2](https://stackoverflow.com/questions/1249548/side-by-side-plots-with-ggplot2) – S Rivero Jul 12 '17 at 22:49
  • but I'm looking for a way that I won't need to repeat 12 times. I tried making a list of the dataframes and then use a loop. but it seem to be that ggplot2 doesn't work with list – Michael Spector Jul 12 '17 at 22:51
  • 1
    Then, formulate your question properly. – S Rivero Jul 12 '17 at 22:57

1 Answers1

2

if p is your plot, and ld the list of data.frames, you could do:

grid.arrange(grobs = lapply(ld, "%+%", e1 = p))

(untested, for lack of reprex)

But note that you should never have $ inside aes(). And, of course, facetting is probably the better option.

baptiste
  • 75,767
  • 19
  • 198
  • 294