Is there an easy way to extend the dashed line from the end of the solid regression line to the predicted value?
The following is my main attempt:
x = rnorm(10) y = 5 + x + rnorm(10,0,0.4) my_lm <- lm(y~x) summary(my_lm) my_intercept <- my_lm$coef[1] my_slope <- my_lm$coef[2] my_pred = predict(my_lm,data.frame(x = (max(x)+1))) ggdf <- data.frame( x = c(x,max(x)+1), y = c(y,my_pred), obs_Or_Pred = c(rep("Obs",10),"Pred") ) ggplot(ggdf, aes(x = x, y = y, group = obs_Or_Pred ) ) + geom_point( size = 3, aes(colour = obs_Or_Pred) ) + geom_abline( intercept = my_intercept, slope = my_slope, aes( linetype = obs_Or_Pred ) )
This does not give the result that I was hoping to see. I looked at other answers on SO and did not see anything simple. The best I came up with is:
ggdf2 <- data.frame( x = c(x,max(x),max(x)+12), y = c(y,my_intercept+max(x)*my_slope,my_pred), obs_Or_Pred = c(rep("Obs",8),"Pred","Pred"), show_Data_Point = c(rep(TRUE,8),FALSE,TRUE) ) ggplot(ggdf2, aes(x = x, y = y, group = obs_Or_Pred ) ) + geom_point( data = ggdf2[ggdf2[,"show_Data_Point"],] ,size = 3, aes(colour = obs_Or_Pred) ) + geom_smooth( method = "lm", se=F, aes(colour = obs_Or_Pred, linetype=obs_Or_Pred) )
This gives a result that is correct, but I had to include an extra column that determines if I want to show data points. If I do not, I get the second of these two graphs, which has an additional point at the end of the regression line:

Is there an easier way to tell ggplot to predict one point from a linear model and draw a dashed line?