jjjjjjj jjjjjjj
jjjjjjj jjjjjjj

Reputation: 129

Regression analysis ggplot2

I have done multiple linear regression. I have tried to plot it with this command.

layout(matrix(c(1,2,3,4),2,2)) 
plot(fit_ec_urban_franchise)

After that I have 4 plots 'residuals vs. fitted', 'scale vs. location', 'normal q-q' and 'residuals vs.leverage'.

Is it possible use ggplot2 to fit the 4 plots into one?

Upvotes: 3

Views: 1441

Answers (1)

scoa
scoa

Reputation: 19867

A solution is to use ggplot2::fortify. Here is the code you can find on its help page ?fortify. I'm adding gridExtra to arrange the 4 plots together.

library(ggplot2)
library(gridExtra)

mod <- lm(mpg ~ wt + cyl, data = mtcars)

p1 <- qplot(.fitted, .resid, data = mod) +
  geom_hline(yintercept = 0) +
  geom_smooth(se = FALSE)

p2 <- qplot(sample =.stdresid, data = mod, stat = "qq") + geom_abline()

p3 <- qplot(.fitted, sqrt(abs(.stdresid)), data = mod) + geom_smooth(se = FALSE)

p4 <- qplot(.hat, .stdresid, data = mod) + geom_smooth(se = FALSE)


grid.arrange(p1,p2,p3,p4)

enter image description here

Upvotes: 4

Related Questions