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

如何调整forestplot中同一对象两条置信区间的行间距

解决forestplot中同组两条置信区间间距过大的问题

问题描述

绘制forestplot时,每个对象对应两条置信区间,但同一组的两条线间距过大,导致下方线条与下一组重叠。尝试设置lineheight="auto"后图表过长,不符合需求,需调整两条OR线的垂直间距。

当前效果:
当前forestplot效果

解决方案

可以通过以下几种方式调整同组内两条置信区间的垂直间距,同时避免整体图表过长:

1. 用固定值替代lineheight="auto"

lineheight="auto"会让行高自适应内容,导致间距过大。可将其设置为较小的固定单位,同时配合调整boxsize和ci.vertices.height优化显示:

forestplot(labeltext =as.matrix(rbind(header,umresult[seq(1,38,2),c(1,23,20)])),  
       legend = c("method1", "method2"),
       mean =cbind(c(NA,umresult$or[seq(1,38,2)]),c(NA,umresult$or[seq(2,38,2)])),
       lower=cbind(c(NA,umresult$or_lci[seq(1,38,2)]),c(NA,umresult$or_lci[seq(2,38,2)])),
       upper=cbind(c(NA,umresult$or_uci[seq(1,38,2)]),c(NA,umresult$or_uci[seq(2,38,2)])),
       graph.pos = 3,
       graphwidth = unit(70,"mm"),
       is.summary=c(T,rep(F,19)),
       txt_gp=fpTxtGp(label=gpar(cex=1),
                      ticks=gpar(cex=1.1), 
                      xlab=gpar(cex=1), 
                      title=gpar(cex=2)),
       hrzl_lines=list("2" = gpar(lwd=1, col="#444444")),
       zero = 1,
       xticks = c(0,1,2,3),
       clip=c(0,3),
       lwd.zero=1,
       lwd.ci=0.5,
       lwd.xaxis=1, 
       lty.ci=1,
       ci.vertices=TRUE, ci.vertices.height = 0.15,  # 缩小顶点高度
       lineheight=unit(8, 'mm'),  # 固定行高替代auto
       line.margin=unit(2, 'mm'),  # 缩小行边距
       colgap=unit(7, 'mm'),
       boxsize =0.08,  # 缩小置信区间方块大小
       col=fpColors(zero = "#e22e2a",lines = "BLACK",text=c("black"),box = c("#006400", "#CD853F")),
       #fn.ci_norm = forestcolor,
       grid =T)

2. 自定义fn.ci_norm调整垂直偏移

通过自定义绘制函数,手动控制同组内两条线的垂直位置,让它们更紧凑:

# 自定义置信区间绘制函数,添加垂直偏移
custom_ci <- function(lower, upper, mean, clr, x, y, ...) {
  # 第一条线向上偏移
  forestplot::fpDrawCI(lower = lower[1], upper = upper[1], mean = mean[1], clr = clr[1], 
                       x = x, y = y + 0.05, ...)
  # 第二条线向下偏移
  forestplot::fpDrawCI(lower = lower[2], upper = upper[2], mean = mean[2], clr = clr[2], 
                       x = x, y = y - 0.05, ...)
}

# 在forestplot中应用自定义函数
forestplot(labeltext =as.matrix(rbind(header,umresult[seq(1,38,2),c(1,23,20)])),  
       legend = c("method1", "method2"),
       mean =cbind(c(NA,umresult$or[seq(1,38,2)]),c(NA,umresult$or[seq(2,38,2)])),
       lower=cbind(c(NA,umresult$or_lci[seq(1,38,2)]),c(NA,umresult$or_lci[seq(2,38,2)])),
       upper=cbind(c(NA,umresult$or_uci[seq(1,38,2)]),c(NA,umresult$or_uci[seq(2,38,2)])),
       graph.pos = 3,
       graphwidth = unit(70,"mm"),
       is.summary=c(T,rep(F,19)),
       txt_gp=fpTxtGp(label=gpar(cex=1),
                      ticks=gpar(cex=1.1), 
                      xlab=gpar(cex=1), 
                      title=gpar(cex=2)),
       hrzl_lines=list("2" = gpar(lwd=1, col="#444444")),
       zero = 1,
       xticks = c(0,1,2,3),
       clip=c(0,3),
       lwd.zero=1,
       lwd.ci=0.5,
       lwd.xaxis=1, 
       lty.ci=1,
       ci.vertices=TRUE, ci.vertices.height = 0.2,
       lineheight=unit(7, 'mm'),  # 固定合适行高
       line.margin=unit(2, 'mm'),
       colgap=unit(7, 'mm'),
       boxsize =0.1,
       col=fpColors(zero = "#e22e2a",lines = "BLACK",text=c("black"),box = c("#006400", "#CD853F")),
       fn.ci_norm = custom_ci,  # 应用自定义绘制函数
       grid =T)

3. 缩小line.margin减少组间距离

line.margin控制行与行的额外边距,将其从unit(4, 'mm')减小到unit(2, 'mm')或更小,可减少组间距离避免重叠:

line.margin=unit(2, 'mm')

关键说明

  • 优先尝试调整lineheight为固定值,配合缩小line.margin和boxsize,这是最简单直接的方法。
  • 若需要更精细控制,再使用自定义fn.ci_norm函数调整两条线的垂直偏移。
  • 所有数值(如lineheight的8mm、偏移量0.05)需根据图表实际尺寸微调,直到达到理想效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 23:48:21