[R] abline within data range

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Mar 20 12:56:48 CET 2007



Nguyen Dinh Nguyen wrote:
> Dear R helpers,
> 
> I would like to have abline,  for a lm model for
> example, lying within data range. Do you know how to
> get it?
> 
> Thank in advance
> 
> Nguyen D Nguyen
> 
> #CODE
> x<- rnorm(200, 35,5)
>  y<- rnorm(200, 0.87,0.12)
>  plot(y~x, xlim=c(0,50), pch=17, bty="l")
>  abline(lm(y~x))
>
> # I would like abline is between min(x) and max(x)

Just make it yourself as in:

  newdat <- data.frame(x = range(x))
  pred <- predict(lm(y ~ x), newdata = newdat)
  lines(newdat$x, pred)

Uwe Ligges




>  
> 
>  
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.



More information about the R-help mailing list