How to plot the significant Tukey results as red

1.3k Views Asked by At

I have used the following code to plot the results of the Tukey test after my Anova analysis in R.

TukeyHSD(myANOVA, conf.level=.90)
TUKEY <- TukeyHSD(myANOVA, conf.level=.90)
plot(TUKEY , las=1 , col="black")

However, since the number of lines plotted is too large, I would like to have the significant ones highlighted or in red. I have seen a similar question here with the comment "overwrite the black lines showing significant differences with red lines" however, I don't know how to do it.

2

There are 2 best solutions below

1
On

Imagine we have the following data:

data <- data.frame(group = rep(c("P1", "P2", "P3"), each = 40), values = c(rnorm(40, 0, 3),rnorm (40, 8, 10),rnorm (40, 0, 3)))

Then we conduct a Tukey test, convert the results to a matrix, and then to a dataframe (I don't know how to do it otherwise):

results_test <- TukeyHSD(aov(data$values~ data$group), conf.level=.95)
results_matrix <- as.matrix (results_test) 
df_res <- as.data.frame(results_matrix[1])

Then we plot it using an ifelse function, as a function of the p-values:

plot(results_matrix, col= ifelse(df_res[,4]<0.05, 'red', 'black'))
3
On

I personally prefer the plot generated by the multcomp package, and with this package you can perform the Tukey test for unbalanced designs.

library(multcomp)

### set up a one-way ANOVA
data(warpbreaks)
amod <- aov(breaks ~ tension, data = warpbreaks)
### specify all pair-wise comparisons among levels of variable "tension"
tuk <- glht(amod, linfct = mcp(tension = "Tukey"))
### p-values
pvalues <- adjusted()(tuk)$pvalues
### get confidence intervals
ci.glht <- confint(tuk)
### plot them
plot(ci.glht, col = ifelse(pvalues < 0.05, "red", "black"), 
     xlab = "Difference in mean levels")

enter image description here