Ferga Hamilton
Ferga Hamilton

Reputation: 1

Changing order of letters of significance in TUKEY test

I have created a boxplot using ggplot from the results of my tukey test. I have added the letters of significance above my boxes but the letters are not in order. I wish for my first sample to be "a" and then have "b" and then "c". I used the following code;

value_max = 
 Rosettes %>% 
 group_by(Genotype) %>% 
 summarize(max_value = max(X0.5xMS))

hsd=HSD.test(aov(X0.5xMS~Genotype, data=Rosettes), trt = "Genotype", group = T)

sig.letters <- hsd$groups[order(row.names(hsd$groups)), ]

p <- ggplot(data = Rosettes, aes(x = Genotype, y = X0.5xMS)) +
  geom_boxplot(aes(fill = Genotype,)) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+ theme(axis.text.x = element_text(angle = 90)) +
  geom_text(data = value_max, aes(x=Genotype, y = 0.1 + max_value, label = sig.letters$groups), vjust=0)+
  stat_boxplot(geom = 'errorbar', width = 0.1)+
  ggtitle("Rosette Tukey Results \n 0.5xMS") + theme(plot.title = element_text(hjust=0.5))+
  xlab("Genotype") + ylab("Rosette Area (cm2)"); p

This code has given me the desired graph, the order of the letters is my only issue. If anyone could help, I would be very grateful.

Upvotes: 0

Views: 1132

Answers (1)

Paul Schmidt
Paul Schmidt

Reputation: 1219

Does this help? I know it is not your data and not your functions (aov and hsd.test) but it does what you want, I believe. (Please find more about the compact letter display here.)

library(emmeans)
library(multcomp)
library(multcompView)

# set up model
model <- lm(weight ~ group, data = PlantGrowth)

# get (adjusted) weight means per group
model_means <- emmeans(object = model,
                       specs = "group")

# add letters to each mean
model_means_cld <- cld(object = model_means,
                       adjust = "Tukey",
                       Letters = letters,
                       reversed = TRUE, # <---- this one here!
                       alpha = 0.05)

# show output
model_means_cld
#>  group emmean    SE df lower.CL upper.CL .group
#>  trt2    5.53 0.197 27     5.02     6.03  a    
#>  ctrl    5.03 0.197 27     4.53     5.53  ab   
#>  trt1    4.66 0.197 27     4.16     5.16   b   
#> 
#> Confidence level used: 0.95 
#> Conf-level adjustment: sidak method for 3 estimates 
#> P value adjustment: tukey method for comparing a family of 3 estimates 
#> significance level used: alpha = 0.05 

Created on 2021-10-18 by the reprex package (v2.0.1)

Upvotes: 1

Related Questions