R中注解的下标

06odsfpq  于 2023-01-22  发布在  其他
关注(0)|答案(2)|浏览(152)

我有一个新手问题:对于这段代码,我想把VT11作为下标。

[...]
  annotate("text", x=VT1PPO_CAD-2, y=40,   ##or bp_CAD if we use absVO2%
           label=paste0("VT1: ", round(VT1PPO_CAD, 0), "%"), angle=90, size = 4) +
[...]

我试过了,但它不起作用。

label=paste0(expression(paste("V", T[1], ": ")), round(VT1PPO_CAD, 0), "%")

非常感谢!

vuktfyat

vuktfyat1#

你尝试的解决方案的根本问题是,如果你想让R正确地格式化你的 entire 标签,它必须是一个未求值的表达式。你的代码试图将未求值的表达式作为字符串的一部分使用(外部的paste0调用将参数转换为字符串)。这是行不通的。
因此,您需要将逻辑颠倒过来:你需要创建一个未赋值的表达式,并且你需要将变量(VT1PPO_CAD)* 插入到 * 那个表达式中(舍入后)。
expression函数不允许插入值1。要插入(= insert evaluated)子表达式,您需要使用其他解决方案。存在几种解决方案,但我最喜欢的方法是bquote函数。
此外,不需要拆分和/或引用VT;把它们放在一起:

bquote(paste(VT[1], ": ", .(round(VT1PPO_CAD, 0)), "%"))

bquote接受表达式,并在将求值结果插入到周围的表达式之前,计算 Package 在.(…)中的子表达式。
1一般来说expression函数是完全无用的,我甚至不知道它为什么存在;据我所知,它对于quote是100%冗余的,答案可能是“for compatibility with S”

evrscar2

evrscar22#

我们可以使用substitute

VT1PPO_CAD <- 96.32

plot(1:5,
     main = substitute(paste(VT[1],": ", value1, "%"),
                       list(value1 = round(VT1PPO_CAD, 0))
                       )
     )

ggplot2(根据您的标签):

library(tibble)
library(ggplot2)

tibble(x = 1:5, y = 1:5) |>
  ggplot(aes(x, y)) +
  geom_point() +
  labs(title = substitute(paste(VT[1],": ", value1, "%"),
                         list(value1 = round(VT1PPO_CAD, 0))
                          )
        )

相关问题