I would like to add the regression line equation and r squared value to my ggplot2
scatter plot.
I have found a similar question, which gives the code below, but it doesn't work when I force the regression through the intercept:
library(devtools)
source_gist("524eade46135f6348140")
df = data.frame(x = c(1:100))
df$y = 2 + 5 * df$x + rnorm(100, sd = 40)
ggplot(data = df, aes(x = x, y = y, label=y)) +
stat_smooth_func(geom="text",method="lm",hjust=0,parse=TRUE, formula=y~x-1) +
geom_smooth(method="lm",se=FALSE, formula=y~x-1) +
geom_point()
By adding formula=y~x-1
, the text displayed shows the coefficient as the intercept, with the intercept as NA
. Is there a fix for this?