如何显示成对比较图的字母?

How to display letters to pairwise comparison plot?

提问人:Ahsk 提问时间:6/30/2023 最后编辑:user438383Ahsk 更新时间:7/1/2023 访问量:138

问:

如何在 Kruskal Walllis 检验的封装图上显示字母?这是一个基于这个问题的可重复的例子。ggstatsplot

set.seed(123)

# Create vector for number of cases per month
cases_per_month <- c(10, 25, 20, 20, 25, 20, 19, 5)

# Create vector for months (April to November)
months <- c("April", "May", "June", "July", "August", "September", "October", "November")

# Create empty vectors for final dataset
dataset <- data.frame(mean_severity = numeric(), month = character())

# Generate dataset
dat <- list()

for (i in 1:length(months)) {
  month <- rep(months[i], cases_per_month[i])
  severity <- sample.int(n = 10, size = cases_per_month[i], replace = TRUE)
  
  # generate some differences in the sample
  if (i %in% c(1, 4, 7)){
    severity <- severity^2
  }
  
  temp_data <- data.frame(mean_severity = severity, month = month)
  dat[[i]] <- rbind(dataset, temp_data)
}

# Using rbind to combine rows
dat <- do. Call(rbind, dat) 

enter image description here

目前,我有显示 p 值的条形图。我想要字母而不是显示 p 值的条形图。这个问题已经在这里得到解答。显然,函数应该显示字母而不是 p 值,如下面的示例所示,但在我的情况下,它无限期地运行而不显示任何字母。有没有其他显示字母的方式?AddLetters

此处显示字母而不是条形图的示例图 enter image description here

R ggPlot2 线性回归方 差分析 统计检验

评论

0赞 David 6/30/2023
我不确定使用该函数是否有效,部分原因是该包难以使用。要搜索的关键字是“紧凑的字母显示”。这里有一些解决方案:stackoverflow.com/questions/69309101/... 和 community.rstudio.com/t/...AddLetters"CLD"geom_text
0赞 Ahsk 6/30/2023
@David 谢谢。我能够复制上述方差分析示例,但不能复制 Kruskal-Wallis 测试。出现错误Error in x$terms %||% attr(x, "terms") %||% stop("no terms component nor attribute") : no terms component nor attribute

答:

2赞 David 7/1/2023 #1

我们必须更改输入以匹配 .multcompLetters

这是如何做到的。

library(Matrix)
library(PMCMRplus)
library(ggstatsplot)

#this does the plot in your post
p <- ggbetweenstats(data = dat, y = mean_severity, x = month, type = "nonparametric")

#this does the Dunn pairwise tests
#kwAllPairsDunnTest(mean_severity ~ month, data=dat, p.adjust.method = "holm")

#now we have to format the p-value matrix into a symmetric matrix for multcompLetters
pval.matrix <- kwAllPairsDunnTest(x = dat$mean_severity, 
               g = as.factor(dat$month), p.adjust.method = "holm")

> pval.matrix$p.value
                April      August        July         June         May   November      October
August    0.004092534          NA          NA           NA          NA         NA           NA
July      1.000000000 0.001097334          NA           NA          NA         NA           NA
June      0.004907027 1.000000000 0.001911186           NA          NA         NA           NA
May       0.029411147 1.000000000 0.015500796 1.0000000000          NA         NA           NA
November  0.136459815 1.000000000 0.167977744 1.0000000000 1.000000000         NA           NA
October   1.000000000 0.000118815 1.000000000 0.0002455998 0.002648157 0.07101786           NA
September 0.009390306 1.000000000 0.004164969 1.0000000000 1.000000000 1.00000000 0.0006461642

在操作成对的 p 值矩阵后,我们从包中使用:forceSymmetricMatrix

#square and diagonalize the p-value matrix
new.pval.matrix <- rbind(1,pval.matrix$p.value)
new.pval.matrix <- cbind(new.pval.matrix, 1)
diag(new.pval.matrix) <- 1

new.pval.matrix <- as.matrix(forceSymmetric(new.pval.matrix, "L"))

#Add September to the row and column names
rownames(new.pval.matrix)[dim(pval.matrix$p.value)+1] <- 
                    rownames(pval.matrix$p.value)[dim(pval.matrix$p.value)[1]]
colnames(new.pval.matrix)[dim(pval.matrix$p.value)+1] <- 
                    rownames(pval.matrix$p.value)[dim(pval.matrix$p.value)[1]]

> new.pval.matrix
                April      August        July         June         May   November      October    September
April     1.000000000 0.004092534 1.000000000 0.0049070268 0.029411147 0.13645981 1.0000000000 0.0093903064
August    0.004092534 1.000000000 0.001097334 1.0000000000 1.000000000 1.00000000 0.0001188150 1.0000000000
July      1.000000000 0.001097334 1.000000000 0.0019111864 0.015500796 0.16797774 1.0000000000 0.0041649687
June      0.004907027 1.000000000 0.001911186 1.0000000000 1.000000000 1.00000000 0.0002455998 1.0000000000
May       0.029411147 1.000000000 0.015500796 1.0000000000 1.000000000 1.00000000 0.0026481566 1.0000000000
November  0.136459815 1.000000000 0.167977744 1.0000000000 1.000000000 1.00000000 0.0710178590 1.0000000000
October   1.000000000 0.000118815 1.000000000 0.0002455998 0.002648157 0.07101786 1.0000000000 0.0006461642
September 0.009390306 1.000000000 0.004164969 1.0000000000 1.000000000 1.00000000 0.0006461642 1.0000000000

现在有效:multcompLetters

> multcompLetters(new.pval.matrix)
    April    August      July      June       May  November   October September 
      "a"       "b"       "a"       "b"       "b"      "ab"       "a"       "b" 

我们可以点击您的链接了解如何准备 CLD:

data.summary <- group_by(dat, month) %>% 
  summarise(mean=mean(mean_severity), sd=sd(mean_severity)) %>% 
  arrange(desc(mean))
#match ordering of the factors [month]
data.summary <- data.summary[order(data.summary$month),]

CLD <- multcompLetters(new.pval.matrix)
data.summary$CLD <- CLD$Letters

#you'll likely need to change these graphics options for your purposes
p + geom_text(data = data.summary, aes(label=CLD,x=month, y=mean), 
    position=position_dodge2(0.75), hjust = 3)

plot with CLD