library(ggplot2) fit <- lm(price ~ lotsize + I(lotsize^2), data = housing) prd <- data.frame(lotsize = seq(from = range(housing$lotsize)[1], to = range(housing$lotsize)[2], length.out = 100)) err <- predict(fit, newdata = prd, se.fit = TRUE) prd$lci <- err$fit - 1.96 * err$se.fit prd$fit <- err$fit prd$uci <- err$fit + 1.96 * err$se.fit ggplot(prd, aes(x = lotsize, y = fit)) + theme_bw() + geom_line() + geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity") + geom_point(data = housing, aes(x = lotsize, y = price))