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

如何在ggforestplot中扩大数据点间距,区分多结局森林图数据?

问题描述

我使用ggforestplot包绘制包含20个风险因素、7个结局的森林图,代码直接参考该包官方示例:

df <- df_logodds_associations %>% 
  mutate(study = factor(study, levels = c("Meta-analysis", "NFBC-1997", "DILGOM", "FINRISK-1997", "YFS"))) %>% 
  ggforestplot::forestplot(
    df = df, 
    name = name, 
    estimate = beta, 
    se = se, 
    pvalue = pvalue, 
    psignif = 0.002, 
    colour = study, 
    shape = study, 
    xlab = "Odds ratio for incident type 2 diabetes (95% CI)\nper 1−SD increment in metabolite concentration", 
    title = "Amino acids", 
    logodds = TRUE
  ) + 
  ggplot2::scale_shape_manual(
    values = c(23L, 21L, 21L, 21L, 21L), 
    labels = c("Meta-analysis", "NFBC-1997", "DILGOM", "FINRISK-1997", "YFS")
  )
#> Scale for 'shape' is already present. Adding another scale for 'shape', which
#> will replace the existing scale.

将结局数量从示例的5个改为7个后,生成的森林图数据点过于拥挤,无法清晰区分各数据,请问如何扩大数据点之间的间距?


解决方法

以下几种方法可有效缓解数据点拥挤问题:

1. 分组偏移数据点

利用position_dodge()设置分组点的横向偏移,让不同结局的点错开排列:

df <- df_logodds_associations %>% 
  mutate(study = factor(study, levels = c("Meta-analysis", "NFBC-1997", "DILGOM", "FINRISK-1997", "YFS"))) %>% 
  ggforestplot::forestplot(
    df = df, 
    name = name, 
    estimate = beta, 
    se = se, 
    pvalue = pvalue, 
    psignif = 0.002, 
    colour = study, 
    shape = study, 
    xlab = "Odds ratio for incident type 2 diabetes (95% CI)\nper 1−SD increment in metabolite concentration", 
    title = "Amino acids", 
    logodds = TRUE,
    position = position_dodge(width = 0.6) # 调整width值控制偏移幅度
  ) + 
  ggplot2::scale_shape_manual(
    values = c(23L, 21L, 21L, 21L, 21L), 
    labels = c("Meta-analysis", "NFBC-1997", "DILGOM", "FINRISK-1997", "YFS")
  )

可根据实际拥挤程度在0.4-0.8之间调整width数值。

2. 增大图表输出尺寸

通过ggsave()指定更大的宽高,给数据点预留更多展示空间:

# 先保存绘图对象
p <- df_logodds_associations %>% 
  mutate(study = factor(study, levels = c("Meta-analysis", "NFBC-1997", "DILGOM", "FINRISK-1997", "YFS"))) %>% 
  ggforestplot::forestplot(
    df = df, 
    name = name, 
    estimate = beta, 
    se = se, 
    pvalue = pvalue, 
    psignif = 0.002, 
    colour = study, 
    shape = study, 
    xlab = "Odds ratio for incident type 2 diabetes (95% CI)\nper 1−SD increment in metabolite concentration", 
    title = "Amino acids", 
    logodds = TRUE
  ) + 
  ggplot2::scale_shape_manual(
    values = c(23L, 21L, 21L, 21L, 21L), 
    labels = c("Meta-analysis", "NFBC-1997", "DILGOM", "FINRISK-1997", "YFS")
  )

# 输出大尺寸图表
ggsave("forestplot.png", p, width = 12, height = 18, dpi = 300)

针对20个风险因素,可将height设置为16-20,对应调整width即可。

3. 差异化点的形状与尺寸

给每个结局分配独特的形状,同时增大点的尺寸,即使拥挤也能快速区分:

df <- df_logodds_associations %>% 
  mutate(study = factor(study, levels = c("Meta-analysis", "NFBC-1997", "DILGOM", "FINRISK-1997", "YFS"))) %>% 
  ggforestplot::forestplot(
    df = df, 
    name = name, 
    estimate = beta, 
    se = se, 
    pvalue = pvalue, 
    psignif = 0.002, 
    colour = study, 
    shape = study, 
    xlab = "Odds ratio for incident type 2 diabetes (95% CI)\nper 1−SD increment in metabolite concentration", 
    title = "Amino acids", 
    logodds = TRUE,
    size = 3 # 增大点的显示尺寸
  ) + 
  ggplot2::scale_shape_manual(
    values = c(23L, 21L, 22L, 24L, 25L, 17L, 19L), # 给7个结局分配不同形状
    labels = c("Meta-analysis", "NFBC-1997", "DILGOM", "FINRISK-1997", "YFS", "结局6", "结局7")
  )

4. 扩展纵向行间距

通过调整y轴文本边距,增加风险因素之间的纵向空间,间接缓解横向点的拥挤:

df <- df_logodds_associations %>% 
  mutate(study = factor(study, levels = c("Meta-analysis", "NFBC-1997", "DILGOM", "FINRISK-1997", "YFS"))) %>% 
  ggforestplot::forestplot(
    df = df, 
    name = name, 
    estimate = beta, 
    se = se, 
    pvalue = pvalue, 
    psignif = 0.002, 
    colour = study, 
    shape = study, 
    xlab = "Odds ratio for incident type 2 diabetes (95% CI)\nper 1−SD increment in metabolite concentration", 
    title = "Amino acids", 
    logodds = TRUE
  ) + 
  ggplot2::scale_shape_manual(
    values = c(23L, 21L, 21L, 21L, 21L), 
    labels = c("Meta-analysis", "NFBC-1997", "DILGOM", "FINRISK-1997", "YFS")
  ) +
  ggplot2::theme(
    axis.text.y = ggplot2::element_text(margin = ggplot2::margin(r = 10)) # 增加y轴文本右侧边距
  )

内容的提问来源于Stack Exchange,提问作者Tom Mueller

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 15:06:16