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

R绘图中带上标的多行文本居中及行间距适配问题

R绘图:多行居中文本框(含上标)的行间距调整问题

问题说明

需要在R绘制的图表中添加一个包含5行文本的居中文本框,文本内带有上标格式。尝试了三种实现方案,但均存在缺陷:

  • 使用atop()函数嵌套:由于行数(5行)不是2的幂次,导致每行文本的显示尺寸不一致
  • 结合expression()与paste():文本无法居中对齐,且最后一行与前一行的间距过大
  • 多次调用text()函数:行间距不符合R绘图的默认样式,需要调整到与默认一致

测试代码

layout(matrix(c(1, 1, 2, 2, 0, 3, 3, 0), byrow = T, nrow = 2))
plot(0, type = 'n', main = "Plot 1")
text(1, 0, bquote(atop(atop("Total", "Number of"), atop("Observed", atop("Interactions", "(n * m" ^ "-2" * " * wk" ^ "-1" * ")")))))
plot(0, type = 'n', main = "Plot 2")
text(1, 0, expression(paste("Total\nNumber of\nObserved\nInteractions\n(n * m" ^ "-2" * " * wk" ^ "-1" * ")")))
plot(0, type = 'n', main = "Plot 3")
text(1, 0.4, "Total")
text(1, 0.3, "Number of")
text(1, 0.2, "Observed")
text(1, 0.1, "Interactions")
text(1, 0, expression(paste("(n * m" ^ "-2" * " * wk" ^ "-1" * ")")))

核心疑问

想知道使用atop()函数或\n换行时的默认行间距参数,以此调整第三种多次text()方案的行间距,使其与默认样式匹配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 17:02:39