[R] abline within data range

Nguyen Dinh Nguyen n.nguyen at garvan.org.au
Tue Mar 20 22:53:19 CET 2007


Dear all Rhelpers,

Thank you all for quick and helpful response.

Question: How to get the abline lying within the data range?

Suggested solutions:

## 1-  base graphics (suggested by Dimitris and Uwe)

x<- rnorm(200, 35,5)
y<- rnorm(200, 0.87,0.12)

plot(y~x, xlim=c(0,55), pch=17, bty="l")
lmObj <- lm(y ~ x)
xs <- range(x)
ys <- predict(lmObj, newdata = data.frame(x = xs))
plot(x, y, pch = 17, bty = "l")
lines(xs, ys, col = "red", lty = 2, lwd = 2)

## 2- using ggplot package (by Hadley)
install.packages("ggplot")
library(ggplot)

qplot(x,y, type=c("point","smooth"), method=lm)

#or, if you don't want the standard errors
qplot(x,y, type=c("point","smooth"), method=lm, se=F)

## 3-Using TeachingDemo package (suggested by Greg)
# but I haven’t try yet


Regards

Nguyen



More information about the R-help mailing list