如何使用 plotmath 表达式自动标记图轴?

How to automate graph axis labeling with plotmath expressions?

我正在尝试通过使用其绘图函数的多个实例并为每个函数分配适当的 y 轴标签(使用 plotmath)来使用 ggplot2 创建多个图形。下面是一些代码,演示了我失败的解决方案。 (注意愚蠢)

library(tidyverse)

df <- tibble(
  measure = c('Current', 'Resistance', 'Volts'),
  mean = c(532, 42, 50),
  sd = c(45, 6, 8),
  ylabel = c('Amps (A)', 'Ohms (Omega)', 'Volts (V)')
)

for (i in 1:3)) {
  g <- ggplot(df[i,], aes(x = measure, y = mean)) + 
    geom_point(size = 7) +
    geom_errorbar(aes(ymax = mean + sd, ymin = mean - sd), width = .25) +
    ylab(expression(df[i,]$ylabel))

  ggsave(g, paste0('~/Desktop/', df$measure[i]))

}

我 运行 遇到了 expression() 及其特殊字符放置的问题。你会如何解决这个问题?

尝试 parse 而不是 expression:

ylab(parse(text = df[i, ]$ylabel))