R ggplot2 箱线图 - ggpubr stat_compare_means 无法正常工作

标签 r ggplot2 boxplot p-value ggpubr

我正在尝试使用ggplot2ggpubr包以星号的形式向我的箱线图添加显着性水平,但我有很多比较,我只想显示重要的。

我尝试在 stat_compare_means 中使用选项 hide.ns=TRUE,但它显然不起作用,这可能是 ggpubr 包中的一个错误。

此外,您会看到我在成对 wilcox.test 比较中遗漏了组“PGMC4”;我怎样才能把这个组也排除在 kruskal.test 之外?

我的最后一个问题是显着性水平如何运作? * 低于 0.05 显着,** 低于 0.025,*** 低于 0.01? ggpubr 使用什么约定?它显示的是 p 值还是调整后的 p 值?如果是后者,调整方法是什么?黑度?

请检查下面我的 MWE 和 this linkthis other one供引用

##############################
##MWE
set.seed(5)
#test df
mydf <- data.frame(ID=paste(sample(LETTERS, 163, replace=TRUE), sample(1:1000, 163, replace=FALSE), sep=''),
                   Group=c(rep('C',10),rep('FH',10),rep('I',19),rep('IF',42),rep('NA',14),rep('NF',42),rep('NI',15),rep('NS',10),rep('PGMC4',1)),
                   Value=rnorm(n=163))
#I don't want to compare PGMC4 cause I have only onw sample
groups <- as.character(unique(mydf$Group[which(mydf$Group!="PGMC4")]))
#function to make combinations of groups without repeating pairs, and avoiding self-combinations
expand.grid.unique <- function(x, y, include.equals=FALSE){
    x <- unique(x)
    y <- unique(y)
    g <- function(i){
        z <- setdiff(y, x[seq_len(i-include.equals)])
        if(length(z)) cbind(x[i], z, deparse.level=0)
    }
    do.call(rbind, lapply(seq_along(x), g))
}
#all pairs I want to compare
combs <- as.data.frame(expand.grid.unique(groups, groups), stringsAsFactors=FALSE)
head(combs)
my.comps <- as.data.frame(t(combs), stringsAsFactors=FALSE)
colnames(my.comps) <- NULL
rownames(my.comps) <- NULL
#pairs I want to compare in list format for stat_compare_means
my.comps <- as.list(my.comps)
head(my.comps)
pdf(file="test.pdf", height=20, width=25)
print(#or ggsave()
  ggplot(mydf, aes(x=Group, y=Value, fill=Group)) + geom_boxplot() +
    stat_summary(fun.y=mean, geom="point", shape=5, size=4) +
    scale_fill_manual(values=myPal) +
    ggtitle("TEST TITLE") +
    theme(plot.title = element_text(size=30),
      axis.text=element_text(size=12),
      axis.text.x = element_text(angle=45, hjust=1),
      axis.ticks = element_blank(),
      axis.title=element_text(size=20,face="bold"),
      legend.text=element_text(size=16)) +
  stat_compare_means(comparisons=my.comps, method="wilcox.test", label="p.signif", size=14) + #WHY DOES hide.ns=TRUE NOT WORK??? WHY DOES size=14 NOT WORK???
  stat_compare_means(method="kruskal.test", size=14) #GLOBAL COMPARISON ACROSS GROUPS (HOW TO LEAVE PGMC4 OUT OF THIS??)
)
dev.off()
##############################

MWE 将生成以下箱线图:

test

问题是:

1-如何使 hide.ns=TRUE 工作?

2-如何增加*的大小?

3-如何从 kruskal.test 比较中排除某个组?

4- ggpubr 使用的 * 约定是什么,显示的 p 值是否经过调整?

非常感谢!!

编辑

此外,在做的时候

stat_compare_means(comparisons=my.comps, method="wilcox.test", p.adjust.method="BH")

我没有获得与执行时相同的 p 值

wilcox.test(Value ~ Group, data=mydf.sub)$p.value

其中 mydf.sub 是 mydf 的子集(),用于 2 个组的给定比较。

ggpubr 在这里做什么?它如何计算 p.values?

编辑2

请帮忙,解决方案不必使用ggpubr(但必须使用ggplot2),我只需要能够隐藏NS并使星号的大小更大,以及与 wilcox.test() + p.adjust(method"BH") 相同的 p 值计算。

谢谢!

最佳答案

编辑:自从我发现了 rstatix 包后,我会这样做:

