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

R ggplot图表添加显著性星号遇报错,请求代码排查

组间显著性星号添加失败问题排查

我是R语言新手,制作海报图表时无法为组间添加显著性星号,反复出现以下警告:

Warning: [38;5;232mRemoved 512 rows containing non-finite values (stat_summary()).[39m
Warning: [38;5;232mRemoved 512 rows containing non-finite values (stat_signif()).[39m
Warning: [38;5;232mComputation failed in stat_signif()[39m

使用的代码如下:

eye_data_subset <- eye_data2 %>%
  filter(Fixation_by_trial %in% c("FixationCount_1", "FixationCount_4"))
  desired_group_order <- c("YA", "Healthy OA", "MCI")

eye_data_subset$Group <- factor(eye_data_subset$Group, levels = desired_group_order)

ggplot(eye_data_subset, aes(x = Fixation_by_trial, y = Eye_Fixation_Count_Total, fill = Group)) +
  geom_bar(stat = "summary", fun = "mean", position = "dodge", width = 0.5) + 
  ylim(0, 15) +
  geom_signif(comparisons = list(c("YA", "Healthy OA"), c("YA", "MCI"), c("Healthy OA", "MCI")), 
            annotations = "***", y_position = 14, tip_length = 0.2, vjust = 0.5) +
  labs(title = "Fixation Bias for Visual Conditions With Interference vs No Interference", x = "Visual Conditions", y = "Mean Fixation (ms)") +
  theme_minimal() +
  scale_x_discrete(labels = c("FixationCount_1" = "V+C+, V-C+", "FixationCount_4" = "V+C-, V-C-")) +
  scale_fill_manual(values = c("YA" = "maroon", "Healthy OA" = "gold", "MCI" ="gray"))

问题原因及修正方案

1. 数据缺失值导致行丢失

警告中的non-finite values表示Eye_Fixation_Count_Total列存在NA、无穷值等无效数据,需先清理:

# 在数据过滤后添加缺失值移除步骤
eye_data_subset <- eye_data2 %>%
  filter(Fixation_by_trial %in% c("FixationCount_1", "FixationCount_4")) %>%
  drop_na(Eye_Fixation_Count_Total)

2. geom_signif参数匹配错误

图表按Fixation_by_trial分x轴,Group作为填充分组,但geom_signif默认针对x轴类别做比较,直接传入Group分组会导致无法识别比较对象,需指定group参数并对齐分组位置。

修正后的完整代码

eye_data_subset <- eye_data2 %>%
  filter(Fixation_by_trial %in% c("FixationCount_1", "FixationCount_4")) %>%
  drop_na(Eye_Fixation_Count_Total)  # 清理缺失值

desired_group_order <- c("YA", "Healthy OA", "MCI")
eye_data_subset$Group <- factor(eye_data_subset$Group, levels = desired_group_order)

ggplot(eye_data_subset, aes(x = Fixation_by_trial, y = Eye_Fixation_Count_Total, fill = Group)) +
  geom_bar(stat = "summary", fun = "mean", position = position_dodge(width = 0.5), width = 0.5) + 
  ylim(0, 15) +
  geom_signif(
    aes(group = Group),  # 指定比较的分组为Group
    comparisons = list(c("YA", "Healthy OA"), c("YA", "MCI"), c("Healthy OA", "MCI")),
    annotations = c("***", "***", "ns"),  # 根据实际显著性调整标注,示例用***和无显著性ns
    y_position = c(12, 13, 14),  # 不同比较设置不同高度,避免重叠
    tip_length = 0.05,
    position = position_dodge(width = 0.5),  # 和条形图的分组对齐
    test = "t.test"  # 如需自动计算显著性,指定检验方法;若手动标注可设为NULL
  ) +
  labs(title = "Fixation Bias for Visual Conditions With Interference vs No Interference", 
       x = "Visual Conditions", 
       y = "Mean Fixation (ms)") +
  theme_minimal() +
  scale_x_discrete(labels = c("FixationCount_1" = "V+C+, V-C+", "FixationCount_4" = "V+C-, V-C-")) +
  scale_fill_manual(values = c("YA" = "maroon", "Healthy OA" = "gold", "MCI" = "gray"))

额外提示

  • 若已提前算出显著性结果,无需geom_signif自动计算,可设置test = NULL,直接用annotations传入对应星号。
  • position_dodge的宽度要和geom_bar的width参数匹配,确保星号和对应条形对齐。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 04:37:12