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

如何为ggplot点须图添加基于数据框P值的显著性星号

为dotwhisker点须图添加显著性星号

要基于数据框中的P值给点须图添加对应星号(P<0.01显示**,P<0.05显示*),可以按以下步骤操作:

1. 生成显著性标记列

首先给合并后的模型数据框m4新增一列,根据P值规则生成对应的星号:

m4 <- m4 %>%
  mutate(
    sig = case_when(
      p.value < 0.01 ~ "**",
      p.value < 0.05 ~ "*",
      TRUE ~ ""
    )
  )

2. 在绘图中添加星号文本层

在原有的small_multiple绘图代码后,添加geom_text图层,把星号放置在对应系数点的上方(可通过偏移量调整位置):

small_multiple(m4, dot_args = list(aes(shape = term, col = term)),
               whisker_args = list(aes(linetype = term))
) +
  theme_bw(base_size = 20) + 
  ylab("") +
  geom_hline(yintercept = 0,
             colour = "grey60",
             linetype = 2) +
  # 添加显著性星号
  geom_text(aes(x = model, y = estimate, label = sig, group = term),
            nudge_y = 0.2,  # 调整星号与点的垂直距离,按需修改
            size = 6,       # 星号字号
            show.legend = FALSE) +
  theme(
    legend.position = "none",
    axis.text.x = element_text(angle = 60, hjust = 1)
  )

小提示

  • 如果星号位置不合适,可修改nudge_y的数值,或者直接设置y = estimate + 0.3这类固定偏移值;
  • group = term参数是确保每个自变量的星号能对应到正确的系数点,避免错位。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 11:45:17