Problem : I would like to learn how to change the order of samples for which the Tukey test in R calculates the means and assigns the corresponding letters. The following is a simple example.
I played with aperture data and found that there are differences in Sepal.Length among different species. Here is the box:

I ran an ANOVA test and found that the differences were statistically significant.
> fit <- lm(Sepal.Length ~ Species, data = iris)
> summary(aov(fit))
Df Sum Sq Mean Sq F value Pr(>F)
Species 2 63.21 31.606 119.3 <2e-16 ***
Residuals 147 38.96 0.265
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Then I did a Tukey test and got the following:
> library(agricolae)
> HSD.test(fit, "Species", group=T, console=T)
Study: fit ~ "Species"
HSD Test for Sepal.Length
Mean Square Error: 0.2650082
Species, means
Sepal.Length std r Min Max
setosa 5.006 0.3524897 50 4.3 5.8
versicolor 5.936 0.5161711 50 4.9 7.0
virginica 6.588 0.6358796 50 4.9 7.9
alpha: 0.05 ; Df Error: 147
Critical Value of Studentized Range: 3.348424
Honestly Significant Difference: 0.2437727
Means with the same letter are not significantly different.
Groups, Treatments and means
a virginica 6.588
b versicolor 5.936
c setosa 5.006
According to the group table, the HSD.test function sorts the funds in descending order, and then assigns the letters. Thus, "virginica" has the highest average value, so it is the first in the table.
:
?
, .
:
a setosa 5.006
b versicolor 5.936
c virginica 6.588
. multcomp , :
1 - glht Tukey
> an <- aov(fit)
> library(multcomp)
> glht(an, linfct = mcp(Species = "Tukey"))
General Linear Hypotheses
Multiple Comparisons of Means: Tukey Contrasts
Linear Hypotheses:
Estimate
versicolor - setosa == 0 0.930
virginica - setosa == 0 1.582
virginica - versicolor == 0 0.652
2 - cld , Species iris$Species
> cld(glht(an, linfct = mcp(Species = "Tukey")))
setosa versicolor virginica
"a" "b" "c"
, glht , (, std, p-). , HSD.test, cld. HSD.test .