My question is an exact duplicate of this one [1], but the provided answer isn't working on my system.
Question: How can I add geom_segment() layers within a for loop? If I use aes, as the OP of the referenced question does, I also get only the last layer. If, as the answerer suggests, I don't use aes, I don't get any segments at all.
Here is a minimal example:
ga <- 2.39996322972865332
p <- ggplot()
p <- p + scale_y_continuous(limits = c(-1, 1))
p <- p + scale_x_continuous(limits = c(-1, 1))
for (i in 0:2) {
# p <- p + geom_segment(x = -cos(i*ga), y = -sin(i*ga), xend = cos(i*ga), yend = sin(i*ga)) # No segments
p <- p + geom_segment(aes(x = -cos(i*ga), y = -sin(i*ga), xend = cos(i*ga), yend = sin(i*ga))) # Only last segment
}
p
I'm using R version 3.1.2 and (apparently) ggplot2 version 1.0.0 on Ubuntu.
(Note: I get the same result if I use repeat
or while
.)
[1] Enriching a ggplot2 plot with multiple geom_segment in a loop?