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

亚组森林图格式问题求助:含代码与可视化异常

亚组森林图格式问题解决方案

针对绘制亚组森林图时遇到的三个格式问题,以下是具体解决方法及修改后的完整代码:


问题1:Odds Ratio估计值偏移,无法居中对齐

问题根源

  • 原始数据中CIHigh和CILow的数值完全颠倒(正常逻辑应为CILow < Odds < CIHigh),导致置信区间线条方向错误;
  • Model列全为NA,分组失效,同时geom_text的位置设置未与点、置信区间的position_dodge参数对齐。

修改方案

  1. 修正Model列的分组值,按行分配对应亚组;
  2. 在geom_linerange中用pmin()和pmax()确保置信区间上下限正确;
  3. 将geom_text的y轴位置绑定到Odds值,添加hjust=0.5实现居中,同时保持position_dodge宽度与其他图层一致。

问题2:调整为标准黑色线条样式

直接在geom_linerange中设置color="black",覆盖原有的分组颜色映射,不影响其他图层布局,同时可通过size参数调整线条粗细。


问题3:调整图例顺序与绘图匹配

  1. 调整Model因子的水平顺序,将橙色对应的high racial/ethnic minority放在首位;
  2. 同步修改dotCOLS和barCOLS的颜色顺序,确保图例颜色与绘图中的亚组颜色对应。

修改后的完整代码

df_adj <- structure(list(Variable = structure(c(5L, 6L, 4L, 3L, 2L, 1L, 
5L, 6L, 4L, 3L, 2L, 1L), levels = c("racial and ethnic minority (ref = White)", 
"sex male (ref=Female)", "age 12 yrs (ref=10 yrs)", 
"High population  (ref= Low)", "High land use(ref = Low)", 
"High traffic (ref = Low)"), class = "factor"), 
    # 修正Model列的分组值,前6行对应第一个亚组,后6行对应第二个
    Model = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), 
                      levels = c("high racial/ethnic minority", "Your other factor level"), 
                      class = "factor"), 
    Odds = c(2.12, 0.24, 1.16, 0.56, 1.02, 0.19, 3.72, 1.58, 3.14, 1.24, 0.9, 0.38), 
    CIHigh = c(0.73, 0.07, 0.36, 0.16, 0.36, 0.02, 1.38, 0.49, 1.24, 0.54, 0.41, 0.17), 
    CILow = c(6.48, 0.76, 3.72, 1.91, 2.88, 1.07, 10.7, 5.21, 8.74, 2.86, 1.98, 0.85), 
    CI_midpoint = c(3.605, 0.415, 2.04, 1.035, 1.62, 0.545, 6.04, 2.85, 4.99, 1.7, 1.195, 0.51)), 
    row.names = c(NA, -12L), class = "data.frame")

# 调整颜色顺序,橙色对应第一个亚组(图例顶部)
dotCOLS = c("#f9b282","#a6d8f0")
barCOLS = c("#de6b35","#008fd5")

ggplot(df_adj, aes(x = Variable, y = Odds, fill = Model)) + 
  # 修正CI上下限,设置黑色线条
  geom_linerange(aes(ymin = pmin(CILow, CIHigh), ymax = pmax(CILow, CIHigh)), 
                 size = 1.0, color = "black", position = position_dodge(width = 0.5)) +
  geom_hline(yintercept = 1, lty = 1) +
  geom_point(aes(color = Model), shape = 21, size = 2, colour = "white", stroke = 0.5, 
             position = position_dodge(width = 0.5)) +
  # OR值居中显示在点的上方
  geom_text(aes(label = round(Odds, 2)), 
            hjust = 0.5, vjust = -1.2, size = 3, color = "black", 
            family = "Times New Roman", position = position_dodge(width = 0.5)) +
  scale_x_discrete(name = "自变量") +
  scale_y_continuous(name = "比值比(95% CI)", limits = c(0, 11)) +
  scale_fill_manual(values = barCOLS) +
  scale_color_manual(values = dotCOLS) +
  coord_flip() +
  theme(
    axis.text.y = element_text(family = "Times New Roman", size = 9, color = "black"),
    axis.text.x = element_text(family = "Times New Roman", size = 9, color = "black"),
    axis.title.x = element_text(family = "Times New Roman", size = 9, color = "black"),
    text = element_text(family = "Times New Roman", size = 10)
  ) 

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 11:55:01