scatter plot with segment

50 views
Skip to first unread message

Yue Li

unread,
Aug 6, 2015, 4:53:37 PM8/6/15
to ggplot2
I have a scatter plot like this:
p<-ggplot(df, aes(x=x,y=y, group = Year, colour=Year, shape =Year)) + geom_point()+ ...

The Year variable i use has four levels: Yr 1, 2, 3, 4. 

What i try to do is to show changes across years by adding arrow segments pointing from Yr 1 to Yr 2, from Y2 to Yr 3, and from Yr 3 to Yr 4. So i used something like this to connect points Yr1 and Yr2:

  geom_segment(aes(x=x1,y=y1,xend=x2,yend=y2), color = "grey20", arrow=arrow(length=unit(.1,"cm")))+
  geom_segment(aes(x=x2,y=y2,xend=x3,yend=y3), color = "grey20", arrow=arrow(length=unit(.1,"cm")))+
  geom_segment(aes(x=x3,y=y3,xend=x4,yend=y4), color = "grey20", arrow=arrow(length=unit(.1,"cm")))

The problem is that i have many missing points. So if a person only has data in Yr1 and Yr4, I still want to connect point Yr 1 with Yr 4, but don't know how. 

Is there a way to fix this? 

Many thanks!

Yue

Hadley Wickham

unread,
Aug 6, 2015, 6:23:06 PM8/6/15
to Yue Li, ggplot2
You will find this problem much much easier if you have tidy data:
http://vita.had.co.nz/papers/tidy-data.html

Hadley
> --
> --
> 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/d/optout.



--
http://had.co.nz/
Reply all
Reply to author
Forward
0 new messages