How do I use variables in Latex expressions in R?
For example:
plot(X, Y, main=expression(R^2))
Will put R with a nice superscripted 2
Another variation on @Joris' theme is substitute()
. You give substitute()
an expression and an environment or list within which to evaluate the expression. Passing a list is usually easiest, especially for jobs such as the one posed here.
plot(X,Y, main = substitute(R^2 : a, list(a = a)))
We can see why this works, by looking solely at the substitute()
call:
> substitute(R^2 : a, list(a = a))
R^2:0.8
The a
in the expression is replace with the value of a
in the list.