Ggplot2 regression analysis
I've done some linear regressions. I tried to build it using this command.
layout(matrix(c(1,2,3,4),2,2)) plot(fit_ec_urban_franchise)
After that I have 4 "leftover" graphs versus the set, "scale against space", "normal qq" and "residuals against".
Is it possible to use ggplot2
to fit 4 graphs into one?
+3
source to share
1 answer
The solution is to use ggplot2::fortify
. Here is the code you can find on the help page ?fortify
. I add gridExtra
4 graphs to arrange.
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)
+3
source to share