How can I plot the residuals of lm() with ggplot?

Lanza picture Lanza · Apr 20, 2016 · Viewed 22.6k times · Source

I would like to have a nice plot about residuals I got from an lm() model. Currently I use plot(model$residuals), but I want to have something nicer. If I try to plot it with ggplot, I get the error message:

ggplot2 doesn't know how to deal with data of class numeric

Answer

Gopala picture Gopala · Apr 20, 2016

Fortify is no longer recommended and might be deprecated according to Hadley.

You can use the broom package to do something similar (better):

library(broom)
y <-rnorm(10)
x <-1:10
mod <- lm(y ~ x)
df <- augment(mod)
ggplot(df, aes(x = .fitted, y = .resid)) + geom_point()