我使用了r-graph-gallery.com中的代码,并根据我的数据进行了调整。
我想在一个图中做6个盒子图,并以特定的顺序对它们进行排序,但是当我在进行排序时,tukey分析不会同时对其本身进行排序!
你知道我如何改进它吗?
这是错误的顺序,但是正确的tukey测试表示:

在这里,它有正确的顺序,但没有正确的tukey测试重新划分。

我应该怎么做才能得到TUKEY测试的正确顺序呢?
顺便问一下,谁知道如何得到tukey测试的最高均值的"a“值,而不是"c”?
感谢您的帮助!
下面是我使用的代码:
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])] )发布于 2020-02-21 06:30:06
嘿,我想我知道你的问题了,基本上是正确地给盒子图分配了一个颜色的/text标签。
下面我生成了一些数据,做了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)]]和plot:
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 )

https://stackoverflow.com/questions/60244098
复制相似问题