ggplot(data=Dane, aes(x=reg$fitted.values, y=reg$residuals))+
geom_smooth(method="lm", se=TRUE, level=0.95)+
theme(panel.background = element_rect(fill = "white", colour = "grey50"))+
geom_point()
Asked
Active
Viewed 2,226 times
2

Dan
- 11,370
- 4
- 43
- 68

Aneta Wlazłowska
- 21
- 2
-
Add some explanation to the question. – Rarblack Dec 03 '18 at 16:44
-
show you us your ms paint skills. – Andre Elrico Dec 03 '18 at 16:47
-
1Not directly related, but check [here](https://stackoverflow.com/q/32543340/5325862) on why you basically never want a `$` inside your `aes` calls. Folks can probably help if you make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), such as by including your data – camille Dec 03 '18 at 17:01
1 Answers
5
The following will use a subset of the built-in dataset iris
, with Species == "setosa"
.
Note that in order to get the predicted confidence bands, a linear model must be fit prior to plotting.
library(ggplot2)
data(iris)
subdf <- iris[iris$Species == "setosa", ]
pred <- predict(lm(Sepal.Width ~ Sepal.Length, subdf),
se.fit = TRUE, interval = "confidence")
limits <- as.data.frame(pred$fit)
ggplot(subdf, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point() +
theme(panel.background = element_rect(fill = "white",
colour = "grey50"))+
geom_smooth(method = "lm") +
geom_line(aes(x = Sepal.Length, y = limits$lwr),
linetype = 2) +
geom_line(aes(x = Sepal.Length, y = limits$upr),
linetype = 2)

Rui Barradas
- 70,273
- 8
- 34
- 66