1

I have a regression plot and I want to put R^2 value on my plot. Is there way to access to math symbols in R ?

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
ocean
  • 55
  • 1
  • 10
  • Use `expression(R^2)`. For example: `plot(1:2, 1:2, xlab=expression(R^2))` – Sam Dickson Dec 17 '15 at 19:46
  • This seems like something that would have been asked before. [This one](http://stackoverflow.com/questions/4423130/use-a-variable-within-a-plotmath-expression) is closely related. – Rich Scriven Dec 17 '15 at 19:59
  • Of you know latex, this pkg may be helpful: https://cran.r-project.org/web/packages/latex2exp/vignettes/using-latex2exp.html – Thomas Dec 17 '15 at 20:18

2 Answers2

2

See the help page ?plotmath

plot(1:10, 1:10, type = "l")
text(4, 9, expression(R^2 == 1))
Raad
  • 2,675
  • 1
  • 13
  • 26
1

Here is a simple example which you can tailor to your needs.

x <- rnorm(50)
y <- 0.5 + 2 * x + rnorm(50)
model <- lm(y ~ x)
rsquared <- format(summary(model)$r.squared, digits = 3)
label <- substitute(expression(R^2 == VALUE), list(VALUE = rsquared))
png("rsquared.png")
plot(x, y)
abline(a = model$coefficients[1], b = model$coefficients[2])
legend('topleft', legend = eval(label))

enter image description here

maccruiskeen
  • 2,748
  • 2
  • 13
  • 23