本文介绍了R ggplot2 boxplots - ggpubr stat_compare_means无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我尝试使用 ggplot2 和 ggpubr 包以星号的形式将重要性级别添加到我的箱型图中,但我有很多比较和我只想显示重要的。 我尝试使用选项 hide.ns = TRUE stat_compare_means 中,但很明显不起作用,它可能是 ggpubr 包中的一个错误。 另外,您看到我从成对的 wilcox.test 比较中省略了组PGMC4;我怎么能把这个小组也留给 kruskal.test ? 最后一个问题是重要程度如何工作?由于*在0.05以下显着,**在0.025以下,***在0.01以下ggpubr使用的是什么?它是显示p值还是调整后的p值?如果是后者,调整方法是什么? BH? 请检查我的MWE以下并 问题是: 1-如何让hide.ns = TRUE工作? 2-如何增加*的大小? 3-如何从kruskal.test比较中排除组? 4- ggpubr使用的*约定是什么,p值是否显示已调整? 非常感谢! ! 编辑 此外,在做 stat_compare_means(比较= my.comps,method =wilcox.test,p.adjust.method =BH) 我没有获得与 wilcox.test(Value〜Group,data = mydf.sub)$ p.value 其中mydf.sub是mydf的子集()给出了2组的比较。 ggpubr在这里做什么?如何计算p.values? 编辑2 请帮忙,该解决方案不必与ggpubr(但它必须与 ggplot2 ),我只需要能够隐藏NS,并使星号的大小,以及一个p值计算与wilcox.test()+ p.adjust(方法BH)完全相同。 谢谢!解决方案您可以尝试以下操作。这个想法是,您可以使用 pairwise.wilcox.test 来计算您自己的统计数据。然后使用 ggsignif 函数 geom_signif 添加预先计算的pvalues。使用 y_position ,您可以放置括号,使它们不重叠。 图书馆(tidyverse)图书馆(ggsignif)图书馆(扫帚)#您的您想比较的组合列表 CN #pvalues。我使用扫帚和整齐,以获得一个不错的格式化数据框。请注意,我关闭了对pvalues的调整。 pv #数据准备 CN2 colnames(CN2)< - colnames(pv)[ - 3] #subset通过合并CN列表 pv_final< - merge(CN2,pv,by.x = c(group2,group1),by.y = c(group1,group2) )#修复订单 pv_final< - pv_final [order(pv_final $ group1),] #set signif level pv_final $ map_signif< - ifelse(pv_final $ p。值> 0.05,,ifelse(pv_final $ p.value> 0.01,*,**)) #plot ggplot(mydf,aes( x =组,y =值,填充=组) = Group),size = 5)+ ylim(-4,30)+ geom_signif(比较= CN, y_position = 3:30,annotation = pv_final $ map_signif)+ theme_bw(base_size = 16) 参数 vjust , textsize 和 size 不能正常工作。似乎是最新版本 ggsignif_0.3.0 中的错误。 编辑:只显示重要的比较,您可以轻松地对数据集进行子集化。 vjust 和 textsize 正在更新为 ggsignif_0.4.0 和 R版本3.4.1 。您可以尝试 step_increase y_position p> #subset gr 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 = (比较= 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(比较= CN [gr],method =wilcox.test,label =p.signif ,color =red) p> I am trying to add significance levels to my boxplots in the form of asterisks using ggplot2 and the ggpubr package, but I have many comparisons and I only want to show the significant ones.I try to use the option hide.ns=TRUE in stat_compare_means, but it clearly does not work, it might be a bug in the ggpubr package.Besides, you see that I leave out group "PGMC4" from the pairwise wilcox.test comparisons; how can I leave this group out also for the kruskal.test?The last question I have is how the significance level works? As in * is significant below 0.05, ** below 0.025, *** below 0.01? what is the convention ggpubr uses? Is it showing p-values or adjusted p-values? If the latter, what's the adjusting method? BH?Please check my MWE below and this link and this other one for reference################################MWEset.seed(5)#test dfmydf <- 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 samplegroups <- as.character(unique(mydf$Group[which(mydf$Group!="PGMC4")]))#function to make combinations of groups without repeating pairs, and avoiding self-combinationsexpand.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 comparecombs <- as.data.frame(expand.grid.unique(groups, groups), stringsAsFactors=FALSE)head(combs)my.comps <- as.data.frame(t(combs), stringsAsFactors=FALSE)colnames(my.comps) <- NULLrownames(my.comps) <- NULL#pairs I want to compare in list format for stat_compare_meansmy.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()##############################The MWE will produce the following boxplots:The questions would be:1- How to make hide.ns=TRUE work?2- How to increase the size of the *?3- How to exclude a group from the kruskal.test comparison?4- What is the * convention used by ggpubr, and are the p-values shown adjusted or not?Many thanks!!EDITBesides, when doingstat_compare_means(comparisons=my.comps, method="wilcox.test", p.adjust.method="BH")I do not obtain the same p-values as when doingwilcox.test(Value ~ Group, data=mydf.sub)$p.valuewhere mydf.sub is a subset() of mydf for a given comparison of 2 groups.What is ggpubr doing here? How does it calculate the p.values?EDIT 2Please help, the solution does not have to be with ggpubr (but it has to be with ggplot2), I just need to be able to hide the NS and make the size of the asterisks bigger, as well as a p-value calculation identical to wilcox.test() + p.adjust(method"BH").Thanks! 解决方案 You can try following. The idea is that you calculate the stats by your own using pairwise.wilcox.test. Then you use the ggsignif function geom_signifto add the precalculated pvalues. With y_position you can place the brackets so they don't overlap. library(tidyverse)library(ggsignif)library(broom)# your list of combinations you want to compareCN <- 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 listpv_final <- merge(CN2, pv, by.x = c("group2", "group1"), by.y = c("group1", "group2"))# fix orderingpv_final <- pv_final[order(pv_final$group1), ] # set signif levelpv_final$map_signif <- ifelse(pv_final$p.value > 0.05, "", ifelse(pv_final$p.value > 0.01,"*", "**")) # the plotggplot(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)The arguments vjust, textsize, and size are not properly working. Seems to be a bug in the latest version ggsignif_0.3.0. Edit: To show only the significant comparisons, you easily can subset the datasets. vjust and textsize are working now as I updated to ggsignif_0.4.0 and R version 3.4.1. Instead of y_position you can als try step_increase# subset gr <- pv_final$p.value <= 0.05CN[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)You can use ggpubr as well. Add:stat_compare_means(comparisons=CN[gr], method="wilcox.test", label="p.signif", color="red") 这篇关于R ggplot2 boxplots - ggpubr stat_compare_means无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-20 22:04