用上标和下标用逗号注释ggplot

问题描述 投票:0回答:1

我正在尝试用三行注释我的 ggplot。我已经放弃尝试 atop() 方式,因为我有三行,每行都有一个上标或下标,而且我一直遇到错误。

我想更改该图的以下注释:

  • 下标中的“149”应为“1,49”
  • “R2adj”应该有 2 上标和 adj 下标。

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[149] ==", F_stat), 
         parse = TRUE,
         hjust = 0) +
  annotate(geom = 'text',
         x = 4,
         y = 25, 
         label = paste("p < 0.001"), 
         parse = TRUE,
         hjust = 0) +
  annotate(geom = 'text',
         x = 4,
         y = 20, 
         label = paste(
           "R^2",
           "[adj]",
           "==",
           round(summary(mod1)$adj.r.squared, 3)), 
         parse = TRUE,
         hjust = 0)

我查看了其他几篇 stackoverflow 帖子,但无法弄清楚。有什么建议吗?谢谢!

r ggplot2 annotations subscript superscript
1个回答
4
投票

要获取下标中的 adj 和上标中的 2,请尝试使用

R[adj]^2
而不是
R^2[adj]
。要在下标数字中添加逗号,请将数字用引号引起来。您需要对引号使用反斜杠转义,因此它是
paste("F[\"1,49\"] ==", 122.952)

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  annotate(geom = 'text',
           x = 4,
           y = 30, 
           label = paste("F[\"1,49\"] ==", 122.952), 
           parse = TRUE,
           hjust = 0) +
  annotate(geom = 'text',
           x = 4,
           y = 25, 
           label = paste("p < 0.001"), 
           parse = TRUE,
           hjust = 0) +
  annotate(geom = 'text',
           x = 4,
           y = 20, 
           label = paste(
             "R[adj]^2",
             "==",
             0.709), 
           parse = TRUE,
           hjust = 0)

© www.soinside.com 2019 - 2024. All rights reserved.