[R] Controlling the length of line with abline(lm())

hadley wickham h.wickham at gmail.com
Thu Jun 19 15:48:36 CEST 2008

On Wed, Jun 18, 2008 at 9:09 PM, Tariq Perwez <tariq.perwez at gmail.com> wrote:
> Hi
> I just realized that when I use linear regression to draw a line through my
> data points with something like the following:
> abline(lm(y ~ x))
> the length of the line is infinite, i.e., the line goes beyond the smallest
> and the largest data values. This seems not very right to me (not to mention
> it looks unaesthetic). I do not mean to imply that the straight-line
> behavior of my system is maintained throughout. I would like to limit the
> length of this line to the range of my data. However, I have not been able
> to figure out how to. Very disconcertingly, I found out that all the books
> that teach statistics using R seem to be drawing such infinite length-lines.
> I would appreciate any advice or suggestions. Regards,

ggplot2 only extends the line as far as your data:

qplot(x, y) + geom_smooth(method=lm)

You can find out more about ggplot2 at http://had.co.nz/ggplot2.



More information about the R-help mailing list