Da die Arten von Informationen in jedem Diagramm so unterschiedlich sind, müssen Sie drei Diagramme erstellen und sie miteinander verbinden.
library(ggplot2)
library(broom)
library(purrr)
library(gridExtra)
iris.lm <- lm(Sepal.Width ~ Sepal.Length*Species, iris)
p1 <- ggplot(augment(iris.lm), aes(Sepal.Length, Sepal.Width, color = Species)) +
theme_classic() + guides(color = F) +
labs(title = "Regression") +
theme(strip.background = element_blank(), strip.text = element_blank(),
panel.background = element_rect(color = "black")) +
stat_smooth(method = "lm", colour = "black") + geom_point(shape = 1) +
facet_grid(Species~.)
p2 <- ggplot(augment(iris.lm), aes(.fitted, .resid, color = Species)) +
theme_classic() + guides(color = F) +
labs(x = "Fitted values", y = "Residuals") +
theme(strip.background = element_blank(), strip.text = element_blank(),
panel.background = element_rect(color = "black")) +
stat_smooth(se = F, span = 1, colour = "black") + geom_point(shape = 1) +
facet_grid(Species~.)
p3 <- ggplot(augment(iris.lm), aes(sample = .resid/.sigma, color = Species)) +
theme_classic() + theme(panel.background = element_rect(color = "black")) +
labs(x = "Theoretical quantiles", y = "Standardized residuals", title = "Q-Q") +
geom_abline(slope = 1, intercept = 0, color = "black") +
stat_qq(distribution = qnorm, shape = 1) +
facet_grid(Species~.)
p <- list(p1, p2, p3) %>% purrr::map(~ggplot_gtable(ggplot_build(.)))
cbind.gtable(p[[1]], p[[2]], p[[3]]) %>% grid.arrange()
Um zu zeigen, was um die Daten Gerangel sie alle in einer ggplot
Aufruf wie folgt aussieht zu tun, hier ist eine andere Riss an sie. Dies ist eine minderwertige Lösung, da Sie geom_blank
mit den geänderten Daten aufrufen müssen, um einheitliche Skalen innerhalb des Plottyps zu erhalten, und Sie können die Plots nicht ordnungsgemäß mit ihren Achsen beschriften.
library(dplyr)
library(broom)
library(tidyr)
library(ggplot2)
iris.lm <- lm(Sepal.Width ~ Sepal.Length*Species, iris)
data_frame(type = factor(c("Regression", "F vs R", "Q-Q"),
levels = c("Regression", "F vs R", "Q-Q"))) %>%
group_by(type) %>%
do(augment(iris.lm)) %>%
group_by(Species) %>%
mutate(yval = case_when(
type == "Regression" ~ Sepal.Width,
type == "F vs R" ~ .resid,
type == "Q-Q" ~ .resid/.sigma
),
xval = case_when(
type == "Regression" ~ Sepal.Length,
type == "F vs R" ~ .fitted,
type == "Q-Q" ~ qnorm(ppoints(length(.resid)))[order(order(.resid/.sigma))]
),
yval.sm = case_when(
type == "Regression" ~ .fitted,
type == "F vs R" ~ loess(.resid ~ .fitted, span = 1)$fitted,
type == "Q-Q" ~ xval
)) %>% {
ggplot(data = ., aes(xval, yval, color = Species)) + geom_point() +
facet_wrap(~interaction(type, Species, sep = ": "), scales = "free") +
geom_line(aes(xval, yval.sm), colour = "black") +
geom_blank(data = . %>% ungroup() %>% select(-Species) %>%
mutate(Species = iris %>% select(Species) %>% distinct()) %>%
unnest(),
aes(xval, yval)) +
labs(x = "Sepal.Length: actual values, fitted values, theoretical quantiles",
y = "Sepal.Width: actual values, residuals, standardized residuals")}
zeigen Bitte ein reproduzierbares Beispiel und was haben Sie bisher versucht. Und bitte lesen Sie dies: https://stackoverflow.com/tour –