Sang Sang - 1 year ago 165
R Question

multiple bquote items in legend of an R plot

Following works, (copy & paste into R)

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

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.

Answer Source

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

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

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:

       sapply(c(bquote(theta == .(a)), bquote(theta == .(a))), as.expression))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download