Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latex and variables in plot label in R?

Tags:

plot

r

latex

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 as main title.

But let's say I want it to say 'R^2: 0.5', with 0.5 coming from a R variable. How do I do that?

like image 828
c00kiemonster Avatar asked Jul 28 '11 09:07

c00kiemonster


1 Answers

The hack of Owen is pretty cool, but not really the common way to do that. If you use bquote, this is actually pretty easy. bquote will do the same as quote, with the exception that everything between .() will be evaluated in a specified environment (or the global, if nothing is specified).

A trivial example :

X <- 1:10
Y <- 1:10
a <- 0.8
plot(X,Y,main=bquote(R^2 : .(a)))

Gives :

enter image description here

See also ?bquote and ?plotmath for more examples and possibilities

like image 195
Joris Meys Avatar answered Nov 16 '22 14:11

Joris Meys