insert vertical line based on the max value by group

1,061 views
Skip to first unread message

Jinyi Hung

unread,
Jun 25, 2013, 10:01:47 PM6/25/13
to ggp...@googlegroups.com

Hi,

I'm trying to draw an overlapped area plot with vertical lines showing the time (x) of the maximum value (y) in each group (two groups).

Right now I just manually draw the lines but I am trying to figure out if there is any function I can use to draw the line based on the max value of each group.  Chang's R graphics cookbook has example adding lines by mean value of each group as follow:

library(plyr) # For the ddply() function
hw_means <- ddply(heightweight, "sex", summarise, heightIn=mean(heightIn))

hw_means
sex heightIn
f 60.52613
m 62.06000

p + geom_hline(aes(yintercept=heightIn, colour=sex), data=hw_means,
linetype="dashed", size=1)


How do I replace the mean function with extracting max value function, if it's possible?


Thank you.
Jinyi

Ben Bond-Lamberty

unread,
Jun 26, 2013, 8:43:32 AM6/26/13
to ggplot2
This is a little confusing. If you just want the max, use

hw_maxes <- ddply(heightweight, "sex", summarise, heightIn=max(heightIn))

But if you want "the time (x) of the maximum value (y) in each group
(two groups)" then you'll probably want to use the which.max()
function. If you have a sample data set, I or someone else can give a
more concrete answer.

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

Jinyi Hung

unread,
Jun 26, 2013, 10:12:40 AM6/26/13
to Ben Bond-Lamberty, ggplot2

Hi Ben,

Attached is the data set and a graph I have right now.  The vertical lines were drawn based on the max values of each group.  I did this using geom_vline(xintercept=value) but I wonder there is other way to draw x intercept and correspond the lines to each group.

My original code is 
ggplot(KDTpDensity, aes(x=Time_ms, y=FixPro, fill=AOI)) + geom_area(alpha=.3, position="identity") +geom_vline(xintercept=1400) +geom_vline(xintercept=1250) 

To add lines, I was thinking - 
1) identify max value in each group
FixPro_max<-ddply(KDTpDensity, "AOI", summarize, FixPro=max(FixPro))

2) find the corresponding x axis value

3) plot the graph based on the obtained values in 2)

I hope this makes more sense.

Thank you,
Jinyi

2013/6/26 Ben Bond-Lamberty <bpb...@gmail.com>
KDTpDensity.txt
Graph.bmp

Ben Bond-Lamberty

unread,
Jun 26, 2013, 11:18:04 AM6/26/13
to Jinyi Hung, ggplot2
So how about something like this:

FixPro_max<-ddply(KDTpDensity, "AOI", .fun=function(d){
d[which.max(d$FixPro),"Time_ms"]}) # identify Time_ms corresponding
to max FixPro value

ggplot(KDTpDensity, aes(x=Time_ms, y=FixPro, fill=AOI)) +
geom_area(alpha=.3, position="identity")
+geom_vline(data=FixPro_max,aes(xintercept=V1,group=AOI))

Ben

Jinyi Hung

unread,
Jun 26, 2013, 4:33:17 PM6/26/13
to Ben Bond-Lamberty, ggplot2

This works very nicely.  I really appreciated.  
Reply all
Reply to author
Forward
0 new messages