3

I'm having trouble generating a compact letter display for my results. I've run an ANOVA followed by Tukey's HSD to generate the p values for each pair, but I do not know how (or if it is possible?) to assign letters to these p values to show which pairs are significant from each other.

csa.anova<-aov(rate~temp*light,data=csa.per.chl) summary(csa.anova) TukeyHSD(csa.anova)

This runs the tests I need, but I don't know how to assign letters to each p value to show which pairs are significant.

Amy
  • 75
  • 1
  • 1
  • 4
  • 7
    Your example is not reproducible, please edit the question with the output of `dput(head(csa.per.chl, 20))`. What are the letters you want to assign to what ranges of p-values? Say, `A: 0.00-0.05`, etc? What, exactly? Edit the question with that information, please. – Rui Barradas Aug 31 '19 at 12:05
  • 1
    What do you mean by "assign letters"? For others looking at this, here is an example using mtcars: `m <- mtcars m$cyl <- as.factor(m$cyl); m$gear <- as.factor(m$gear) m.anova<-aov(mpg~cyl*gear,data=m) summary(m.anova) TukeyHSD(m.anova)` – Monk Aug 31 '19 at 13:12
  • A CLD is a misleading graphic because it shows what comparisons were NOT found significant. So my answer is "just don't" and instead show a matrix of P values, or confidence intervals for differences of measure, or some other graphic that is not misleading. – Russ Lenth Jun 03 '21 at 17:49

2 Answers2

5

Find more details here.

mod <- lm(Sepal.Width ~ Species, data = iris)

mod_means_contr <- emmeans::emmeans(object = mod,
                                    pairwise ~ "Species",
                                    adjust = "tukey")

mod_means <- multcomp::cld(object = mod_means_contr$emmeans,
                           Letters = letters)

library(ggplot2)

ggplot(data = mod_means,
       aes(x = Species, y = emmean)) +
  geom_errorbar(aes(ymin = lower.CL, 
                    ymax = upper.CL), 
                width = 0.2) +
  geom_point() +
  geom_text(aes(label = gsub(" ", "", .group)),
            position = position_nudge(x = 0.2)) +
  labs(caption = "Means followed by a common letter are\nnot significantly different according to the Tukey-test")

Created on 2021-06-03 by the reprex package (v2.0.0)

Paul Schmidt
  • 1,072
  • 10
  • 23
2

You need to install the multcomp package first. It can compute the Tukey HSD Test and returns an object that has summary and plot methods. The package also has a function (cld) to print the "compact letter display." As an example we can use the iris data set that comes with R:

library(multcomp)
data(iris)
iris.aov <- aov(Petal.Length~Species, iris)
iris.tukey <- glht(iris.aov, linfct=mcp(Species="Tukey"))
cld(iris.tukey)
#     setosa versicolor  virginica 
#        "a"        "b"        "c" 
dcarlson
  • 10,936
  • 2
  • 15
  • 18