单因素ANOVA后,如何在箱线图展示与TukeyHSD一致的p值?
问题:如何让箱线图展示的p值与Tukey检验结果完全一致?
我已经完成了单因素ANOVA和Tukey事后检验,但在把Tukey检验的p值添加到箱线图时,发现图上显示的p值和我的检验结果对不上。下面是我用到的包、分析代码、Tukey检验结果,还有绘图的代码,想请教怎么调整才能让箱线图上的p值和Tukey的结果一致?
用到的R包与ANOVA、Tukey检验代码
library(ggplot2) library(ggpubr) library(rstatix) CR.anova <- aov(CR ~ Group, data = df) summary(CR.anova) tukey_hsd(CR.anova)
Tukey检验结果
term group1 group2 estimate conf.low conf.high p.adj p.adj.signif 1 Group K02 K12 -1.90 -3.14 -0.660 0.00105 ** 2 Group K02 BASELINE 0.153 -1.06 1.37 0.987 ns 3 Group K02 PLACEBO 0.239 -1.00 1.48 0.955 ns 4 Group K12 BASELINE 2.05 0.838 3.27 0.000286 *** 5 Group K12 PLACEBO 2.14 0.898 3.38 0.000215 *** 6 Group BASELINE PLACEBO 0.0861 -1.13 1.30 0.998 ns
箱线图绘制代码
ggboxplot( Metabolite.data, x= "Group", y= "CRP", color = "black", title = "CRP", ylab = "Normalized value", rug = TRUE, legend = "right", font.label = list(size = 40, face = "bold"), linetype = "solid", size = 1.1, palette = c("#cc3300","#E7B800","#99ccff", "#003366"), fill= "Group", order = c("BASELINE", "PLACEBO", "K02","K12"), bxp.errorbar= TRUE, alpha= 0.6 ) + border(color = "black", size= 2)+ geom_jitter(aes(fill= Group),shape = 21, size = 4,color = "black", stroke= 1, position = position_jitter()) + theme(plot.title = element_text(hjust=0.5 , face = "bold", size = 20 ), axis.text = element_text(face = "bold"))+ theme(axis.title.x=element_blank())+ theme(axis.title.y = element_text(face = "bold", size= 16 ))+ theme(legend.position = "none")+ stat_boxplot(geom ="errorbar",size= 1.1, width= 0.4)+ stat_compare_means(comparisons = CRP_comparisons)
解决方法:手动导入Tukey检验结果进行标注
问题核心在于stat_compare_means()默认不会调用你已经计算好的Tukey检验p值,它会自动执行其他事后检验(比如配对t检验或Wilcoxon检验),所以才会出现结果不一致的情况。我们可以用stat_pvalue_manual()直接导入Tukey的结果来标注,步骤如下:
- 整理Tukey检验结果为标注所需的格式
# 提取并保存Tukey检验结果 tukey_results <- tukey_hsd(CR.anova) # 创建包含对比组、p值、显著性标记的标注数据框,y值需要根据你的数据范围调整(避免标注重叠) tukey_p_df <- data.frame( group1 = tukey_results$group1, group2 = tukey_results$group2, p = tukey_results$p.adj, p.signif = tukey_results$p.adj.signif, y = c(15, 16, 17, 18, 19, 20) # 这里的数值请根据你的箱线图y轴最大值调整 )
- 修改绘图代码,替换
stat_compare_means()为stat_pvalue_manual()
ggboxplot( Metabolite.data, x= "Group", y= "CRP", color = "black", title = "CRP", ylab = "Normalized value", rug = TRUE, legend = "right", font.label = list(size = 40, face = "bold"), linetype = "solid", size = 1.1, palette = c("#cc3300","#E7B800","#99ccff", "#003366"), fill= "Group", order = c("BASELINE", "PLACEBO", "K02","K12"), bxp.errorbar= TRUE, alpha= 0.6 ) + border(color = "black", size= 2)+ geom_jitter(aes(fill= Group),shape = 21, size = 4,color = "black", stroke= 1, position = position_jitter()) + theme(plot.title = element_text(hjust=0.5 , face = "bold", size = 20 ), axis.text = element_text(face = "bold"))+ theme(axis.title.x=element_blank())+ theme(axis.title.y = element_text(face = "bold", size= 16 ))+ theme(legend.position = "none")+ stat_boxplot(geom ="errorbar",size= 1.1, width= 0.4)+ # 手动添加Tukey检验的p值标注 stat_pvalue_manual( tukey_p_df, x = "group1", xend = "group2", y = "y", label = "p = {round(p, 6)} ({p.signif})", # 可以自定义显示格式,只显示标记就写"{p.signif}" tip.length = 0.01, size = 4 )
额外提示
- 如果只需要显示显著性标记(比如***、**、ns),把
label参数改成"{p.signif}"即可,图表会更简洁。 y列的数值一定要根据你的实际数据调整,确保标注线不会和箱线、散点重叠,如果有重叠可以适当增大数值。
内容的提问来源于stack exchange,提问作者suad alshammari
相关产品推荐
相关产品推荐

