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
- strsplit(x, sep)
- vec2mat2(namx)
- multcompLetters(Tukey.levels)
- data.frame(multcompLetters(Tukey.levels)["Letters"])
- generate_label_df(TUKEY, "TRY2$Tissue")