Professional Documents
Culture Documents
MENU
https://www.r-graph-gallery.com/84-tukey-test/ 1/9
7/2/2019 #84 Tukey test – The R Graph Gallery
Tukey’s test is a single-step multiple comparison procedure and statistical test. It is a post-hoc analysis,
what means that it is used in conjunction with an ANOVA. It allows to nd means of a factor that are
signi cantly different from each other, comparing all possible pairs of means with a t-test like method.
You can read this page if you want to understand the exact procedure of this test.
Results are usually drawn in a boxplot. Levels that are not signi cantly different one each other are
represented with the same letter.
synonymous: Tukey’s range test / Tukey method / Tukey’s honest signi cance test / Tukey’s HSD
(honest signi cant difference) test / Tukey–Kramer method
1 # library
2 library(multcompView)
3
4 # Create data
5 treatment=c(rep("A", 20) , rep("B", 20) , rep("C", 20), rep("D", 20) , rep("E", 20))
6 value=c( sample(2:5, 20 , replace=T) , sample(6:10, 20 , replace=T), sample(1:7, 20 , re
7 data=data.frame(treatment,value)
8
9 # What is the effect of the treatment on the value ?
10 model=lm( data$value ~ data$treatment )
11 ANOVA=aov(model)
12
13 # Tukey test to study each pair of treatment :
14 TUKEY <- TukeyHSD(x=ANOVA, 'data$treatment', conf.level=0.95)
15
16 # Tuckey test representation :
17 plot(TUKEY , las=1 , col="brown" )
18
19
20 # I need to group the treatments that are not different each other together.
https://www.r-graph-gallery.com/84-tukey-test/ 2/9
7/2/2019 #84 Tukey test – The R Graph Gallery
21 generate_label_df <- function(TUKEY, variable){
22
23 # Extract labels and factor levels from Tukey post-hoc
24 Tukey.levels <- TUKEY[[variable]][,4]
25 Tukey.labels <- data.frame(multcompLetters(Tukey.levels)['Letters'])
26
27 #I need to put the labels in the same order as in the boxplot :
28 Tukey.labels$treatment=rownames(Tukey.labels)
29 Tukey.labels=Tukey.labels[order(Tukey.labels$treatment) , ]
30 return(Tukey.labels)
31 }
32
33 # Apply the function on my dataset
34 LABELS=generate_label_df(TUKEY , "data$treatment")
35
36
37 # A panel of colors to draw each group with the same color :
38 my_colors=c( rgb(143,199,74,maxColorValue = 255),rgb(242,104,34,maxColorValue = 255), rg
39
40 # Draw the basic boxplot
41 a=boxplot(data$value ~ data$treatment , ylim=c(min(data$value) , 1.1*max(data$value)) ,
42
43 # I want to write the letter over each box. Over is how high I want to write it.
44 over=0.1*max( a$stats[nrow(a$stats),] )
45
46 #Add the labels
47 text( c(1:nlevels(data$treatment)) , a$stats[nrow(a$stats),]+over , LABELS[,1] , col=my
Search … SEARCH
[mediatagger]
https://www.r-graph-gallery.com/84-tukey-test/ 3/9
7/2/2019 #84 Tukey test – The R Graph Gallery
9 LEAVE A REPLY
7 2 2 9
Eleftheria
Many thanks for this! Do you know of a code for checking the assumptions fro the TukeyHSD test
Guest in R? Thanks again!
Bien
Quin
Hello, This is exactly the graph I’d like to build for my data.! Thanks so much for writing this. I am
Guest wondering, how would you order the levels from highest means to lowest?
Thanks in advance,
Q
Lochs
You could probably achieve this by ordering your categorical factors in your data frame by
Guest
their mean values? Then just produce the plot as normal.
https://www.r-graph-gallery.com/84-tukey-test/ 4/9
7/2/2019 #84 Tukey test – The R Graph Gallery
Diana
what is the treatment and value???? im failing to interprete them. trying to use the
code on my data. thanx.
Guest
Ayush Gyawali
Hey, thanks for this R-Script. It worked. But is it possible to modify this script to get graph and
Guest signi cant letters in terms of a factorial design?
For example
In case of code- LABELS=generate_label_df(TUKEY , “x$Treatment”)
If Treatment and Depth are two Treatments in a factorial design then can this above be written
as..
code – LABELS=generate_label_df(TUKEY , “x$Treatment”, “x$Depth”)????
Thanks in advance!!
https://www.r-graph-gallery.com/84-tukey-test/ 5/9
7/2/2019 #84 Tukey test – The R Graph Gallery
https://www.r-graph-gallery.com/84-tukey-test/ 6/9
7/2/2019 #84 Tukey test – The R Graph Gallery
https://www.r-graph-gallery.com/84-tukey-test/ 7/9
7/2/2019 #84 Tukey test – The R Graph Gallery
https://www.r-graph-gallery.com/84-tukey-test/ 8/9
7/2/2019 #84 Tukey test – The R Graph Gallery
https://www.r-graph-gallery.com/84-tukey-test/ 9/9