[R] ggplot2::geom_path() in a loop problems.

Martin Møller Skarbiniks Pedersen tr@xp|@yer @end|ng |rom gm@||@com
Fri Apr 23 20:11:53 CEST 2021


Hi,

I have some problems understanding how to use geom_path() inside a loop.
I know the code below is a bit silly but it is just a MRE
( https://stackoverflow.com/help/minimal-reproducible-example )

p1 looks like I expect however p2 only contains the last square. I
expected p2 to be the same as p1.

Any hints what is going on?
Regards
Martin

library(ggplot2)

df <- data.frame(x = c(0,25,0,-25,0), y = c(25,0,-25,0,25))

p1 <- ggplot()
p1 <- p1 + geom_path(data = df,aes(x = x/1, y = y/1))
p1 <- p1 + geom_path(data = df,aes(x = x/2, y = y/2))
p1 <- p1 + xlim(-30,30)
p1 <- p1 + ylim(-30,30)
p1


p2 <- ggplot()
for (idx in 1:2) {
  p2 <- p2 + geom_path(data = df,aes(x = x/idx, y = y/idx))
}
p2 <- p2 + xlim(-30,30)
p2 <- p2 + ylim(-30,30)
p2

	[[alternative HTML version deleted]]



More information about the R-help mailing list