Man
Man

Reputation: 191

Superscript a legend in plot() and add the adjusted R2 from lm()

How can I superscript the 2 in "Adjusted R2 = " and import the value from summary()?

As an example:

#Multiple linear regression model
nitrate<-SSwells[,6]
predictors<-SSwells[,9:31]
linear<-lm(nitrate~.,data=predictors)
summary(linear)

#Obs vs. predicted
obs <-nitrate
pred <-linear$fitted.values
obs.pred <- lm(obs~pred)
plot(obs~pred, main="Observed vs. Predicted \nMultiple Linear 
Regression", xlim=c(0,14), ylim=c(0,14), ylab="Predicted Nitrate [mg L-1 
NO3-N]", xlab="Observed Nitrate [mg L-1 NO3-N]")
abline(obs.pred)

Through some searching, I found that:

lgd <- bquote(R^2 == .(round(summary(obs.pred)$r.squared,3)))
legend("bottomright", legend=lgd)

gives me a legend with the 2 in R2 superscripted, but when I modify it to:

lgd <- bquote("Adjusted" R^2 == .(round(summary(obs.pred)$adj.r.squared,3)))
legend("bottomright", legend=lgd)

so that it can say Adjusted R2 for the new value I get the error

Error: unexpected symbol in "lgd <- bquote("Adjusted" R"

I've tried taking the quotes out from around Adjusted, doesn't work. I've tried putting just a single quote in front of it, doesn't work. I've read the help file and examples and I can't figure it out.

So then I tried:

text(2,12,expression(paste("Adjusted ", R^2, " =", sep = "")))

This works, except I can't figure out how to import the adjusted R2 from summary(obs.pred)$adj.r.squared to go over the =, if I do:

text(2,12,expression(paste("Adjusted ", R^2, " =", 
round(summary(actpred)$r.squared,3), sep = "")))

It just prints the text instead of the actual value.

Is there a simple way to to this that I'm missing?

Upvotes: 1

Views: 620

Answers (1)

parth
parth

Reputation: 1631

i agree with @jruf003,

lgd <- bquote(Adjusted~R^2 == .(round(summary(obs.pred)$r.squared,3)))

Note : don't quote "Adjusted" in bquote(...) expression

Then, you can add it in main title of plot as plot(1:10, main=lgd)

Output : snap1

Upvotes: 2

Related Questions