I want to add to an existing ggplot in a loop. It works fine as shown in a minimal example below when I add points to a list not using a loop (left plot). When I do the same in a loop, the final result only contains the last point that I added (right plot).
library(ggplot2)
p <- list(); pl <- list()
x0 <- c(-1,1); y0 <- c(-3,3); q <- c(-5,5)
#no loop
p[[1]] <- ggplot() + coord_fixed(xlim = q, ylim = q)
p[[2]] <- p[[1]] +geom_point(aes(x=x0[1], y=y0[1]))
p[[3]] <- p[[2]] + geom_point(aes(x=x0[2], y=y0[2]))
#loop
pl[[1]] <- ggplot() + coord_fixed(xlim = q, ylim = q)
for (i in 1:2)
{
pl[[i+1]] <- pl[[i]] + geom_point(aes(x=x0[i], y=y0[i]))
}
p[[3]]
pl[[3]]