I'm trying to format the legend in my plot, but I have to use expression() because of greek and superscripts. However, when I want to display r^2 = 0.45, P<0.0001, I get r^2 = 0.45 P<1e-04, when I type in
legend(expression(r^2==0.9230~~P<0.0001))
I tried looking up the list() function but it doesn't help with the commas. I couldn't find anything on using decimals in the expression() function either.
Any suggestions would be appreciated.
Thanks
You can use paste()
(within the call to expression()
) to splice together character strings and unquoted expressions. The unquoted bits will be evaluated using the special rules exhibited by example(plotmath)
and demo(plotmath)
, while the character strings will be printed verbatim.
Here's an example (which also uses phantom()
, because the <
operator expects/needs something to both its left and its right):
plot(1)
legend(x = "topleft",
legend = expression(paste(r^2==0.9230, ", ", P<phantom(), "0.0001")))