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

如何在ggplot标注中设置带逗号的下标及复合上下标?

解决ggplot中标注的上下标格式问题

关键修改点

  • F统计量下标带逗号:plotmath语法中,下标包含逗号时需用list()包裹,写成F[list(1,49)],解析后会正确显示F₁,₄₉。
  • R²adj的上下标:使用R[adj]^2的plotmath语法,实现R的上标2、下标adj的格式(即R²ₐdj)。

修改后的完整代码

mod1 <- lm(data = mtcars, mpg ~ cyl)
F_stat <- (car::Anova(mod1))$`F value`[1]

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  annotate(geom = 'text',
           x = 4,
           y = 30, 
           label = paste("F[list(1,49)] ==", round(F_stat, 3)), 
           parse = TRUE,
           hjust = 0) +
  annotate(geom = 'text',
           x = 4,
           y = 25, 
           label = "p < 0.001", 
           parse = TRUE,
           hjust = 0) +
  annotate(geom = 'text',
           x = 4,
           y = 20, 
           label = paste("R[adj]^2 ==", round(summary(mod1)$adj.r.squared, 3)), 
           parse = TRUE,
           hjust = 0)

可选:合并标注为多行(解决atop报错问题)

如果想把三个标注合并成一个多行文本,可使用atop()语法,注意每个分句需为合法的plotmath表达式,用逗号分隔:

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  annotate(geom = 'text',
           x = 4,
           y = 30, 
           label = paste0(
             "atop(F[list(1,49)] == ", round(F_stat,3), 
             ", p < 0.001",
             ", R[adj]^2 == ", round(summary(mod1)$adj.r.squared,3), ")"
           ),
           parse = TRUE,
           hjust = 0,
           vjust = 1)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 05:07:47