我想知道是否有一种方法可以指定在 R 中的 ggpubr 包中的 ggboxplot 上显示哪些 p 值,但我一直没有成功尝试这样做。
我发现的唯一选择是预先过滤数据,但我不想在制作图表时排除这些组。即,我只想显示平均值 > 我的参考组的组的 p 值,但我仍然想查看所有数据。
在我的示例中,我希望“lo”组隐藏其显着性水平,同时仍保留在图表上,因为其平均值低于参考组
library(ggplot2)
library(ggpubr)
library(dplyr)
Plant<-c("ref","ref","ref","ref","ref","ref","ref","ref","ref",
"hi","hi","hi","hi","hi","hi","hi","hi","hi",
"lo","lo","lo","lo","lo","lo","lo","lo","lo")
Delta.CCI<-c(11.05,11.45,9.65,10.65,10.15,8.95,8.95,12.45,8.95,
20.56,20.66,19.76,20.36,20.26,20.06,19.16,19.16,19.06,
2.18,1.58,2.98,1.11,1.91,0.21,1.68,2.11,0.51)
df<-data.frame(Plant,Delta.CCI)
#generate a list of comparisons for the data frame
cdf<-compare_means(Delta.CCI ~ Plant,
data = df,
ref.group = "ref")
#make a new list
my_list <- vector()
#for every other comparison put a 1 in the list
#this is so I can stagger the p-values later since there are so many in my actual data set
for(i in 1:nrow(cdf)+1){
if(i %% 2 == 0){
my_list[i] <- 1
} else {
my_list[i] <- 0
}
}
#Make the box plot
p<-ggboxplot(df,
title = "A",
x="Plant",
y="Delta.CCI")+
#rotate the text so it is legible
rotate_x_text(angle = 90)+
#make a line showing where the mean of the LBA/GFP group is
geom_hline(yintercept = mean(df$Delta.CCI[df$Plant=="ref"]), linetype = 2)+
#add out p-values to the plot as significance labels
stat_compare_means(label="p.signif",label.y=my_list+28,ref.group="ref")
p
一种选择是使用
stat_pvalue_manual
。为此,我使用 rstatix
包中的较低级别函数来创建测试统计数据的 data.frame。您可以轻松地向此 df 添加 y 位置,而无需创建单独的向量。此外,通过参数 detailed=TRUE
wilcox_test
允许添加对均值差异的估计,以有条件地用空字符串替换不需要的标签:
library(ggplot2)
library(ggpubr)
library(dplyr, warn=FALSE)
library(rstatix)
cdf <- df |>
wilcox_test(Delta.CCI ~ Plant, ref.group = "ref",
detailed = TRUE) |>
add_significance("p") |>
mutate(
y.position = 28 + (row_number() %% 2 == 0),
p.signif = if_else(estimate < 0, p.signif, "")
)
p <- ggboxplot(df,
title = "A",
x = "Plant",
y = "Delta.CCI"
) +
rotate_x_text(angle = 90) +
geom_hline(yintercept = mean(df$Delta.CCI[df$Plant == "ref"]), linetype = 2) +
stat_pvalue_manual(
cdf,
label = "p.signif",
x = "group2"
)
p