英文:
How to display letters to pairwise comparison plot?
问题
I can help you with the translation. Here's the translated text without the code:
如何在 ggstatsplot
包的 Kruskal Wallis 检验图上显示字母?这是基于 此问题 的可重现示例。
目前,我的图上显示的是 p 值的柱形图。我想要显示字母,而不是 p 值的柱形图。该问题已经在 这里 得到了回答。显然,AddLetters
函数应该像下面示例中显示的那样,显示字母,但在我的情况下,它无限运行而不显示任何字母。有没有其他显示字母的方法?
示例图,其中显示了字母而不是柱形图 这里
英文:
How can I display letters on a ggstatsplot
package plot for Kruskal Walllis test? This is a reproducible example based on this question.
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)
Currently, I have bars showing p-values. I want letters instead of bars showing p values. The question has been answered here. Apparently, AddLetters
function should show letters instead of p values as shown below in his example, but it runs indefinitely without displaying any letters in my case. Is there any other way of displaying letters?
Example plot where letters are shown instead of bars here
答案1
得分: 2
我们需要更改输入以匹配multcompLetters
。
这是如何做的。
library(Matrix)
library(PMCMRplus)
library(ggstatsplot)
#此代码在您的帖子中制作图表
p <- ggbetweenstats(data = dat, y = mean_severity, x = month, type = "nonparametric")
#此代码执行Dunn配对检验
#kwAllPairsDunnTest(mean_severity ~ month, data=dat, p.adjust.method = "holm")
#现在我们需要将p值矩阵格式化为multcompLetters的对称矩阵
pval.matrix <- kwAllPairsDunnTest(x = dat$mean_severity,
g = as.factor(dat$month), p.adjust.method = "holm")
> pval.matrix$p.value
四月 八月 七月 六月 五月 十一月 十月
八月 0.004092534 NA NA NA NA NA NA
七月 1.000000000 0.001097334 NA NA NA NA NA
六月 0.004907027 1.000000000 0.001911186 NA NA NA NA
五月 0.029411147 1.000000000 0.015500796 1.0000000000 NA NA NA
十一月 0.136459815 1.000000000 0.167977744 1.0000000000 1.000000000 NA NA
十月 1.000000000 0.000118815 1.000000000 0.0002455998 0.002648157 0.07101786 NA
九月 0.009390306 1.000000000 0.004164969 1.0000000000 1.000000000 1.00000000 0.0006461642
我们在操纵配对p值矩阵后使用Matrix
包中的forceSymmetric
:
#使p值矩阵成为方阵并对角化
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"))
#将九月添加到行和列名称
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
四月 八月 七月 六月 五月 十一月 十月 九月
四月 1.000000000 0.004092534 1.000000000 0.0049070268 0.029411147 0.13645981 1.0000000000 0.0093903064
八月 0.004092534 1.000000000 0.001097334 1.0000000000 1.000000000 1.00000000 0.0001188150 1.0000000000
七月 1.000000000 0.001097334 1.000000000 0.0019111864 0.015500796 0.16797774 1.0000000000 0.0041649687
六月 0.004907027 1.000000000 0.001911186 1.0000000000 1.000000000 1.00000000 0.0002455998 1.0000000000
五月 0.029411147 1.000000000 0.015500796 1.0000000000 1.000000000 1.00000000 0.0026481566 1.0000000000
十一月 0.136459815 1.000000000 0.167977744 1.0000000000 1.000000000 1.00000000 0.0710178590 1.0000000000
十月 1.000000000 0.000118815 1.000000000 0.0002455998 0.002648157 0.07101786 1.0000000000 0.0006461642
九月 0.009390306 1.000000000 0.004164969 1.0000000000 1.000000000 1.00000000 0.0006461642 1.0000000000
现在multcompLetters
起作用:
> multcompLetters(new.pval.matrix)
四月 八月 七月 六月 五月 十一月 十月 九月
"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))
#匹配[factor]月份的顺序
data.summary <- data.summary[order(data.summary$month),]
CLD <- multcompLetters(new.pval.matrix)
data.summary$CLD <- CLD$Letters
#您可能需要更改这些图形选项以适应您的目的
p + geom_text(data = data.summary, aes(label=CLD,x=month, y=mean),
position=position_dodge2(0.75), hjust = 3)
英文:
We have to change the input to match multcompLetters
.
Here is how to do it.
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
We use forceSymmetric
from the Matrix
package after manipulating the pairwise p-value matrix:
#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
Now multcompLetters
works:
> multcompLetters(new.pval.matrix)
April August July June May November October September
"a" "b" "a" "b" "b" "ab" "a" "b"
We can follow your link on how to prepare the 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论