adding text labels to boxplot + facet_wrap

1,881 views
Skip to first unread message

Etienne Laliberté

unread,
Mar 30, 2010, 8:33:39 PM3/30/10
to ggp...@googlegroups.com
I want to add text labels (a, b, c, etc; i.e. results of post hoc Tukey)
above 75% quantiles in boxplots with facet_wrap. How do I do this?
Example code:

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

Many thanks

Etienne

Etienne Laliberté

unread,
Mar 30, 2010, 10:29:23 PM3/30/10
to ggp...@googlegroups.com
After much fiddling around I finally found a solution (I actually wanted
the letters just above the upper whiskers, I got it wrong in my last
message). But is there a simpler way to do this?

# previous code


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

# solution: 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)


--
Etienne Laliberté
================================
School of Forestry
University of Canterbury
Private Bag 4800
Christchurch 8140, New Zealand
Phone: +64 3 366 7001 ext. 8365
Fax: +64 3 364 2124
www.elaliberte.info

Brian Diggs

unread,
Mar 31, 2010, 11:03:45 AM3/31/10
to ggplot2
You have the right approach. The positions for the text have to be
pre-computed, as you have done in test. The only way I would simplify
it is by adding a plot=FALSE argument to the boxplot call in lettpos
since you are only interested in the return values and not the plot
itself anyway.

--Brian Diggs

On Mar 30, 7:29 pm, Etienne Laliberté <etiennelalibe...@gmail.com>
wrote:

elaliberte

unread,
Mar 31, 2010, 5:02:21 PM3/31/10
to ggplot2
Thanks Brian. I actually noticed this just after posting the message,
and so I added plot = F.

Cheers

Etienne

patco...@yahoo.com.br

unread,
Apr 4, 2014, 11:53:32 AM4/4/14
to ggp...@googlegroups.com, etiennel...@gmail.com
It worked, but all the letters are repeated. So, I take this opportunity to know how we should do in the event of a significant difference between groups? How to put different letters?
 
Best regards,
Patricia
 
Biologist, UNICAMP
 

patco...@yahoo.com.br

unread,
Apr 4, 2014, 12:02:37 PM4/4/14
to ggp...@googlegroups.com, etiennel...@gmail.com

Dear Etienne,
It worked, but all the letters are repeated. I take the opportunity to know how we should do in the event of a significant difference between groups? How to put different letters?
Best regards
 
Patricia
Biologist, UNICAMP
Reply all
Reply to author
Forward
0 new messages