labels in dodged boxplot

23 views
Skip to first unread message

Adam Kiezun

unread,
Jul 7, 2010, 12:28:37 PM7/7/10
to ggplot2
How to add labels to boxplot if boxplot is dodged?
In the archive, I found a solution for faceted boxplots. But how to do
it for dodged boxplots?

#Solution for faceted boxplots
dummy <- data.frame(values = rnorm(30), fac1 = factor(rep(rep(1:3,
each
= 5), 2)), fac2 = factor(rep(1:2, each = 15) ))
p <- ggplot(dummy, aes(fac1, values)) + geom_boxplot() + facet_wrap(~
fac2)

# add letters for post-hoc Tukey results above upper whiskers
lettpos <- function(dummy) boxplot(dummy$values)$stats[5,]
test <- ddply(dummy, .(fac2, fac1), lettpos)
test$lett <- rep("a", nrow(test))
colnames(test)[3] <- "upper"
p + geom_text(aes(fac1, upper + 0.05, label = lett), data = test,
vjust
= 0)

What to do if the plot is created using dodging instead of faceting:
p <- ggplot(dummy, aes(fac1, values)) +
geom_boxplot(aes(fill=factor(fac2)))

./adam

Hadley Wickham

unread,
Jul 7, 2010, 2:01:58 PM7/7/10
to Adam Kiezun, ggplot2
Try this:

ggplot(dummy, aes(fac1, values)) +
geom_boxplot(aes(fill=factor(fac2))) +
geom_text(aes(fac1, upper + 0.05, label = lett), data = test, vjust = 0,
position = position_dodge(width = 0.8))

You'll need ggplot 0.8.8

Hadley

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

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

Reply all
Reply to author
Forward
0 new messages