I need to plot Tukey's Test result which includes 8 groups. I am following R-graph gallery example where it works for four group. I tried to spent time modifying it 1. Changing the Tukey.levels <- TUKEY[[variable]][,4] to Tukey.levels <- TUKEY[[variable]][,7] because I have 8 groups. This gave me error Error in TUKEY[[variable]][, 7] : subscript out of bounds So I again use 4 2. Added more colors
But I am not able to get the plot which shows all the 28 comparisons on a box plot. Here is the data and my code
treatment <- rep(c("A", "B", "C", "D", "E","F","G","H"), each=20)
value=c( sample(2:5, 20 , replace=T) , sample(6:10, 20 , replace=T), sample(1:7, 20 , replace=T), sample(3:10, 20 , replace=T) , sample(10:20, 20 , replace=T),sample(11:15, 20 , replace=T),sample(6:12, 20 , replace=T),sample(3:3, 20 , replace=T) )
data=data.frame(treatment,value)
model=lm( data$value ~ data$treatment )
ANOVA=aov(model)
TUKEY <- TukeyHSD(x=ANOVA, 'data$treatment', conf.level=0.95)
plot(TUKEY , las=1 , col="brown")
generate_label_df <- function(TUKEY, variable){
Tukey.levels <- TUKEY[[variable]][,4]
Tukey.labels <- data.frame(multcompLetters(Tukey.levels)['Letters'])
Tukey.labels$treatment=rownames(Tukey.labels)
Tukey.labels=Tukey.labels[order(Tukey.labels$treatment) , ]
return(Tukey.labels)
}
LABELS <- generate_label_df(TUKEY , "data$treatment")
my_colors <- c(
rgb(143,199,74,maxColorValue = 255),
rgb(242,104,34,maxColorValue = 255),
rgb(111,145,202,maxColorValue = 255),
rgb(255,0,0,maxColorValue = 255),
rgb(0,255,255,maxColorValue = 255),
rgb(128,0,0,maxColorValue = 255),
rgb(128,0,128,maxColorValue = 255)
)
a <- boxplot(data$value ~ data$treatment , ylim=c(min(data$value) , 1.1*max(data$value)) , col=my_colors[as.numeric(LABELS[,1])] , ylab="value" , main="")
text( c(1:nlevels(data$treatment)) , a$stats[nrow(a$stats),]+over , LABELS[,1] , col=my_colors[as.numeric(LABELS[,1])] )
If the groups are same then they should be represented by same color and same letter.
I also want to know how generate_label works. Do I always need to give value 4 even though I have 8 groups?