3

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.

albifrons
  • 303
  • 2
  • 9
  • Thanks David, the easiest solution however is to use coord_cartesian() instead of ylim() as indicated by joran. Thanks joran! – albifrons Jul 12 '15 at 14:33

0 Answers0