R图的图例中有多个bquote项 [英] multiple bquote items in legend of an R plot

查看:166
本文介绍了R图的图例中有多个bquote项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下作品,(复制并粘贴到R中)

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.

任何帮助将不胜感激.

推荐答案

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

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)))))

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

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))

这篇关于R图的图例中有多个bquote项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