Hi:
I suspect it's because you didn't define the y variable in geom_ribbon(). Here's an example with lm() [because it's a lot less work to get the confidence limits ;)]:
x <- 1:20
df <- data.frame(x, y = 2 + 0.8 * x + rnorm(20, s = 0.5))
m <- lm(y ~ x, data = df)
pp <- predict(m, interval = 'confidence')
head(pp, 1)
fit lwr upr
1 2.679853 2.151570 3.208136
g <- ggplot(df, aes(x, y)) + theme_bw()
g + geom_point(size = 2.5) + geom_line(aes(y = pp$fit), color = 'red', size = 1) +
geom_ribbon(data = pp, aes(x = df$x, ymin = lwr, ymax = upr))
# Error: ggplot2 doesn't know how to deal with data of class matrix
class(pp)
# [1] "matrix"
pp <- as.data.frame(pp)
g + geom_point(size = 2.5) + geom_line(aes(y = pp$fit), color = 'red', size = 1) +
geom_ribbon(data = pp, aes(x = df$x, ymin = lwr, ymax = upr))
# Error in eval(expr, envir, enclos) : object 'y' not found
g + geom_point(size = 2.5) +
geom_line(aes(y = pp$fit), color = 'red', size = 1) +
geom_ribbon(data = pp, aes(x = df$x, y = fit, ymin = lwr, ymax = upr),
alpha = 0.2)
# Works
HTH,
Dennis