2

enter image description here

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()
Dan
  • 11,370
  • 4
  • 43
  • 68
  • 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
  • 1
    Not 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 Answers1

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)

enter image description here

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