library(ggplot2)
df <- as.data.frame(matrix(rnorm(60*2, mean=3,sd=1), 60, 2))
colnames(df) <- c("A", "B")
cf1 <- coef(lm(B~A, data=df))
ggplot(df, aes(A,B)) +
geom_point() +
stat_smooth(method = "lm", color="red", fill="red", alpha=0.1, fullrange=TRUE) +
geom_abline(intercept = cf1[1], slope = cf1[2], lty="dashed", col="green")

I want to limit geom_line to the same range as stat_smooth (which seems to be defined by xmax / xmin). The xlim argument did not help (suggested here). In a real-life application, geom_line geometry and interception will be extracted from model updates, so they will be slightly different. Thanks.
nouse source
share