Hi All, I have a scatter plot produced using ggplot2 and I want to add the regression line to this scatter plot. I suppose I can use geom_smooth() to do this, but for the sake of learning ( I am new both to R and ggplot2), I want to try and add it as a function (something that curve() does in the standard R plotting). I did some search and found that stat_function() can be used for this. But somehow it is not working. The following is my code. Can you please tell me where I am going wrong and what the correct code would be:
reg1 <- lm(kid_score ~ mom_hs, data=iqdata) scatter <- ggplot() + geom_point(data=iqdata, aes(x=as.factor(mom_hs), y=kid_score) ) + xlab("Mother's High School Status") + ylab("Children's Test Score") + stat_function(fun=function(x) coef(reg1)[1] + coef(reg1)[2] * x) I understand that you probably do not have the data I am using (I am trying this out from Gelman Hill's book on Multilevel models). But I was hoping you may be able to point out the error without the data. Thanks for your help. ______________________________________________ R-help@r-project.org 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.