1

I have a loop that creates multiple plots with ggplot. I would like to create a new object with each plot in the same loop, but I can't find a way. This is an example code:

for(i in 1:5){
   ggplot(df[location==i], aes(x = date, y = value, group = 1)) +
   geom_line()
}

And I'm trying to do something like this

for(i in 1:5){
   plot_i -> ggplot(df[location==i], aes(x = date, y = value, group = 1)) +
   geom_line()
}

To get five objects, plot_1, plot_2, plot_3, and plot_5, in order to be able to call them by the object name later. But putting the i from the loop in the object name does not work, it only creates one object: plot_i, which ends up being the last of the sequence (i=5)

I would be very thankful for any help.

0 Answers0