Reputation: 2460
In my work I often have to make different treatment comparisons using Anova and Tukey tests to determine which of multiple treatments in one factor experiments are statistically distinct from one another.
The code I have attached yields two separate figures: one with treatment distribution of values (example graph1) and another with the Tukey test results showing which pair of treatments are significantly different from one another (example graph2).
What I have done in the past is to look at the Tukey results and manually edit the first graph with letters indicating groups of statistically equivalent groups (example graph3). I have been looking at different r libraries for ways to automatically produce something similar to graph 3 that summarizes such groupings but have not yet found a way. Does anyone have any suggestions?
PS- I am sorry if the graph routine below is a little cumbersome, but it is essentially a fragment of a much more comprehensive set of functions that I have developed to test data distribution, conditionally apply relevant tests and produce output tables and figures.
The code I have written to make the first two graphs is below. t?usp=sharing
Group=c("G1","G1","G1","G1","G2","G2","G2","G2","G3","G3","G3","G3")
Vals=c(runif(4),runif(4)+0.5,runif(4)+0.1)
data=data.frame(Group)
data=cbind(data, Vals)
anova_results=aov(Vals~Group,data=data)
anova_results2=anova(anova_results)[1, ]
anova_significance=anova_results2[1,"Pr(>F)"]
significant=anova_significance[1]<=0.05
if (significant==1) {
Tukey_results=TukeyHSD(anova_results,"Group")
Tukey_results=Tukey_results$Group
}
plot(data$Group, data$Vals)
if (significant==1) {
plot(TukeyHSD(anova_results,"Group"), las=1)
}
Upvotes: 1
Views: 1830
Reputation: 2460
Roman Lustrik's suggestions on the comments above were spot on. I ended up finding two alternative ways to do it based on two related libraries. After running the code posted in the question, to create the grouped treatment plots run:
#simple looking solution
library(multcomp)
tuk <- glht(anova_results, linfct = mcp(Group = "Tukey"))
summary(tuk) # standard display
tuk.cld <- cld(tuk) # letter-based display
opar <- par(mai=c(1,1,1.5,1))
plot(tuk.cld)
par(opar)
#more fancy looking solution using the multcompView library with a lot of ways to
#alter the plot appearance as necessary
library(multcompView)
multcompBoxplot(Vals~Group,data=data)
# Now, the solution below is my favorite solution as the text direction of the groups
#work very well if you have many treatments that you are comparing
opar <- par()
par(oma = c(6, 0, 0, 0)) #extra space for extra large treatment names
xzx <-multcompBoxplot(Vals~Group,data=data,sortFn=median, decreasing=FALSE,
horizontal=FALSE,
plotList=list(
boxplot=list(fig=c(0, 1, 0, 1), las=3,
cex.axis=1.5),
multcompLetters=list(
fig=c(0.87, 0.97, 0.115, 0.923), #0.1108, 0.9432 Top of
#page 18 manual for very convoluted explanation (c(y bottom, y top,x L, x R))
type='Letters') ) )
par(opar)
Upvotes: 1