1

I would like a plot legend to contain both the R-squared value, as well as another value (the number of datapoints, n, in this case, but it's not specifically important). I'd like R-squared in the format R^2 =, where R is italicised, and 2 is superscript.

Adding R-squared in this way on its own can be achieved on the following example using bquote

DF <- data.frame(VAR1=rnorm(100), VAR2=rnorm(100))
with(DF, plot(VAR1, VAR2))
fit <- lm(VAR2 ~ VAR1, data=DF)
r2 <- summary(fit)$adj.r.squared
mylabel = bquote(italic(R)^2 == .(format(r2, digits = 2)))
legend("bottomright", legend = mylabel ) # display legend

Adding both the R-squared value and n can be achieved in the following way (with credit to @DirkEddelbuettel here How can I plot my R Squared value on my scatterplot using R?)

legend("topleft", legend = c( paste("R2 =", format(r2, digits = 2)),
                              paste("n = ", nrow(DF)) ) )

However, I'm unable to find a way of modifying this to format the R^2 in a way that doesn't also paste the call. For example,

legend("topleft", legend = c( expression(paste(italic(R)^2, " = ", format(r2, digits = 2))),
                          paste("n = ", nrow(DF)) ) )

will successfuly format the R^2 but also pastes format(r2, digits = 2)

This seems as though it should be straightforward but I'm having no joy. Any help would be much appreciated, thanks.

Community
  • 1
  • 1
pyg
  • 716
  • 6
  • 18
  • This might help you http://lukemiller.org/index.php/2012/10/adding-p-values-and-r-squared-values-to-a-plot-using-expression/. – Alex Jun 18 '16 at 08:55
  • See [here](http://stackoverflow.com/q/20453408/4241780) for a possible duplicate of this question and workable answers. – JWilliman Jun 18 '16 at 09:04
  • Thank you both for bringing these solutions to my attention. Both work as desired, though the solution alluded to by @JWilliman is simpler and hence preferable. For completeness sake, that solution (by @SvenHohenstein) is `legend("bottomright", c(as.expression(bquote(italic(R)^2 == .(format(r2, digits = 2)))), as.expression(paste("n =", nrow(DF)))))` – pyg Jun 19 '16 at 06:04

1 Answers1

0

Here is a way to do it, using atop to split within several lines.

# your code
DF <- data.frame(VAR1=rnorm(100), VAR2=rnorm(100))
with(DF, plot(VAR1, VAR2))
fit <- lm(VAR2 ~ VAR1, data=DF)
r2 <- summary(fit)$adj.r.squared
mylabel = bquote(italic(R)^2 == .(format(r2, digits = 2)))

# the legend
legend("topright", legend=bquote(atop(italic(R)^2 == .(format(r2, digits = 2)),"n" == .(nrow(DF)))),bty="n")

Note that I had to remove the box around the legend (bty=n") as it is not well managed. For another solution involving mtext you may be interested in those two other post on stack and all links there:

Expression and new line in plot labels

Line break in expression()?

Community
  • 1
  • 1
Eric Lecoutre
  • 1,461
  • 16
  • 25
  • Thanks @Eric, this does the job, although not sure whether `atop` will permit multiple legend items (for the sake of versatility) – pyg Jun 19 '16 at 05:57