I have a question about creating ggplot2 graphs in a for loop, renaming them based on the iteration and then arranging the graphs in a grid.
I want to do something like this dummy example
library(ggplot2)
a = c(1, 2, 3)
b = c(4, 5, 6)
for ( i in c(1:5)){
x = i*a
y = i*b
p = qplot(x, y)
... do something to rename p as plot_i...
}
... do something to arranage plots plot_1
... plot_6
into a 2 x 3 grid
Any suggestions?
You could save the plots into a list:
An alternative way of approaching this problem is to use use facets in your plot:
The cowplot library has the
plot_grid
function that will do this nicely: