# I use this code to label a graph with the R2:
# graph
x <- rnorm(100)
y <- x + rnorm(100)
lm1 <- lm(y~x)
plot(x,y)
# label
R2text <- substitute(paste(R^2," = ",r2),list(r2=r2))
text(1,-3,R2text, col="red")
# i have modified this a bit, so that i have a vector with other labels,
each of which
# will be labelled on the graph. Example:
texts <- c("And the R2 is", R2text)
x <- c(-2,-2)
y <- c(2,1)
for(i in 1:length(texts))text(x[i],y[i],texts[i],pos=4, col="blue")
# As you can see the label "R2 = 48.7" remains at "paste(R^2,
" = ", 48.7)"
# What to do?
Thanks for your help,
Remko
..-~-.-~-.-~-.-~-.-~-.-~-.-~-.-~-.-~-
Remko Duursma
Post-doctoral researcher
Dept. Forest Ecology
University of Helsinki, Finland
_________________________________________________________________
With tax season right around the corner, make sure to follow these few
simple tips.
hi, your first argument to substitute should be an expression, not a character string (which the output of paste() will give you) so... # first assign to variable r2 (think you forgot to do that) # in your example r2 <- summary(lm1)$r.squared # then to your label R2text <- substitute(R^2==r2,list(r2=round(r2,2))) # then you can annotate with text() as you did alternatively, you can use R2text <- bquote(R^2==.(round(r2,2))) --- remko duursma <remkoduursma at hotmail.com> wrote:> > # I use this code to label a graph with the R2: > > # graph > x <- rnorm(100) > y <- x + rnorm(100) > lm1 <- lm(y~x) > plot(x,y) > > # label > R2text <- substitute(paste(R^2," = ",r2),list(r2=r2)) > text(1,-3,R2text, col="red") > > # i have modified this a bit, so that i have a vector with other labels, > each of which > # will be labelled on the graph. Example: > texts <- c("And the R2 is", R2text) > x <- c(-2,-2) > y <- c(2,1) > for(i in 1:length(texts))text(x[i],y[i],texts[i],pos=4, col="blue") > > # As you can see the label "R2 = 48.7" remains at "paste(R^2, " = ", 48.7)" > # What to do? > > > > Thanks for your help, > > Remko > > > > ..-~-.-~-.-~-.-~-.-~-.-~-.-~-.-~-.-~- > Remko Duursma > Post-doctoral researcher > Dept. Forest Ecology > University of Helsinki, Finland > > _________________________________________________________________ > With tax season right around the corner, make sure to follow these few > simple tips. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >____________________________________________________________________________________ Finding fabulous fares is fun.
Dear Renko
To modify the script
plot(x,y)
r2<-summary(lm1)$r.squared*100
# label
R2text <- substitute(paste(R^2," = ",r2),list(r2=r2))
text(-1,1,R2text, col="red") # To see of the coordinates of the graph.
Grettings
Felipe
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of remko duursma
Sent: Tuesday, March 13, 2007 8:02 AM
To: r-help at stat.math.ethz.ch
Subject: [R] 'substitute' question
# I use this code to label a graph with the R2:
# graph
x <- rnorm(100)
y <- x + rnorm(100)
lm1 <- lm(y~x)
plot(x,y)
# label
R2text <- substitute(paste(R^2," = ",r2),list(r2=r2))
text(1,-3,R2text, col="red")
# i have modified this a bit, so that i have a vector with other labels,
each of which
# will be labelled on the graph. Example:
texts <- c("And the R2 is", R2text)
x <- c(-2,-2)
y <- c(2,1)
for(i in 1:length(texts))text(x[i],y[i],texts[i],pos=4, col="blue")
# As you can see the label "R2 = 48.7" remains at "paste(R^2,
" = ", 48.7)"
# What to do?
Thanks for your help,
Remko
..-~-.-~-.-~-.-~-.-~-.-~-.-~-.-~-.-~-
Remko Duursma
Post-doctoral researcher
Dept. Forest Ecology
University of Helsinki, Finland
_________________________________________________________________
With tax season right around the corner, make sure to follow these few
simple tips.
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
I do not understand why you play with 'substitute' instead of something
like this:
# CAUTION: this will work only for bivariate case
# plotmaking function
plotModel <- function(m)
{
x <- m$model$x
y <- m$model$y
r2 <- summary(m)$r.squared
plot(x,y)
abline(m)
text( min(x), max(y), paste("And the R^2 is", round(r2,3)),
pos=4)
invisible(NULL)
}
# your data
x <- rnorm(100)
y <- x + rnorm(100)
lm1 <- lm(y~x)
# make the plot
plotModel(lm1)
*** Note that my e-mail address has changed to m.j.bojanowski at uu.nl
*** Please update your address books accordingly. Thank you!
_________________________________________
Michal Bojanowski
ICS / Sociology
Utrecht University
Heidelberglaan 2; 3584 CS Utrecht
Room 1428
m.j.bojanowski at uu.nl
http://www.fss.uu.nl/soc/bojanowski
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of remko duursma
Sent: Tuesday, March 13, 2007 2:02 PM
To: r-help at stat.math.ethz.ch
Subject: [R] 'substitute' question
# I use this code to label a graph with the R2:
# graph
x <- rnorm(100)
y <- x + rnorm(100)
lm1 <- lm(y~x)
plot(x,y)
# label
R2text <- substitute(paste(R^2," = ",r2),list(r2=r2))
text(1,-3,R2text,
col="red")
# i have modified this a bit, so that i have a vector with other labels,
each of which # will be labelled on the graph. Example:
texts <- c("And the R2 is", R2text)
x <- c(-2,-2)
y <- c(2,1)
for(i in 1:length(texts))text(x[i],y[i],texts[i],pos=4, col="blue")
# As you can see the label "R2 = 48.7" remains at "paste(R^2,
" = ",
48.7)"
# What to do?
Thanks for your help,
Remko
..-~-.-~-.-~-.-~-.-~-.-~-.-~-.-~-.-~-
Remko Duursma
Post-doctoral researcher
Dept. Forest Ecology
University of Helsinki, Finland
_________________________________________________________________
With tax season right around the corner, make sure to follow these few
simple tips.
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
I'm sorry for my first post as I did not properly understood your
problem.
The reason why you were getting "paste(R^2, " = ", 48.7)"
instead of the
properly formatted R^2=48.7
is that in creating 'texts' in your code you were mixing character
string with expressions resulting in 'texts' being a list not a vector.
The only modification to make is to index texts with double square
brackets as below
# the figure
x <- rnorm(100)
y <- x + rnorm(100)
lm1 <- lm(y~x)
plot(x,y)
# label
R2text <- substitute(paste(R^2," = ",r2),list(r2=r2))
text(1,-3,R2text,
col="red")
# i expanded your list of labels to four elements...
texts <- c("And the R2 is", R2text, "third label",
"fourth label")
# texts is a list not an atomic vector
class(texts)
str(texts)
# ... and added more coordinates accordingly
x <- c(-2,-2, -1, -1)
y <- c(2,1, 2, 1)
# here 'texts[[i]]' instead of 'texts[i]'
for(i in 1:length(texts))text(x[i],y[i], texts[[i]],pos=4, col="blue")
hth,
Michal
*** Note that my e-mail address has changed to m.j.bojanowski at uu.nl
*** Please update your address books accordingly. Thank you!
_________________________________________
Michal Bojanowski
ICS / Sociology
Utrecht University
Heidelberglaan 2; 3584 CS Utrecht
Room 1428
m.j.bojanowski at uu.nl
http://www.fss.uu.nl/soc/bojanowski
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of remko duursma
Sent: Tuesday, March 13, 2007 2:02 PM
To: r-help at stat.math.ethz.ch
Subject: [R] 'substitute' question
# I use this code to label a graph with the R2:
# graph
x <- rnorm(100)
y <- x + rnorm(100)
lm1 <- lm(y~x)
plot(x,y)
# label
R2text <- substitute(paste(R^2," = ",r2),list(r2=r2))
text(1,-3,R2text,
col="red")
# i have modified this a bit, so that i have a vector with other labels,
each of which # will be labelled on the graph. Example:
texts <- c("And the R2 is", R2text)
x <- c(-2,-2)
y <- c(2,1)
for(i in 1:length(texts))text(x[i],y[i],texts[i],pos=4, col="blue")
# As you can see the label "R2 = 48.7" remains at "paste(R^2,
" = ",
48.7)"
# What to do?
Thanks for your help,
Remko
..-~-.-~-.-~-.-~-.-~-.-~-.-~-.-~-.-~-
Remko Duursma
Post-doctoral researcher
Dept. Forest Ecology
University of Helsinki, Finland
_________________________________________________________________
With tax season right around the corner, make sure to follow these few
simple tips.
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.