set.seed(123)
#test df
mydf <- data.frame(ID=paste(sample(LETTERS, 163, replace=TRUE), sample(1:1000, 163, replace=FALSE), sep=''),
                   Group=c(rep('C',10),rep('FH',10),rep('I',19),rep('IF',42),rep('NA',14),rep('NF',42),rep('NI',15),rep('NS',10),rep('PGMC4',1)),
                   Value=c(runif(n=100), runif(63,max= 0.5)))


library(tidyverse)

stat_pvalue <- mydf %>% 
 rstatix::wilcox_test(Value ~ Group) %>%
 filter(p < 0.05) %>% 
 rstatix::add_significance("p") %>% 
 rstatix::add_y_position() %>% 
 mutate(y.position = seq(min(y.position), max(y.position),length.out = n())

ggplot(mydf, aes(x=Group, y=Value)) + geom_boxplot() +
  ggpubr::stat_pvalue_manual(stat_pvalue, label = "p.signif") +
  theme_bw(base_size = 16)

enter image description here

旧答案:

您可以尝试关注。这个想法是您使用pairwise.wilcox.test 自己计算统计数据。然后使用ggsignif函数geom_signif 添加预先计算的 p 值。使用 y_position,您可以放置​​括号,使它们不会重叠。

library(tidyverse)
library(ggsignif)
library(broom)
# your list of combinations you want to compare
CN <- combn(levels(mydf$Group)[-9], 2, simplify = FALSE)
# the pvalues. I use broom and tidy to get a nice formatted dataframe. Note, I turned off the adjustment of the pvalues. 
pv <- tidy(with(mydf[ mydf$Group != "PGMC4", ], pairwise.wilcox.test(Value, Group, p.adjust.method = "none")))
#  data preparation 
CN2 <- do.call(rbind.data.frame, CN)
colnames(CN2) <- colnames(pv)[-3]
# subset the pvalues, by merging the CN list
pv_final <- merge(CN2, pv, by.x = c("group2", "group1"), by.y = c("group1", "group2"))
# fix ordering
pv_final <- pv_final[order(pv_final$group1), ] 
# set signif level
pv_final$map_signif <- ifelse(pv_final$p.value > 0.05, "", ifelse(pv_final$p.value > 0.01,"*", "**"))  

# the plot
ggplot(mydf, aes(x=Group, y=Value, fill=Group)) + geom_boxplot() +
  stat_compare_means(data=mydf[ mydf$Group != "PGMC4", ], aes(x=Group, y=Value, fill=Group), size=5) + 
  ylim(-4,30)+
  geom_signif(comparisons=CN,
              y_position = 3:30, annotation= pv_final$map_signif) + 
  theme_bw(base_size = 16)

enter image description here

参数vjusttextsizesize 无法正常工作。似乎是最新版本 ggsignif_0.3.0 中的一个错误。

<小时/>

编辑:当您只想显示显着的比较时,您可以轻松地对数据集 CN 进行子集化。由于我更新到 ggsignif_0.4.0R 版本 3.4.1vjusttextsize 现在可以按预期工作。您可以尝试 step_increase,而不是 y_position

# subset 
gr <- pv_final$p.value <= 0.05
CN[gr]

ggplot(mydf, aes(x=Group, y=Value, fill=Group)) + 
  geom_boxplot() +
  stat_compare_means(data=mydf[ mydf$Group != "PGMC4", ], aes(x=Group, y=Value, fill=Group), size=5) + 
  geom_signif(comparisons=CN[gr], textsize = 12, vjust = 0.7, 
             step_increase=0.12, annotation= pv_final$map_signif[gr]) + 
  theme_bw(base_size = 16)

您也可以使用 ggpubr。添加:

stat_compare_means(comparisons=CN[gr], method="wilcox.test", label="p.signif", color="red")

enter image description here

关于R ggplot2 箱线图 - ggpubr stat_compare_means 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45476950/

相关文章:

R 示例 - ddply、ave 和合并

r - 我需要 R 在找到条件 = T 时开始过滤行

r - facet_zoom 无法更改缩放图的中断

r - 控制ggplot2图异常(exception)观而不影响绘图

d3.js - 在 nvd3 箱线图上禁用 y 轴最大值和最小值

R - 采样,同时仍保持某些分类变量的相同比例

Rattle R 未加载数据

r - 将 grid.arrange 分配给对象

r - 分组箱线图 r ggplot2

python - 分组后如何排列seaborn swarmplot和各自箱线图中的点