Tukey's test result of two-way ANOVA (unbalance designs) on boxplot R

I want to compute two-way ANOVA (unbalance design, Type III ss) and annotate the HSD post-hoc on boxplot. Can anybody help me?

I have seen many related questions and answers, but all of them deals with one-way ANOVA and balance design.

Let's say this is the data

TRY2<-data.frame(Tissue=c("Panicle","Flag leaf", "Root","Panicle", "Flag leaf "," Root", " Root ", "Flag leaf", "Panicle", "Panicle", "Root","Panicle","Flag leaf ","Flag leaf", "Flag leaf ", "Root","Panicle","Root","Panicle","Root","Panicle","Panicle","Panicle"," Flag leaf","Panicle")
Genotype= c("N22", "N22","N22","IR64","IR64","IR64","N22","IR64","IR64","IR64","N22","N22","N22","IR64","N22","IR64","IR64","N22","N22","IR64","N22","N22","IR64","IR64","N22")
Value= c(1.0000000,0.5000000,1.0000000, 0.7000000,0.7500000,1.0000000,0.8000000,1.0000000,0.3333333,0.8571429,0.8333333,0.3333333,0.8000000,0.8125000,0.8750000,1.0000000,0.2571429,1.0000000,0.3714286,0.6000000,0.9375000,0.9583333,1.0000000,0.9545455,0.7619048))

Compute ANOVA

my_aov<-aov(Value~Genotype*Tissue, TRY2)
my2_aov<-Anova(my_aov, type="III") # This is how I want to compute ANOVA for the unbalance designs, but error in Tukey test (error shown below)

TUKEY<- TukeyHSD(my2_aov)
Error in UseMethod("TukeyHSD") : no applicable method for 'TukeyHSD' applied to an object of class "c('anova', 'data.frame')"

So, decided to compute tukey test of 'my_aov' instead of 'my2_aov' [this is not what I want]

TUKEY<-TukeyHSD(my_aov)

Now, define function to annotate Tukey test result on top of boxplot (Tukey Test and boxplot in R – the R Graph Gallery)

generate_label_df <- function(TUKEY, variable){
# Extract labels and factor levels from Tukey post-hoc
Tukey.levels <- TUKEY[[variable]][,4]
Tukey.labels <- data.frame(multcompLetters(Tukey.levels)['Letters'])
#I need to put the labels in the same order as in the boxplot :
Tukey.labels$treatment=rownames(Tukey.labels)
Tukey.labels=Tukey.labels[order(Tukey.labels$treatment) , ]
return(Tukey.labels)
}

Error

LABELS<- generate_label_df(TUKEY,"TRY2$Tissue")
Error in strsplit(x, sep) : non-character argument
Traceback

  1. strsplit(x, sep)
  2. vec2mat2(namx)
  3. multcompLetters(Tukey.levels)
  4. data.frame(multcompLetters(Tukey.levels)["Letters"])
  5. generate_label_df(TUKEY, "TRY2$Tissue")

This is the boxplot on which I like to annotate the Tukey's test result

Your example is not reproducible, I have tried to make it work based on this other similar topic, I think this could work as a reprex for your issue and as a starting point.

library(tidyverse)
library(vegan)
library(multcompView)
library(car)

TRY2 <- data.frame(stringsAsFactors = FALSE,
                   Tissue=c("Panicle", "Flag leaf", "Root", "Panicle", "Flag leaf",
                            "Root", "Root", "Flag leaf", "Panicle", "Panicle",
                            "Root", "Panicle", "Flag leaf", "Flag leaf", "Flag leaf",
                            "Root", "Panicle", "Root", "Panicle", "Root", "Panicle",
                            "Panicle", "Panicle", "Flag leaf", "Panicle"),
                   Genotype= c("N22", "N22", "N22", "IR64", "IR64", "IR64", "N22",
                               "IR64", "IR64", "IR64", "N22", "N22", "N22", "IR64",
                               "N22", "IR64", "IR64", "N22", "N22", "IR64", "N22",
                               "N22", "IR64", "IR64", "N22"),
                   Value= c(1.0000000, 0.5000000, 1.0000000, 0.7000000, 0.7500000,
                            1.0000000, 0.8000000, 1.0000000, 0.3333333, 0.8571429,
                            0.8333333, 0.3333333, 0.8000000, 0.8125000, 0.8750000,
                            1.0000000, 0.2571429, 1.0000000, 0.3714286, 0.6000000,
                            0.9375000, 0.9583333, 1.0000000, 0.9545455, 0.7619048)
)

my_aov <- aov(Value ~ Genotype * Tissue, TRY2)
TUKEY<- TukeyHSD(my_aov)

generate_label_df <- function(HSD, flev){
  Tukey.levels <- HSD[[flev]][,4]
  Tukey.labels <- multcompLetters(Tukey.levels)['Letters']
  plot.labels <- names(Tukey.labels[['Letters']])
  boxplot.df <- TRY2 %>% 
    group_by(!!sym(flev)) %>% 
    summarise(Value = max(fivenum(Value)) + 0.2)
  plot.levels <- data.frame(var = plot.labels,
                            labels = Tukey.labels[['Letters']],
                            stringsAsFactors = FALSE)
  labels.df <- merge(boxplot.df, plot.levels, by.x = flev, by.y = "var", sort = FALSE)
  return(labels.df)
}

ggplot(TRY2, aes(x=Tissue, y = Value))+
  geom_boxplot(aes(fill = Genotype)) +
  geom_text(data = generate_label_df(TUKEY,'Tissue'), aes(label = labels))

Created on 2019-11-01 by the reprex package (v0.3.0)

1 Like

This is not exactly what I want, but very helpful for me. Thank you @andresrcs

What I want is to annotate the HSD post-hoc test for each box (To compare the value of tissues within and between the genotype).

This is how I want to draw the graph

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