problem with hline when used with stat_summary

686 views
Skip to first unread message

Elaine

unread,
Mar 31, 2010, 11:24:28 AM3/31/10
to ggplot2
I am getting some strange behavior with geom_hline when I use it with
stat_summary. I think this is a bug, but could also be a
misunderstanding on my part about how to use stat_summary.

I want to put a horizontal line on my plots that is 10% below one of
the datapoints. I'm going to do this over and over, so I want it to
be a stat_summary function rather than just calculating and plotting
the values. If it use geom_hline(yintercept=2) as a layer in my
ggplot call, everything is fine. However, when I attempt to use
geom_hline with stat_summary, it seems to expect to get values for y,
yend, etc. My understanding is that it should only need a value for
yintercept. Here is the code I would like to use:

tenBelow <- function(y) {
means <- mean(y, na.rm=TRUE)
data.frame(yintercept=means*.9)
}

tenPercLine <- function(...) {
return(stat_summary(fun.data=tenBelow, geom='hline', color='red',
linetype='dashed', ...))
}


Since I can't get this to work, I'm using segments instead with this
code:

tenBelow <- function(y) {
means <- mean(y, na.rm=TRUE)
data.frame(y=means*.9, yend=means*.9, x=0, xend=15)
}

tenPercLine <- function(...) {
return(stat_summary(fun.data=tenBelow, geom='segment', color='red',
linetype='dashed', ...))
}

This is frustrating because I have to hard code the x values (which is
inelegant), and it results in a line that does not flow off the edges
of my facets, which is undesireable. (I thought I could get around
this by using x=-Inf and x=Inf, but not so).

Elaine McVey
BD Technologies
Research Triangle Park, NC

hadley wickham

unread,
Mar 31, 2010, 1:19:05 PM3/31/10
to Elaine, ggplot2
Hi Elaine,

Unfortunately geom_vline is incredibly complicated and does not work
well with other statistics. Why not just perform the summary outside
of the plot?

Hadley

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2
>
> To unsubscribe, reply using "remove me" as the subject.
>

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages