【问题标题】:R ggplot2 boxplots - ggpubr stat_compare_means not working properlyR ggplot2 boxplots - ggpubr stat_compare_means 无法正常工作
【发布时间】:2018-01-10 15:07:33
【问题描述】:

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

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

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

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

请在下方查看我的 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 将生成以下箱线图:

问题是:

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 值计算。

谢谢!

【问题讨论】:

  • 1.确实是的。似乎是一个错误; 2. 不知道; 3. 使用stat_compare_means(data=mydf[ mydf$Group != "PGMC4", ],aes(x=Group, y=Value, fill=Group), size=5) 4. 将结果与pairwise.wilcox.test(mydf$Value, mydf$Group, p.adjust.method = "none")进行比较
  • 我应该如何通知这个错误?你知道它是否有可能很快得到解决?非常感谢!
  • 自己绘制所有的东西,例如使用library(ggsignif);geom_signif() 和注释。看这里最后一个答案:stackoverflow.com/questions/17084566/…
  • 你能把它发展成一个答案吗?使用 ggsignif 的 geom_signif() 我似乎不知道如何删除 NS 比较,如何更改 ** 的大小,以及如何放置括号以使它们不重叠(就像 ggpubr 一样)...所以我在同一点
  • 请任何人都可以在这里阐明一下吗?非常感谢!

标签: r ggplot2 boxplot p-value ggpubr


【解决方案1】:

编辑:因为我发现了 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)

老答案:

您可以尝试关注。这个想法是您自己使用pairwise.wilcox.test 计算统计数据。然后你使用ggsignif函数geom_signif 添加预先计算的 pvalues。使用 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)

参数vjusttextsizesize 无法正常工作。好像是最新版的bugggsignif_0.3.0


编辑:当您只想显示重要的比较时,您可以轻松地对数据集 CN 进行子集化。由于我更新为ggsignif_0.4.0R version 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")

【讨论】:

  • 嗨,Jimbou,感谢您的建议,但我的目标不仅仅是删除实际的字母“NS”,还有所有那些使情节非常繁忙并挤压箱线图的无用括号。 ..
  • 我升级到 ggsignif_0.4.0 但我仍然无法更改 *** 的大小
  • "textsize" 和 "size" 不起作用,"cex" 改变括号的粗细...
  • 我正在使用 geom_signif 和 ggsignif_0.4.0,而不是 ggpubr...无法更改 * 大小...
  • 哦,我知道了!即使您不在实际情节中使用它,似乎加载 ggpubr 也会搞砸......让我消化所有这些并接受答案
猜你喜欢
  • 2021-07-04
  • 1970-01-01
  • 2018-01-16
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 2016-12-01
  • 2021-10-31
  • 2020-06-30
相关资源
最近更新 更多