首页 > 解决方案 > How to order variable in a boxplot with respective tukey test

问题描述

I used the code from r-graph-gallery.com that i adapted to my data.

I would like to do 6 boxplots in one graph and order them in a specific order, but then as I am doing the ordering, the tukey analysis doesn't order itself at the same time with it !

Do you know how I can improve it ?

This is the wrong order but the right tukey test representation : Graph with the wrong order

And here the one that has the right order but not the right tukey test repartition. Graph with wrong tukey test position

What should I do to get the right order for the TUKEY test as well ?

By the way does anyone knows how to get the "a" value for the highest mean of the tukey test and not the "c" ?

Thank you for your help !

Here is the code I used :

date<- (read.delim("SoilOBIoldtd.txt", header=TRUE))
# library
library(multcompView)
# What is the effect of the level on the CEC ?
model=lm( date$CEC_eff ~ date$level )

ANOVA=aov(model)
# Tukey test to study each pair of level :
TUKEY <- TukeyHSD(x=ANOVA, 'date$level', conf.level=0.95)
#This line is the difference between the two plots (using or ignoring this line)
date$level <- factor(date$level , levels=c("DAFS_Top", "DAFS_Down",
"CONV_Top","CONV_Down","Old_cocoa_Top","Old_cocoa_Down"))
# Tuckey test representation :
plot(TUKEY , las=1 , col="brown")
generate_label_df <- function(TUKEY, CEC_eff){
   # Extract labels and factor levels from Tukey post-hoc 
  Tukey.levels <- TUKEY[[CEC_eff]][,4]
  Tukey.labels <- data.frame(multcompLetters(Tukey.levels,reversed = FALSE)['Letters'])
  #I need to put the labels in the same order as in the boxplot :
  Tukey.labels$level=rownames(Tukey.labels)
  Tukey.labels=Tukey.labels[order(Tukey.labels$level) , ]
  return(Tukey.labels)}
# Apply the function on my dataset
LABELS <- generate_label_df(TUKEY , "date$level")
# A panel of colors to draw each group with the same color :
my_colors <- c(   rgb(143,199,74,maxColorValue = 255),  rgb(242,104,34,maxColorValue = 255),   rgb(111,145,202,maxColorValue = 255))
# Draw the basic boxplot
a <-boxplot(date$CEC_eff ~ date$level , ylim=c(min(date$CEC_eff ) , 1.1*max(date$CEC_eff)) , col=my_colors[as.numeric(LABELS[,1])] , ylab="CEC" , main="")
# I want to write the letter over each box. Over is how high I want to write it.
over <- 0.1*max( a$stats[nrow(a$stats),] )
#Add the labels
text( c(1:nlevels(date$level)) , a$stats[nrow(a$stats),]+over , LABELS[,1]  , col=my_colors[as.numeric(LABELS[,1])] )

标签: rpositionboxplottukey

解决方案


嘿,我想我遇到了您的问题,基本上是为箱线图正确分配了颜色/文本标签。

下面我生成了一些数据,做了 Tukey,然后把标签拿出来了:

library(multcompView)

set.seed(111)
lvl = c("DAFS_Top", "DAFS_Down",
"CONV_Top","CONV_Down","Old_cocoa_Top","Old_cocoa_Down")
df = data.frame(CEC_eff=rnorm(48,rep(c(2,4,6),each=8),1),level=rep(lvl,each=8))

df$level <- factor(df$level , levels=c("DAFS_Top", "DAFS_Down",
"CONV_Top","CONV_Down","Old_cocoa_Top","Old_cocoa_Down"))

ANOVA=aov(lm(CEC_eff ~ level ,data=df))
TUKEY <- TukeyHSD(x=ANOVA, "level", conf.level=0.95)
LABELS <- multcompLetters(TUKEY$level[,4],reversed=FALSE)$Letters

现在您定义了颜色,您可以将字母分配给颜色。

my_colors <- c(   rgb(143,199,74,maxColorValue = 255),  
rgb(242,104,34,maxColorValue = 255),   rgb(111,145,202,maxColorValue = 255))

names(my_colors) <- sort(unique(LABELS))

然后我们将颜色分配给将出现在箱线图中的级别:

lvl_colors <- my_colors[LABELS[levels(df$level)]]

和情节:

a <-boxplot(CEC_eff ~ level , data=df, cex.axis=0.7,ylim=c(min(df$CEC_eff ),
1.1*max(df$CEC_eff)) , col= lvl_colors, ylab="CEC" , main="")

over <- 0.1*max( a$stats[nrow(a$stats),] )
#Add the labels
text(1:nlevels(df$level) ,a$stats[nrow(a$stats),]+over ,names(lvl_colors )  , 
col=lvl_colors  )

在此处输入图像描述


推荐阅读