Have geom_line() follow the order of appearance in the dataframe not the X axis values.

3,285 views
Skip to first unread message

Roey Angel

unread,
Apr 15, 2013, 10:21:41 AM4/15/13
to ggp...@googlegroups.com
geom_line() is plotted by default according the order of the data points on the x-axis.
Is there a way to draw geom_line of Y vs X but have the order of connection according to the order in the data frame (or according to another column in the data frame)?

Thanks in advance,
Roey

Ben Bolker

unread,
Apr 15, 2013, 10:42:17 AM4/15/13
to ggp...@googlegroups.com
Are you looking for geom_path ?

>
> Thanks in advance,
> Roey
>
> --
> --
> You received this message because you are subscribed to the ggplot2
> mailing list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google
> Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Ito, Kaori (Groton)

unread,
Apr 15, 2013, 10:43:12 AM4/15/13
to Roey Angel, ggp...@googlegroups.com

Dear Roey,

You can use geom_path() . See the example below. I think you need to sort the data if you want the order according to another column in the data frame.

 

#--toy example

d1 <- data.frame(z1=1:10, z2=c(1,10,2,9,3,8,4,6,5,7)

        , x=c(0,2,8,20,16,8,4,2,1,0.5), y=1:10)

 

#--plot is based on the order of x

ggplot(d1, aes(x,y)) + geom_line()

 

#--plot is based on the order of the data (for this case)

ggplot(d1, aes(x,y)) + geom_path()

 

#--change the order of the data by z2

d2 <- d1[order(d1$z2),]

ggplot(d2, aes(x,y)) +  geom_path()

--

Roey Angel

unread,
Apr 15, 2013, 10:50:54 AM4/15/13
to ggp...@googlegroups.com, Roey Angel
Thanks Ben and Kaori,
Totally forgot about geom_path()
Reply all
Reply to author
Forward
0 new messages