ggplot2::geom_path() in a loop problems.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

ggplot2::geom_path() in a loop problems.

Martin Møller Skarbiniks Pedersen
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]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: ggplot2::geom_path() in a loop problems.

Martin Møller Skarbiniks Pedersen
On Fri, 23 Apr 2021 at 20:11, Martin Møller Skarbiniks Pedersen <
[hidden email]> wrote:

>
> 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


And the same strange effect if I write it like this:


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

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: ggplot2::geom_path() in a loop problems.

Martin Møller Skarbiniks Pedersen
In reply to this post by Martin Møller Skarbiniks Pedersen
On Fri, 23 Apr 2021 at 20:51, Chris Evans <[hidden email]> wrote:
>
> I may be quite wrong but isn't the for loop in the second example simply
overwriting/replacing the first p2 with the second whereas the p1 version
is adding the paths to p1.
>
> (If you see what I mean, I don't think I have expressed that very well.)
>
> Very best (all),
>
> Chris


Hi Chris,
  Thanks for your email. However I don't think that is the problem.
  Same problem here:

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

df <- data.frame(x = c(0,25,0,-25,0), y = c(25,0,-25,0,25))
p3 <- ggplot()
idx <- 1
p3 <- p3 + geom_path(data = df,aes(x = x/idx, y = y/idx))
idx <- 2
p3 <- p3 + geom_path(data = df,aes(x = x/idx, y = y/idx))
p3 <- p3 + xlim(-30,30)
p3 <- p3 + ylim(-30,30)
p3

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: ggplot2::geom_path() in a loop problems.

Jeff Newmiller
Sigh. Don't do this. Setup your data frame with all the data you want to plot and give it to one geom. Use group columns such as factors (e.g. color, size, linetype, etc) to distinguish them.

Using multiple geoms with different mappings is usually a recipe for disappointment. It also fails to create legible legends.

On April 24, 2021 2:21:01 PM PDT, "Martin Møller Skarbiniks Pedersen" <[hidden email]> wrote:

>On Fri, 23 Apr 2021 at 20:51, Chris Evans <[hidden email]> wrote:
>>
>> I may be quite wrong but isn't the for loop in the second example
>simply
>overwriting/replacing the first p2 with the second whereas the p1
>version
>is adding the paths to p1.
>>
>> (If you see what I mean, I don't think I have expressed that very
>well.)
>>
>> Very best (all),
>>
>> Chris
>
>
>Hi Chris,
>  Thanks for your email. However I don't think that is the problem.
>  Same problem here:
>
>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
>
>df <- data.frame(x = c(0,25,0,-25,0), y = c(25,0,-25,0,25))
>p3 <- ggplot()
>idx <- 1
>p3 <- p3 + geom_path(data = df,aes(x = x/idx, y = y/idx))
>idx <- 2
>p3 <- p3 + geom_path(data = df,aes(x = x/idx, y = y/idx))
>p3 <- p3 + xlim(-30,30)
>p3 <- p3 + ylim(-30,30)
>p3
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.