Sunday, September 30, 2018

r - How to truncate the CI limits to the y range of the plot using stat_smooth

I am trying to use stat_smooth() to draw the confidence interval around a fitted line, e.g.:



Days <- c(rep(0,2),rep(10,4))
Resp <- c(1020, 955, 1599, 1220, 1264, 1273)
DummySet <- as.data.frame(cbind(Days,Resp))

a <- ggplot(data=DummySet, aes(Days, Resp))
a <- a + stat_smooth(method="lm", formula = y ~ x)

a


No y-limits



That's fine, but I face 2 problems when limiting the range of the y-axis. First, the CI shading is interrupted (instead of truncated) at the y-limit:



a <- a + ylim(c(800,1600))



Y Limits 800-1600



Second, if some points are outside of the y-range, they seem to be ignored for calculations (here 1599 seems to be ignored):



a <- a + ylim(c(800,1400))


Y Limits 800-1400



Instead, what I would like to see is the same as in the first graphic, only with truncated CI:

CI truncated



The problem may not be related to stat_smooth() but to a general misunderstanding of ggplot2, to which I am a newbie. Any hint would be appreciated.

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...