You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多代谢物条形图中t检验显著性标记显示异常问题

问题:条形图中t检验显著性符号无法正确定位到对应剂量组上方

我在绘制包含多种代谢物的条形图时,无法正确展示t检验的显著性值。当前输出的图里显著性符号位置不符合需求,我需要让3个显著性符号直接显示在每个代谢物的对应剂量组(条形)上方,但调整参数和对比项都没法移动符号位置。

原代码

my_comparisons <- list(c("0", "50"), c("0", "100"), c("0", "150"))

lr1to13p <- ggplot(lr_data_long_1to13, aes(x=Metabolite, y=Value, fill=factor(CSE.Dose))) +
    geom_bar(stat="summary", fun="mean", position=position_dodge(width=0.8), alpha=0.7) +
    geom_point(aes(color=factor(CSE.Dose)), position=position_dodge(width=0.8), size=2) +
    theme_minimal() +
    labs(x="Metabolite", y="Relative Expression", fill="CSE.Dose", color="CSE.Dose") +
    theme(axis.text.x = element_text(angle=45, hjust=1)) +
    stat_compare_means(comparisons = my_comparisons) +
    stat_compare_means(method = "t.test", paired = T, label = "p.signif", vjust = 1) 

问题分析

当前代码存在两个核心问题:

  • 重复调用stat_compare_means,第一个无分组指定的调用会导致显著性符号默认显示在全局位置;
  • 未在全局映射中指定group参数,也未配合position_dodge让符号与对应条形对齐。

修正后的代码

my_comparisons <- list(c("0", "50"), c("0", "100"), c("0", "150"))

lr1to13p <- ggplot(lr_data_long_1to13, aes(x=Metabolite, y=Value, fill=factor(CSE.Dose), group=factor(CSE.Dose))) +
    geom_bar(stat="summary", fun="mean", position=position_dodge(width=0.8), alpha=0.7) +
    geom_point(aes(color=factor(CSE.Dose)), position=position_dodge(width=0.8), size=2) +
    theme_minimal() +
    labs(x="Metabolite", y="Relative Expression", fill="CSE.Dose", color="CSE.Dose") +
    theme(axis.text.x = element_text(angle=45, hjust=1)) +
    stat_compare_means(
        comparisons = my_comparisons,
        method = "t.test",
        paired = TRUE,
        label = "p.signif",
        position = position_dodge(width=0.8),  # 与条形的dodge宽度匹配,实现对齐
        vjust = -0.5  # 调整符号与条形顶部的距离,避免重叠
    )

关键调整说明

  • 在全局aes中添加group=factor(CSE.Dose),明确分组对比的依据;
  • 合并stat_compare_means调用,避免重复绘制无效的显著性符号;
  • 设置position=position_dodge(width=0.8),让符号与对应剂量组的条形精准对齐;
  • 通过vjust参数调整符号纵向位置,确保显示在条形上方且不重叠。

内容的提问来源于stack exchange,提问作者Johnson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 09:22:35