An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120522/520a69c8/attachment.pl>
Adding Text to a Plot
3 messages · Canto Casasola, Vicente David, David Winsemius, Peter Ehlers
On May 22, 2012, at 9:10 AM, Canto Casasola, Vicente David wrote:
I'm pretty sure I'm missing something about this.
Is there a smart way of typping hat(R)^2 and it's value from a linear
regression?
I've just found this tricky one:
# Sample data
x <- sample(1:100,10)
y <- 2+3*x+rnorm(10)
# Run the regression
lm1 <- lm(y~x)
# Plotting
plot(x,y, main="Linear Regression", col="red")
abline(lm1, col="blue")
placex <- par("usr")[1]+.1*(par("usr")[2]-par("usr")[1])
placey1 <- par("usr")[3]+.9*(par("usr")[4]-par("usr")[3])
placey2 <- par("usr")[3]+.8*(par("usr")[4]-par("usr")[3])
# HERE: Is this the right way?
# To do .... what? I see that you over-plotted the R, presumably because you did not like the way that: bquote(hat(R)^2 == .(summary(lm1)$adj.r.squared)) ... ended up looking (with the exponent higher than in the non-hatted version.)
text(x=placex, y=placey1,
bquote(R^2 == .(summary(lm1)$r.squared)), adj=c(0,0))
text(x=placex, y=placey2,
bquote(R^2 == .(summary(lm1)$adj.r.squared)), adj=c(0,0))
text(x=placex,y=placey2,
expression(hat(R)), adj=c(0,0))
In addition, when I save the plot as PDF, the expression hat(R)
seems to be
somewhat displaced.
Any advice?
Are you sure? It "looks" to me that the lower "R" is slightly shifted to the left, but when I actually measure it, there does not appear to be a shift. If it is real and not just an optical illusion, this may have something to do with your unstated version of R or your also unstated OS. -- David Winsemius, MD West Hartford, CT
On 2012-05-22 07:51, David Winsemius wrote:
On May 22, 2012, at 9:10 AM, Canto Casasola, Vicente David wrote:
I'm pretty sure I'm missing something about this.
Is there a smart way of typping hat(R)^2 and it's value from a linear
regression?
I've just found this tricky one:
# Sample data
x<- sample(1:100,10)
y<- 2+3*x+rnorm(10)
# Run the regression
lm1<- lm(y~x)
# Plotting
plot(x,y, main="Linear Regression", col="red")
abline(lm1, col="blue")
placex<- par("usr")[1]+.1*(par("usr")[2]-par("usr")[1])
placey1<- par("usr")[3]+.9*(par("usr")[4]-par("usr")[3])
placey2<- par("usr")[3]+.8*(par("usr")[4]-par("usr")[3])
# HERE: Is this the right way?
# To do .... what? I see that you over-plotted the R, presumably
because you did not like the way that:
bquote(hat(R)^2 == .(summary(lm1)$adj.r.squared))
... ended up looking (with the exponent higher than in the non-hatted
version.)
text(x=placex, y=placey1,
bquote(R^2 == .(summary(lm1)$r.squared)), adj=c(0,0))
text(x=placex, y=placey2,
bquote(R^2 == .(summary(lm1)$adj.r.squared)), adj=c(0,0))
text(x=placex,y=placey2,
expression(hat(R)), adj=c(0,0))
In addition, when I save the plot as PDF, the expression hat(R)
seems to be
somewhat displaced.
Any advice?
Are you sure? It "looks" to me that the lower "R" is slightly shifted to the left, but when I actually measure it, there does not appear to be a shift. If it is real and not just an optical illusion, this may have something to do with your unstated version of R or your also unstated OS.
I don't see any shift either (Windows), easily checked with insert of an
appropriate vertical line, but I would recommend not
overwriting the 'R' by using the phantom() construct:
text(x=placex, y=placey2,
bquote(phantom(R)^2 == .(summary(lm1)$adj.r.squared)), adj=c(0,0))
text(x=placex, y=placey2,
expression(hat(R)), adj=c(0,0))
Peter Ehlers
-- David Winsemius, MD West Hartford, CT