Dear sir,
I have a problem about ggplot2.
I want to connect data point with ols result via vertical line, like The code listed below.
Can I transfer ..y.., the value calculated by stat_smooth, to geom_linerange directly?
I tried stat_smooth(...,geom="linerange",mapping(aes(ymin=pmin(myy,..y..),ymax=pmax(myy,..y..)) but it is not the result I want.
Thanks for any help.
It is not clear to me why the result is not what you want. I understand that you want a vertical line from each point to the fit line and that is what I see in your plot. Can you explain what you want to be different?
Here is another version of your code that is a little neater, in my opinion.
For the second code, I mean that ..y.. calculated by stat_smooth is the OLS fitted value.
To draw a vertical line, the OLS fitted value was calculated twice. Can I draw the vertical line Completely with ggplot only?
I cannot find a way to use the calculated values from geom_smooth in geom_linerange. If the main concern is to calculate the fit only once, I would store the predictions from the regression in the data frame and not use geom_smooth at all.
I modified my code a bit based on the answer you got on Stack Overflow.