Adding the median-line

11 views
Skip to first unread message

Patrick Hausmann

unread,
Oct 23, 2010, 8:01:45 AM10/23/10
to ggplot2
Dear list,

I would like to add the median for each "bl"-group per year...

library(ggplot2)

set.seed(234)
kr <- rep(paste(seq(1:3), seq(9:1), sep=""), 3)
jahr <- as.factor(rep(2002:2004, each=9))
v1 <- rep(100 * seq(0.1, 0.9, by=0.1), 3) * runif(27)
df <- data.frame(bl = substr(kr,1,1), kr, jahr, v1)

m <- ggplot(df, aes(x=jahr, y=v1, group=kr))
m <- m + geom_line() + facet_wrap(~ bl)
m

dfmed <- ddply(df, .(bl, jahr), function(x) median(x$v1))

## This makes no sense...
m <- m + geom_line(data=dfmed, color="red")

Many thanks in advance for any help.
Patrick

takahashi kohske

unread,
Oct 23, 2010, 8:14:30 AM10/23/10
to Patrick Hausmann, ggplot2
hi, there are two points.
dfmed has V1, not v1, column.
group should be correctly specified.

so

m + geom_line(data=dfmed, color="red", aes(group=1,y=V1))

or

dfmed <- ddply(df, .(bl, jahr), function(x) c(v1=median(x$v1)))
m + geom_line(data=dfmed, color="red", aes(group=1))

will do that.

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

Reply all
Reply to author
Forward
0 new messages