如何用注释ggplot2,R来写“* value * * plus

编程入门 行业动态 更新时间:2024-10-27 18:25:56
如何用注释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)

更多推荐

本文发布于:2023-08-05 22:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1441087.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   如何用   来写

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!