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

如何在箱线图中添加Tukey检验比较标注(SAS/R实现)

实现方法

R 语言(基于ggplot2)

方法1:用ggsignif包直接关联Tukey检验结果

先安装并加载所需工具包,再基于你的数据生成箱线图并添加显著差异标注:

# 首次使用安装包
install.packages(c("ggplot2", "ggsignif", "car"))

# 加载包
library(ggplot2)
library(ggsignif)
library(car)

# 示例数据(替换为你的真实数据)
set.seed(123)
data <- data.frame(
  Group = rep(c("A", "B", "C", "D"), each = 20),
  Value = c(rnorm(20, 5), rnorm(20, 7), rnorm(20, 5.5), rnorm(20, 8))
)

# 生成箱线图并添加Tukey显著标注
ggplot(data, aes(x = Group, y = Value)) +
  geom_boxplot(fill = "lightblue") +
  # 替换comparisons为你Tukey检验得到的显著组对
  geom_signif(
    comparisons = list(c("A", "B"), c("B", "D"), c("A", "D")),
    test = "tukey",
    map_signif_level = TRUE, # 自动用星号对应p值(*<0.05, **<0.01, ***<0.001)
    y_position = c(9, 9.5, 10) # 每组对比的标注高度,按需调整
  ) +
  labs(title = "箱线图带Tukey差异标注", x = "组别", y = "观测值")

方法2:手动导入预计算的Tukey结果

如果已经有整理好的显著组对和对应p值,可直接指定标注内容:

# 先获取Tukey检验结果(你已完成,这里仅示例)
anova_result <- aov(Value ~ Group, data = data)
tukey_result <- TukeyHSD(anova_result)
sig_pairs <- subset(as.data.frame(tukey_result$Group), p adj < 0.05)

# 手动添加标注
ggplot(data, aes(x = Group, y = Value)) +
  geom_boxplot(fill = "lightblue") +
  geom_signif(
    xmin = c(1,2,1), # 对应组的x轴索引(A=1, B=2, C=3, D=4)
    xmax = c(2,4,4),
    annotations = c("*", "***", "**"), # 对应p值的星号
    y_position = c(9,9.5,10),
    tip_length = 0.01 # 标注线尖端长度
  )

SAS 语言(基于PROC SGPLOT)

SAS需要先预处理Tukey检验的显著组对,计算标注位置参数,再通过SGANNO数据集添加线条和文本:

步骤1:提取显著组对并计算标注参数

/* 示例数据(替换为你的真实数据)*/
data test_data;
  do Group = "A", "B", "C", "D";
    do i = 1 to 20;
      if Group = "A" then Value = rand("NORMAL", 5, 1);
      else if Group = "B" then Value = rand("NORMAL", 7, 1);
      else if Group = "C" then Value = rand("NORMAL", 5.5, 1);
      else Value = rand("NORMAL", 8, 1);
      output;
    end;
  end;
run;

/* 做ANOVA和Tukey检验,输出组间对比结果 */
proc glm data=test_data;
  class Group;
  model Value = Group;
  lsmeans Group / adjust=tukey pdiff=all out=tukey_results;
run;
quit;

/* 筛选p<0.05的显著组对,计算标注位置 */
data sig_pairs;
  set tukey_results;
  where probpdiff < 0.05;
  /* 给组别分配x轴坐标 */
  if Group = "A" then x1 = 1;
  else if Group = "B" then x1 = 2;
  else if Group = "C" then x1 = 3;
  else x1 = 4;
  if Group = "A" then x2 = 1;
  else if Group = "B" then x2 = 2;
  else if Group = "C" then x2 = 3;
  else x2 = 4;
  /* 获取观测最大值,计算标注线高度 */
  proc sql noprint;
    select max(Value) into :max_val from test_data;
  quit;
  y_line = &max_val + 0.5 + (_n_ * 0.3); /* 错开每组标注的高度 */
  /* 根据p值分配星号 */
  if probpdiff < 0.001 then star = "***";
  else if probpdiff < 0.01 then star = "**";
  else if probpdiff < 0.05 then star = "*";
run;

/* 创建SGANNO数据集,定义标注元素 */
data sganno;
  set sig_pairs;
  /* 绘制水平标注线 */
  function = "LINE";
  x1 = x1;
  y1 = y_line;
  x2 = x2;
  y2 = y_line;
  linecolor = "BLACK";
  linewidth = 1;
  output;
  /* 绘制两端竖线 */
  function = "LINE";
  x1 = x1;
  y1 = y_line - 0.2;
  x2 = x1;
  y2 = y_line;
  output;
  function = "LINE";
  x1 = x2;
  y1 = y_line - 0.2;
  x2 = x2;
  y2 = y_line;
  output;
  /* 添加星号文本 */
  function = "TEXT";
  x = (x1 + x2)/2; /* 文本居中 */
  y = y_line + 0.1;
  text = star;
  textcolor = "BLACK";
  textsize = 12;
  anchor = "CENTER";
  output;
run;

步骤2:生成带标注的箱线图

proc sgplot data=test_data sganno=sganno;
  vbox Value / category=Group fillattrs=(color=lightblue);
  xaxis label="组别";
  yaxis label="观测值" max=&max_val + 2; /* 预留标注空间 */
  title "箱线图带Tukey差异标注";
run;
quit;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 11:45:10