Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting a regression line through the origin

Tags:

r

ggplot2

I am plotting some data series along with regression lines using this code:

ggplot(dt1.melt, aes(x=lower, y=value, group=variable, colour=variable)) +
    geom_point(shape=1) +    
    geom_smooth(method=lm,  
            se=FALSE)   

However, I need to constrain the regression line to be through the origin for all series - in the same way as abline(lm(Q75~-1+lower,data=dt1)) would achieve on a standard R plot.

Can anyone explain how to do this in ggplot ?

like image 685
Joe King Avatar asked Sep 29 '12 08:09

Joe King


People also ask

What is regression through the origin?

Regression through the Origin means that you purposely drop the intercept from the model. When X=0, Y must = 0. The thing to be careful about in choosing any regression model is that it fit the data well.

Does a regression line have to go through the origin?

Don't force your regression through zero just because you know the true intercept has to be zero. Regression through the origin is when you force the intercept of a regression model to equal zero.


1 Answers

You need to specify this in the formula argument to geom_smooth:

... + geom_smooth(method=lm, se=FALSE, formula=y~x-1)
like image 72
James Avatar answered Sep 20 '22 13:09

James