Add significance letters to compare treatments for each variety in boxplot with Tukey test

Hi everyone,

It's been three days that I have been searching for something and I don't find any answer. I hope I will be clear with my problem.

My datasets is like on the image :
I measured the weight ("biomasse") of my plants, that is my responsive variable that I want to explain by the treatments.

I have 4 varieties, each treated with the same 4 treatments and what I want to do is to do a Tukey test to compare the different effects of the treatments on the weight of my plant for each varieties separately and then add the letters on a boxplot with facet_grid (by variety).

I did a Tukey test but what I see is that it's done on all the treatments for all the varieties in the same time, which is not what I want to explain.

The code

library(ggplot2)
library(multcompView)
library(dplyr)
library(graphics)

model3 = aov(Biomasse ~ Traitement+Variété+ Traitement*Variété, data=biomasseorgemock)

#table with factors, means and standard deviation 
data_summ = group_by(biomasseorgemock, Variété, Traitement) %>% 
  summarise(mean=mean(Biomasse), sd=sd(Biomasse)) %>% 
  arrange(desc(mean))
#View(data_sum)

#tukey test

Tukey= TukeyHSD(model3)
print(Tukey)

Cld=multcompLetters4(model3, Tukey)
print(Cld)


#add letter to table
Cld= as.data.frame.list(Cld$`Traitement:Variété`)
data_summ$Cld=Cld$Letters
print(data_summ)
View(data_summ)
#write.csv(data_sum, "massracinoc_summary.csv")


qq=ggplot(biomasseorgemock, aes(x=Traitement, y=Biomasse, fill = Traitement)) +
  geom_boxplot(outlier.shape = NA) +
  geom_text(data = data_summ, aes(label=Cld,x=Traitement, y=mean +sd), position=position_dodge2(0.75), vjust = -6) + ylim(0,0.6) +
  labs(x="Traitements", y = "Biomasses aériennes (g)") + facet_grid(.~Variété)
qq

I obtain this new dataframe with all the letters but it's not grouped by variety.

I tried to create subsets (on another script because I need to do that on 3 different datasets two answer different questions) of my datasets to select only one variety but I can't have the subset (maybe the dataframe is too short ??) it's written "0 observations of 4 variables".

It doesn't work well but at the end I would like to obtain this graph but with a, b, c letters to compare the four treatment on each variety.

I hope i'm clear enough and that I wrote correctly the post with the run and the images.

Thank you for your help and your answers

2 Likes

Check this discussion,

is the solution

1 Like

Hi,

Thank you for you answer.
Yes it works but the solution for me that I finally found with some help is to generate For() loops with vectors in which I stock my different variables to specifically do the Tukey test to one variety per variety and do the Tukey test inside the loop.

I have in my dataset two groups of data:
Inoc and mock, the same 4 varieties tested in the two groups and for each variety, the same 4 treatments like simply presented juste above and for everyone, I want to do the tukey test with meny outcomes (my different measures for each one) this and there is the code for this.

Inoc
var1: Trt1, Trt2, Trt3, Trt4
var2: Trt1, Trt2, Trt3, Trt4
var3: Trt1, Trt2, Trt3, Trt4
var4 : Trt1, Trt2, Trt3, Trt4

Mock
var1: Trt1, Trt2, Trt3, Trt4
var2 : Trt1, Trt2, Trt3, Trt4
var3 : Trt1, Trt2, Trt3, Trt4
var4 : Trt1, Trt2, Trt3, Trt4

graphes = NULL
dt = NULL

for (inoc in c("Inoc", "Mock")) {
 graphes[[inoc]] = NULL
 dt [[inoc]] = NULL

#outcome is for exemple the weight of the plants, the length that I want to explain in my linear model
  for (var_name in c("Outcome1", "Outcome2", "Outcome3")) {
    graphes[[var_name]] = NULL
    dt[[var_name]] = NULL
    
  
    for (espece in c("Var1","Var2","Var3","Var4")) {
      tab = filter(dt2, Variété==espece, Inoc==inoc)
      
      anova = aov(get(var_name) ~ Traitement, data = tab)
      
      # Tukey's test
      tukey = TukeyHSD(anova)
      
      # compact letter display
      cld = multcompLetters4(anova, tukey)
.
.
.
graph = ggplot(dt, aes(Traitement, w)) +

.
.
.
graphes[[inoc]] [[var_name]] [[espece]]= graph
}
}
}

and then another loop to generate the graphes

library(gridExtra)
for (inoc in c("Inoc","Mock")) {
  for (var_name in c("Outcome1", "Outcome2", "Outcome3")) {
    grid.arrange(graphes[[inoc]] [[var_name]] [["Var1"]],
                 graphes[[inoc]] [[var_name]] [["Var2"]],
                 graphes[[inoc]] [[var_name]] [["Var3"]],
                 graphes[[inoc]] [[var_name]] [["Var4"]],
                 nrow=2, ncol=2, top=paste(var_name,inoc))
  }
}

I show only two but with grid.arrange I obtain four graphes (1 per variety) with Tukey test inside a variety only, not all the varieties in the same time as in the first image of the post. And I obtain the graphes for all my outcomes for Inoc and Mock to my 4 varieties in each.

I hope it'll help because I never found the entire answer to my problem :slight_smile:

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.