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

ggbetweenstats设置对数Y轴后分组分析结果消失求助

解决ggbetweenstats添加对数刻度后组间比较标记消失的问题

问题背景

我使用Kruskal-Wallis检验分析三组测量数据的统计显著性,借助ggbetweenstats工具展示组间的显著关联。初始绘图可以正常显示Kruskal-Wallis检验结果,以及组间两两比较的显著性标记,但在为Y轴添加伪对数刻度后,组间分析的标记完全消失。

样本数据

sampledata <- structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
                                    13, 14, 15, 16, 17, 18, 19, 20), group = c(1, 2, 3, 1, 2, 3, 
                                                                               1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2), measurement = c(0, 
                                                                                                                                          1, 200, 30, 1000, 6000, 1, 0, 0, 10000, 20000, 700, 65, 1, 8, 
                                                                                                                                          11000, 13000, 7000, 500, 3000)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                               20L))

初始正常绘图代码

library(ggstatsplot)
library(ggplot2)

ggbetweenstats(
 data = sampledata,
 x = group,
 y = measurement,
 type = "nonparametric",
 plot.type = "box",
 pairwise.comparisons = TRUE,
 pairwise.display = "all",
 centrality.plotting = FALSE,
 bf.message = FALSE
) 

导致问题的代码(添加伪对数刻度)

ggbetweenstats(
  data = sampledata,
  x = group,
  y = measurement,
  type = "nonparametric",
  plot.type = "box",
  pairwise.comparisons = TRUE,
  pairwise.display = "all",
  centrality.plotting = FALSE,
  bf.message = FALSE
) +
  ggplot2::scale_y_continuous(trans=scales::pseudo_log_trans(sigma = 1, base = exp(1)), limits = c(0,25000), breaks = c(0,1,10,100,1000,10000)
)

问题原因

使用scale_y_continuous事后修改轴转换时,ggbetweenstats生成的组间比较显著性标记是基于原始Y轴坐标计算的,不会同步跟随轴转换更新位置,最终导致标记超出可视范围或完全消失。

解决方法

方法一:使用coord_trans替代scale_y_continuous

coord_trans会对整个绘图(包括显著性标记)应用坐标转换,确保标记位置与转换后的轴匹配:

library(ggstatsplot)
library(ggplot2)
library(scales)

ggbetweenstats(
  data = sampledata,
  x = group,
  y = measurement,
  type = "nonparametric",
  plot.type = "box",
  pairwise.comparisons = TRUE,
  pairwise.display = "all",
  centrality.plotting = FALSE,
  bf.message = FALSE
) +
  coord_trans(y = pseudo_log_trans(sigma = 1, base = exp(1))) +
  scale_y_continuous(limits = c(0, 25000), breaks = c(0, 1, 10, 100, 1000, 10000))

方法二:预先转换数据并指定统计检验用原始数据

由于Kruskal-Wallis是秩和检验,数据的单调转换(如伪对数转换)不会改变检验结果,因此可以先转换数据,再绘图时指定统计检验使用原始数据:

library(ggstatsplot)
library(ggplot2)
library(scales)

# 生成伪对数转换后的变量
sampledata$meas_plog <- pseudo_log_trans(sigma = 1, base = exp(1))(sampledata$measurement)

ggbetweenstats(
  data = sampledata,
  x = group,
  y = measurement,  # 统计检验基于原始数据
  type = "nonparametric",
  plot.type = "box",
  pairwise.comparisons = TRUE,
  pairwise.display = "all",
  centrality.plotting = FALSE,
  bf.message = FALSE,
  ggplot.component = list(
    scale_y_continuous(
      trans = pseudo_log_trans(sigma = 1, base = exp(1)),
      limits = c(0, 25000),
      breaks = c(0, 1, 10, 100, 1000, 10000)
    )
  )
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 04:15:28