Display actual numerical median on boxplot

3,556 views
Skip to first unread message

Kim

unread,
Aug 13, 2010, 10:46:21 AM8/13/10
to ggplot2
Hi there,

I would like to display the actual numeric value of the median next to
the median line of the boxplots (or anywhere on the plot really, if
there is an easy way to do this). Could anyone help with this?

Here is my code for the boxplot that does not display the numeric
value of the median.
qplot(Calculator,score,data=ds,geom=c("boxplot"),fill=disease)

Thank you very much for your help; it is greatly appreciated.

Best wishes,

Kim

Brandon Hurr

unread,
Aug 13, 2010, 11:49:15 AM8/13/10
to Kim, ggplot2
Kim, 

Since I didn't have your dataset I used the diamonds one that comes with ggplot2. 

Here's what I came up with...


require(ggplot2)


median.s<-ddply(diamonds, .(cut), function(df) {
return(median(df$depth))
})

p<-ggplot(data=diamonds, aes(label=median.s$V1))
p1<-p+geom_boxplot(aes(x=cut, y=depth))

p2<-p1 + geom_text(data = median.s, aes(x=cut, y= V1, label = V1, hjust = -2, vjust = 0.5))
p2


I mangled it together so maybe someone can come up with something more elegant, but it seems to work. You can adjust the position with the justifications. I can't get them exactly in the right positions for my preferences. Again, someone else might know better. 

Brandon



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