This loop creates a list of 3 ggplots, but because the arguments for x
and xend
depend on the index of the loop, dismay follows:
DF <- data.frame(column1=c(1,2,3,4,5), column2=c(4,5,6,7,8))
list_of_ggplots <- list()
for (num in seq(1:3)){
p <- ggplot()
p <- p + geom_segment(data=DF, aes(x=column1[num], xend=column2[num], y=1, yend=1))
list_of_ggplots[[num]] <- p }
list_of_ggplots
We get 3 plots being fundamentally the same plot (since at the point in time they are called, num
is 3).
What could be a better strategy to create these plots?