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

ggboxplot中stat_pvalue_manual的p值线超出图界问题求助

问题:ggpubr绘制箱线图时p值线超出图表范围

使用ggpubr包的ggboxplot结合stat_pvalue_manual绘制箱线图时,部分数据集出现p值显著性线超出图界的问题,另一组数据集则显示正常。相同代码仅在特定组对比时触发异常,需调整参数解决。

解决方案

该问题源于add_xy_position默认基于四分位数计算的p值线y轴位置过高,超出了图表默认y轴范围。可以通过以下两种核心方式解决:

方法1:自定义p值线的y轴位置

在add_xy_position中通过fun参数手动指定y轴位置的计算逻辑,比如取每组数据的最大值加上合适的偏移量,替代默认的自动计算:

t1c2_.5_all.pw <- t1c2_.5_all.pw %>% 
  add_xy_position(
    x = "Type",
    fun = function(x) max(x, na.rm = TRUE) + 200  # 偏移量可根据数据实际情况调整
  )

方法2:扩展y轴显示范围

通过scale_y_continuous扩展y轴顶部的空间,为p值线预留显示区域:

scale_y_continuous(expand = expansion(mult = c(0, 0.1)))  # 0.1表示顶部扩展10%空间
修改后的完整代码
library(tidyverse)
library(ggpubr)
library(rstatix)

# 源数据
t1c2_.5_all<-structure(list(Type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L), levels = c("T1", "C1", "C2", "C3"), class = "factor"), Year = c(2004, 
2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 
2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2004, 2005, 2006, 
2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 
2018, 2019, 2020, 2021, 2022, 2023), buff_dist = c(0.5, 0.5, 
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5), 
    NDVI_Avg3 = c(NA, 6395.874774, 6567.268516, 6625.956351, 
    6396.39154166667, 6329.32849466667, 6327.09833166667, 6431.656085, 
    6376.82626833333, 6399.35978466667, 6457.27028166667, 6530.29056866667, 
    6504.67240266667, 6665.66358833333, 6590.31482566667, 6680.14639133333, 
    6509.14329666667, 6504.518513, 6448.14020766667, NA, NA, 
    4509.88554533333, 4563.733277, 4755.027259, 4346.68286633333, 
    4471.81173166667, 4241.25849033333, 4387.160754, 4318.91640866667, 
    4412.97042333333, 4442.207563, 4346.34542466667, 4386.22608033333, 
    4727.431841, 4709.10905166667, 4936.07561766667, 4694.84953633333, 
    4616.632718, 4293.052728, NA)), row.names = c(NA, -40L), class = c("tbl_df", 
"tbl", "data.frame"))

# 配对检验
t1c2_.5_all.pw <- t1c2_.5_all %>% pairwise_t_test(NDVI_Avg3 ~ Type, p.adjust.method = "bonferroni")

# 自定义p值线y轴位置
t1c2_.5_all.pw <- t1c2_.5_all.pw %>% 
  add_xy_position(
    x = "Type",
    fun = function(x) max(x, na.rm = TRUE) + 200
  )

# 绘制箱线图并扩展y轴范围
t1c2_.5_all.box <- ggboxplot(t1c2_.5_all, x = "Type", y = "NDVI_Avg3") +
  ggtitle(label = "PWP Watersheds (all.5yr) - All Years ") +
  stat_pvalue_manual(t1c2_.5_all.pw, label = "p.adj.signif", tip.length = 0, step.increase = 0.1) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
  labs(
    subtitle = get_test_label(t1c2_.5_all.pw, detailed = TRUE),
    caption = get_pwc_label(t1c2_.5_all.pw)
  ) 

t1c2_.5_all.box
ggsave("t1c2_.5km_allyears_boxplot.png", width=6, height=5)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 14:49:52