Using ggplot2 geom_path() in a grouped variable

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Using ggplot2 geom_path() in a grouped variable

Norberto Hernandez
Hi! I am trying to make a scatter/path graph in one variable that is
divided in two groups (variable Control), but when I use the
geom_path() option, the line continues from the group one to the group
two, and I wasn't able to avoid it.I need the path draws over the
group one and then draws over the group two avoiding the connection
between the last value of group one and the first value of group two.

This is my code

library(ggplot2)
plot <- ggplot(data, aes(Pretrend, Outcome)) + geom_point()
plot + geom_point(aes(colour=factor(Control)))+labs(x="Date",
y="Cases", title="Intoxication Cases")+geom_path()

Could you please bring be some advice.
Regards
Norberto Francisco Hernández
______________________________________________
[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: Using ggplot2 geom_path() in a grouped variable

David Winsemius


> On Feb 12, 2019, at 2:09 PM, Norberto Hernandez <[hidden email]> wrote:
>
> Hi! I am trying to make a scatter/path graph in one variable that is
> divided in two groups (variable Control), but when I use the
> geom_path() option, the line continues from the group one to the group
> two, and I wasn't able to avoid it.I need the path draws over the
> group one and then draws over the group two avoiding the connection
> between the last value of group one and the first value of group two.
>
> This is my code
>
> library(ggplot2)
> plot <- ggplot(data, aes(Pretrend, Outcome)) + geom_point()
> plot + geom_point(aes(colour=factor(Control)))+labs(x="Date",
> y="Cases", title="Intoxication Cases")+geom_path()
>
> Could you please bring be some advice.

The first bit of advice would be to include the unsuccessful code that used geom_path. Otherwise we hav no way of knowing what you gave as the grouping argument. The second bit of advice would be to include a dataset that illustrates the problem. (These are both bits of advice that you should have found in the Posting Guide.

--
David.

> Regards
> Norberto Francisco Hernández
> ______________________________________________
> [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.

______________________________________________
[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: Using ggplot2 geom_path() in a grouped variable

Rui Barradas
In reply to this post by Norberto Hernandez
Hello,

I am not understanding the problem.
With a made up dataset everything seems right.

set.seed(1234)    # make the results reproducible
n <- 10
data <- data.frame(Pretrend = rep(1:10, 2),
                    Outcome = 1:10 + rnorm(2*n),
                    Control = rep(1:2, each = n))


library(ggplot2)

p <- ggplot(data, aes(Pretrend, Outcome, colour = factor(Control))) +
   geom_point() +
   geom_path() +
   labs(x = "Date", y = "Cases", title = "Intoxication Cases")

p


I have changed the plot name because 'plot' is the name of a base R
function.

What is the problem with the graph above? Is that what you were looking
for? (Or similar.)


Hope this helps,

Rui Barradas



Às 20:09 de 12/02/2019, Norberto Hernandez escreveu:

> Hi! I am trying to make a scatter/path graph in one variable that is
> divided in two groups (variable Control), but when I use the
> geom_path() option, the line continues from the group one to the group
> two, and I wasn't able to avoid it.I need the path draws over the
> group one and then draws over the group two avoiding the connection
> between the last value of group one and the first value of group two.
>
> This is my code
>
> library(ggplot2)
> plot <- ggplot(data, aes(Pretrend, Outcome)) + geom_point()
> plot + geom_point(aes(colour=factor(Control)))+labs(x="Date",
> y="Cases", title="Intoxication Cases")+geom_path()
>
> Could you please bring be some advice.
> Regards
> Norberto Francisco Hernández
> ______________________________________________
> [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.
>

______________________________________________
[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: Using ggplot2 geom_path() in a grouped variable

David Winsemius
To Norberto;

Your code _probably_ would have succeeded if you had used color as a grouping argument, or you could have used group or linetype or probably others, but I find locating the listing of ggplot2 "behavioral" parameters rather frustrating. These details are not to be found in any of ?aes , ?ggplot , or ?geom_path. I was able to find those alternate parameters illustrated in the Data Visualization cheatsheet: https://github.com/rstudio/cheatsheets/blob/master/data-visualization-2.1.pdf

   ... + +geom_path(aes(color=factor(Control)))

However, one never knows about error semantics unless one has the data-object.

--
David

> On Feb 12, 2019, at 4:22 PM, Rui Barradas <[hidden email]> wrote:
>
> Hello,
>
> I am not understanding the problem.
> With a made up dataset everything seems right.
>
> set.seed(1234)    # make the results reproducible
> n <- 10
> data <- data.frame(Pretrend = rep(1:10, 2),
>                   Outcome = 1:10 + rnorm(2*n),
>                   Control = rep(1:2, each = n))
>
>
> library(ggplot2)
>
> p <- ggplot(data, aes(Pretrend, Outcome, colour = factor(Control))) +
>  geom_point() +
>  geom_path() +
>  labs(x = "Date", y = "Cases", title = "Intoxication Cases")
>
> p
>
>
> I have changed the plot name because 'plot' is the name of a base R function.
>
> What is the problem with the graph above? Is that what you were looking for? (Or similar.)
>
>
> Hope this helps,
>
> Rui Barradas
>
>
>
> Às 20:09 de 12/02/2019, Norberto Hernandez escreveu:
>> Hi! I am trying to make a scatter/path graph in one variable that is
>> divided in two groups (variable Control), but when I use the
>> geom_path() option, the line continues from the group one to the group
>> two, and I wasn't able to avoid it.I need the path draws over the
>> group one and then draws over the group two avoiding the connection
>> between the last value of group one and the first value of group two.
>> This is my code
>> library(ggplot2)
>> plot <- ggplot(data, aes(Pretrend, Outcome)) + geom_point()
>> plot + geom_point(aes(colour=factor(Control)))+labs(x="Date",
>> y="Cases", title="Intoxication Cases")+geom_path()
>> Could you please bring be some advice.
>> Regards
>> Norberto Francisco Hernández
>> ______________________________________________
>> [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.
>>
>
> ______________________________________________
> [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.

______________________________________________
[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: Using ggplot2 geom_path() in a grouped variable

Norberto Hernandez
In reply to this post by Rui Barradas
Rui:

Thank you very much for your help! It works the way you do. My problem
was with the time I declare the control group:

My code:
p <- ggplot(data, aes(Pretrend, Outcome))
+ geom_point(aes(colour=factor(Control))

Your solution:
p <- ggplot(data, aes(Pretrend, Outcome, colour = factor(Control)))
+ geom_point()

I believe that the time you are declaring the control group (inside
the ggplot() function instead the geom_point()) makes your code do the
right way and that my code links the last observation of group zero
with the first observation of group one.

Thank you Rui, and thanks all of you guys for take the time to read
and answer my post.

Regards
Norberto

El mar., 12 feb. 2019 a las 16:22, Rui Barradas
(<[hidden email]>) escribió:

>
> Hello,
>
> I am not understanding the problem.
> With a made up dataset everything seems right.
>
> set.seed(1234)    # make the results reproducible
> n <- 10
> data <- data.frame(Pretrend = rep(1:10, 2),
>                     Outcome = 1:10 + rnorm(2*n),
>                     Control = rep(1:2, each = n))
>
>
> library(ggplot2)
>
> p <- ggplot(data, aes(Pretrend, Outcome, colour = factor(Control))) +
>    geom_point() +
>    geom_path() +
>    labs(x = "Date", y = "Cases", title = "Intoxication Cases")
>
> p
>
>
> I have changed the plot name because 'plot' is the name of a base R
> function.
>
> What is the problem with the graph above? Is that what you were looking
> for? (Or similar.)
>
>
> Hope this helps,
>
> Rui Barradas
>
>
>
> Às 20:09 de 12/02/2019, Norberto Hernandez escreveu:
> > Hi! I am trying to make a scatter/path graph in one variable that is
> > divided in two groups (variable Control), but when I use the
> > geom_path() option, the line continues from the group one to the group
> > two, and I wasn't able to avoid it.I need the path draws over the
> > group one and then draws over the group two avoiding the connection
> > between the last value of group one and the first value of group two.
> >
> > This is my code
> >
> > library(ggplot2)
> > plot <- ggplot(data, aes(Pretrend, Outcome)) + geom_point()
> > plot + geom_point(aes(colour=factor(Control)))+labs(x="Date",
> > y="Cases", title="Intoxication Cases")+geom_path()
> >
> > Could you please bring be some advice.
> > Regards
> > Norberto Francisco Hernández
> > ______________________________________________
> > [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.
> >

______________________________________________
[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.