Plot and report X intercept from linear regression - R

nofunsally picture nofunsally · Jan 22, 2013 · Viewed 10.4k times · Source

I am using lm in r for linear regression. I would like to plot and report the x intercept. I know that I could use algebra and solve for x by setting y = 0, but is there a way to have r report it to me? Also, how can I 'tell' r to plot the x intercept? Would this just entail extending the x axis range to include it? Thanks.

# example r code
plot(y~x)
fit <- lm(y~x)
abline(fit)

Answer

Jonathan Christensen picture Jonathan Christensen · Jan 22, 2013

If you want to plot the x-intercept, extend the plot as you said. You might need to extend it in both the x and y dimensions (use xlim=c(0,100) and ylim=c(0,100) or whatever), and you should note that R does not plot lines for the axes. I supposed you can add them in manually with hline and vline if you want.

To get the numerical value of the x-intercept, you'll have to do algebra.

> coef(fit)
(Intercept)           x 
  0.8671534   0.4095524 

Gives the y-intercept and the slope, and you can easily find the x-intercept from there.