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

Dan*_*Cee 5 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值?如果是后者,调整方法是什么?BH?

请在下面查看我的MWE,此链接以及其他链接以供参考

##############################
##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()
##############################
Run Code Online (Sandbox Code Playgroud)

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")
Run Code Online (Sandbox Code Playgroud)

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

wilcox.test(Value ~ Group, data=mydf.sub)$p.value
Run Code Online (Sandbox Code Playgroud)

其中,mydf.sub是对2组进行给定比较的mydf的subset()。

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

编辑2

请帮忙,解决方案不必是ggpubr(但必须是ggplot2),我只需要能够隐藏NS并使星号的大小变大,并且p值计算相同到wilcox.test()+ p.adjust(方法“ BH”)。

谢谢!

Rom*_*man 7

您可以尝试关注。想法是,您可以使用自己计算统计信息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)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

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


编辑:当您只想显示重要的比较时,可以轻松地对数据集进行子集化CN。由于我更新到ggsignif_0.4.0R version 3.4.1vjust并且textsize现在可以按预期工作。代替y_position您可以尝试step_increase

# 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)
Run Code Online (Sandbox Code Playgroud)

您也可以使用ggpubr。加:

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

在此处输入图片说明