adding horizontal lines at medians

11 views
Skip to first unread message

Gary Collins

unread,
Sep 9, 2009, 3:54:21 AM9/9/09
to ggp...@googlegroups.com
I'm trying to do a simple dotplot of scores at 4 time points and I want
to add a little horizontal line at the median score at each time point.
My current hack is

total.score <- data.frame(total.score = rlnorm(200, 1, 0.75))
total.score$time <- rep(1:4, c(50, 50, 50, 50))

m <- ggplot(total.score, aes(x = time, y = total.score))
m + geom_jitter(position = position_jitter(width = 0.15)) +
xlab("time point") +
ylab("total score") +
stat_summary(fun.y = median, col = "red", pch = "-", size = 20, geom =
"point", alpha = 0.5) +
scale_x_continuous(breaks = c(1, 2, 3, 4))

but it is a hack and I'm sure there is a better solution and something
more obvious which I'm missing. Instead of the chunky bar, I'd prefer
something more subtle (possibly lwd=2 or even lwd=1) but with width the
same as the jitter width.

thanks for any suggestions.

Gary

Learning R Blog

unread,
Sep 9, 2009, 4:45:07 AM9/9/09
to ggplot2
Try this:

m + geom_jitter(position = position_jitter(width = 0.15)) +
stat_summary(fun.ymin = median, fun.ymax = median, col =
"red", geom = "errorbar", width = 0.3) +
scale_x_continuous(breaks = c(1, 2, 3, 4)) +xlab("time point")
+ ylab("total score")

--
http://learnr.wordpress.com
Reply all
Reply to author
Forward
0 new messages