2

After a Tukey test using rstatix, I would like to add another column to the data frame with the Tukey groups.

library(tidyverse)
library(rstatix)

ggplot(data = iris,
       aes(x = Species,
           y = Petal.Length)) +
  geom_boxplot()

Tukey <- iris %>%
  tukey_hsd(Petal.Length ~ Species) %>%
  add_significance() %>%
  # ????? <------- Help here

I'm expecting something like this:

> Tukey
# A tibble: 3 x 9
term    group1     group2     null.value estimate conf.low conf.high p.adj p.adj.signif group
* <chr>   <chr>      <chr>           <dbl>    <dbl>    <dbl>     <dbl> <dbl> <chr>      <chr>       
  1 Species setosa     versicolor          0     2.80     2.59      3.00 3e-15 ****     A       
  2 Species setosa     virginica           0     4.09     3.89      4.29 3e-15 ****     B   
  3 Species versicolor virginica           0     1.29     1.09      1.50 3e-15 ****     C

Extra: If it is possible, please help me to put the letters of the groups over each box of the boxplot.

enter image description here

Daniel Valencia C.
  • 2,159
  • 2
  • 19
  • 38

2 Answers2

0

I was not able to use the tukey_hsd function, but I think this is a reseanable solution for this using other packages, hope it works for you. ;) This answer is based on this one.

library(multcompView)    

# Calculate anova
iris_anova = aov(Petal.Length ~ Species, data = iris)

# Calculate tukey values, similar to tukey_hsd but not exactly the same!
iris_tukey = TukeyHSD(iris_anova)

# make the letter magic !!
cld = multcompLetters4(iris_anova, iris_tukey)

# table with factors and 3rd quantile
iris_tk = iris %>%
    group_by(Species) %>%
    summarise(mean=mean(Petal.Length, na.rm=T), quant = quantile(Petal.Length, probs = 0.75, na.rm=T)) %>%
    arrange(desc(mean))

# extracting the compact letter display and adding to the Tk table
cld = as.data.frame.list(cld$Species)
iris_tk['cld'] = cld$Letters

# boxplot with the letters!!
ggplot(iris, aes(Species, Petal.Length)) + 
    geom_boxplot(aes(x=Species, Petal.Length ),show.legend = F)+
    theme_bw() + #some theme fluff
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + #some more thematic fluff for cuteness
    geom_text(data = iris_tk, aes(x = Species, y = quant, label = cld), size = 3, vjust=-1, hjust =-4) #that is the bit relevant here! note that vjust and hjust might need adjustments

The code above should result in the boxplot: Box-plot with Tukey letters!

PS.: I'm so glad I finally was able to answer something here. Thanks stackoverflow people :P

Jonathan
  • 133
  • 1
  • 15
-1
Tukey <- iris %>%
  tukey_hsd(Petal.Length ~ Species) %>%
  add_significance() %>% add_column(Group=c("A","B","C"))
  • 2
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Dec 26 '21 at 09:09