如何用注释ggplot2,R来写“* value * * plus-minus sign * * value *”和文本?(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 ± ^你能帮我吗? 先谢谢你!
Here's my problem:
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);It gives me this error:
Error in parse(text = lab) : <text>:1:13: unexpected input 1: tau^-1 == 4 ± ^Can you help me please? Thank you in advance!
最满意答案
如果设置parse = TRUE ,标签将按照?plotmath描述显示。 在帮助页面上,您可以找到具有可用功能和正确语法的表。
在您的情况下,您只需使用%+-%而不是unicode Symbol \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)If you set parse = TRUE the label will be displayed as described in ?plotmath. On the help page you can find a table with the avaiable features and the proper syntax.
In your case you just have to use %+-% instead of the unicode Symbol \u00B1 so that R is able to coerce it to an expression.
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)更多推荐
发布评论