[R] Tukey HSD plot with lines indicating (non-)significance

Karl Ove Hufthammer karl at huftis.org
Mon Jan 14 18:36:04 CET 2013

Dear list members,

I'm running some tests looking at differences between means for various 
levels of a factor, using Tukey's HSD method.

I would like to plot the data as boxplots or dotplots, with horizontal 
significance lines indicating which groups are statistically 
significantly different, according to Tukey HSD. Here's a nice image 
showing an example of such a graphical display:


Is there a R function for this? I have tried searching, and found 
several nice plotting fuctions for Tukey's HSD, but they all either hide 
the actual data, or display the results only with letters indicating 


# Fit an one-way ANOVA.
l=aov(ShootDryMass ~ trt, data=rice)

# Calculate the Tukey HSD tests and CIs.
# The plot shows all 6*5/2 = 15 confidence intervals,
# but not the data or the actual group means.
l.hsd=TukeyHSD(l, "trt", ordered = TRUE)

# I rather like this one. It shows the group means in a nice way,
# but hides the data. And checking which groups are different
# isn't always easy (e.g., for NH4Cl - NH4NO3), as one
# has to compare differences to a non-aligned scale (bar).

# One function that gives a results closer to what I want,
# is the compact letter display (CLD) of the multcomp package.
# This shows boxplots (but it is easy to overlay the actual
# observations, using the points() function) and uses letters
# to indicate non-significance.
# (Note that (if I have understood everything correctly) this
# doesn't actually calculcate Tukey HSD values (the "Tukey"
# in the glht() call only selects Tukey *contrasts*), but
# the results should be *very* similar.)
l.glht=glht(l, linfct = mcp(trt = "Tukey"))
old.par <- par( mai=c(1,1,2,1))

# Basically, I want a similar display as the one above (or
# preferably a ggplot2- or lattice-based one), but with lines
# instead of letters. Of course, for this, the levels need to be
# reordered by group means, and it is only guaranteed to
# work for balanced data. Example, with the lines missing:
d$trt=reorder(d$trt, -d$ShootDryMass)
ggplot(d, aes(x=trt, y=ShootDryMass)) + geom_boxplot(outlier.colour=NA) 
   geom_jitter(col="red", size=3, position=position_jitter(width=.1))

Is there such a function available in R?

Karl Ove Hufthammer

More information about the R-help mailing list