R 图例中的多个 bquote 项

发布于 2024-12-02 01:05:05 字数 348 浏览 1 评论 0原文

接下来的工作,(复制并粘贴到 R 中)

a=123
plot(1,1)
legend('bottomleft',legend=bquote(theta == .(a)))

我想在图例中包含多个项目。 全部带有希腊字母。 举一个简单的例子,如果我重复该项目两次,代码将不再起作用,

a=123
plot(1,1)
legend('bottomleft',legend=c(bquote(theta == .(a)),bquote(theta == .(a))))

我尝试了许多更复杂的表达式,但它们都不起作用。

任何帮助将不胜感激。

Following works, (copy & paste into R)

a=123
plot(1,1)
legend('bottomleft',legend=bquote(theta == .(a)))

I want to have multiple items in the legend.
All with greek letters.
As a simple example, if I repeat the item twice the code does not work anymore

a=123
plot(1,1)
legend('bottomleft',legend=c(bquote(theta == .(a)),bquote(theta == .(a))))

I have tried many more complicated expressions but they all did not work.

Any help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽清梦 2024-12-09 01:05:05

在这种情况下,plotmath 无法强制调用表达式的列表。

> cs <- c(bquote(theta == .(a)),bquote(theta == .(a)))
> cs
[[1]]
theta == 123

[[2]]
theta == 123

> sapply(cs, class)
[1] "call" "call"

如果您自己强制表达式,则可以实现此目的:

> c(as.expression(bquote(theta == .(a))), as.expression(bquote(theta == .(a))))
expression(theta == 123, theta == 123)
> plot(1,1)
> legend('bottomleft',legend= c(as.expression(bquote(theta == .(a))), 
+                               as.expression(bquote(theta == .(a)))))

另一种方法是使用 sapply 强制调用表达式的原始列表:

plot(1,1)
legend("bottomleft", 
       sapply(c(bquote(theta == .(a)), bquote(theta == .(a))), as.expression))

In this case, plotmath is not able to coerce the list of calls to expressions.

> cs <- c(bquote(theta == .(a)),bquote(theta == .(a)))
> cs
[[1]]
theta == 123

[[2]]
theta == 123

> sapply(cs, class)
[1] "call" "call"

You can make this work if you coerce to expressions yourself:

> c(as.expression(bquote(theta == .(a))), as.expression(bquote(theta == .(a))))
expression(theta == 123, theta == 123)
> plot(1,1)
> legend('bottomleft',legend= c(as.expression(bquote(theta == .(a))), 
+                               as.expression(bquote(theta == .(a)))))

Another way is to coerce the original list of calls to expressions using sapply:

plot(1,1)
legend("bottomleft", 
       sapply(c(bquote(theta == .(a)), bquote(theta == .(a))), as.expression))
贪恋 2024-12-09 01:05:05

要强制调用表达式的原始列表,无需使用 sapply()。仅对 c() 构造中的组件之一使用 as.expression() 就足够了:

plot(1,1)
legend("bottomleft", 
       c(as.expression(bquote(theta == .(a))), bquote(theta == .(a))))

然后 c() 将自动强制整个列表,到表达式类。

To coerce the original list of calls to expressions it is not necessary to use sapply(). It suffices to use as.expression() only for one of the components within the c() construct:

plot(1,1)
legend("bottomleft", 
       c(as.expression(bquote(theta == .(a))), bquote(theta == .(a))))

c() will then automatically coerce the whole list, to the expression class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文