How do I include a superscript to texts on a plot on R?

Meed picture Meed · Sep 23, 2013 · Viewed 20.6k times · Source

I need it to look like this:

R^2 = some values

And I've tried the code below but it wouldn't work, it came out as "R (expression (^2)) = some values" instead:

text (25, 200, paste ("R (expression (^2)) =", round (rsquarelm2, 2)))

Answer

Gavin Simpson picture Gavin Simpson · Sep 23, 2013

You don't want a character vector, but an expression, hence this

expression(R^2 == 0.85)

is what you need. In this case, you want to substitute in the result of another R operation. For that you want substitute() or bquote(). I find the latter easier to work with:

rsquarelm2 <- 0.855463
plot(1:10, 1:10, type = "n")
text(5, 5, bquote(R^2 == .(round(rsquarelm2, 2))))

With bquote(), anything in .( ) is evaluated and the result is included in the expression returned.