我如何写“ *value* *plus-minus sign* *value* ”,连同文本,以及 ggplot2,R 的注释?

How do I write " *value* *plus-minus sign* *value* ", along with text, with annotate of ggplot2, R?

这是我的问题:

library(ggplot2)

a = c(4, 2)
x = c(1:4)
y = c(1:4)

# This works (τ^-1 = 4 s^-1):

l <- paste("tau^-1 ==", a[1], "*~s^-1")
qplot(x, y) + annotate("text", x = 1.5, y = 3.5, parse=TRUE, label = l);

# But I would like to see something like this ( τ^-1 = 4 ± 2 s^-1): 

l <- paste("tau^-1 ==", a[1], "\u00B1", a[2], "*~s^-1")
qplot(x, y) + annotate("text", x = 1.5, y = 3.5, parse=TRUE, label = l);

它给我这个错误:

Error in parse(text = lab) : <text>:1:13: unexpected input
1: tau^-1 == 4 ±
                ^

你能帮帮我吗?提前致谢!

如果您设置 parse = TRUE,标签将按照 ?plotmath 中的描述显示。在帮助页面上,您可以找到具有可用功能和正确语法的 table。

在您的情况下,您只需要使用 %+-% 而不是 unicode 符号 \u00B1,以便 R 能够将其强制转换为表达式。

 l <- paste("tau^-1 ==", a[1], "%+-%", a[2], "*~s^-1")
 qplot(x, y) + annotate("text", x = 1.5, y = 3.5, parse=TRUE, label = l)