能否在autoplot之外使用ggfortify?求替代方案与最佳实践
Great question! I totally get wanting to dig into the details instead of just relying on a pre-built shortcut—let’s break this down clearly.
Is autoplot() the optimal choice?
Short answer: It depends on your goal.
For quick, standardized model diagnostics: Yes,
autoplot()from ggfortify is absolutely the optimal choice. It wraps up all the tedious work of extracting residuals, fitted values, theoretical quantiles, leverage scores, etc., from yourlmobject, and spits out a grid of 4-6 standard diagnostic plots in one line of code. This is perfect for initial model exploration when you just need to check assumptions quickly.For highly customized plots: No, you’ll want to go manual.
autoplot()is great for defaults, but if you need to tweak colors, themes, add annotations, combine plots in non-standard ways, or only generate a subset of diagnostic plots, it’s easier to build the plots yourself with ggplot2 directly.
Alternatives: Manual plotting with ggplot2
To avoid autoplot(), you’ll need to extract the relevant statistics from your lm object and feed them into ggplot2. The broom package makes this extraction super straightforward (though you can also pull values directly from the lm object if you prefer).
Step 1: Extract model data
Use broom::augment() to create a data frame that includes your original data plus fitted values, residuals, standardized residuals, and more:
library(ggplot2) library(broom) # Fit your linear model my_model <- lm(mpg ~ wt + hp, data = mtcars) # Extract augmented data with model stats augmented_data <- augment(my_model)
Step 2: Build individual diagnostic plots
Here are the most common diagnostic plots, built manually with ggplot2:
1. Residuals vs Fitted Values
Check for non-linear patterns or heteroscedasticity:
ggplot(augmented_data, aes(x = .fitted, y = .resid)) + geom_point(color = "darkslateblue", alpha = 0.7) + geom_hline(yintercept = 0, linetype = "dashed", color = "firebrick") + labs(title = "Residuals vs Fitted Values", x = "Fitted Values", y = "Residuals") + theme_minimal()
2. Normal Q-Q Plot
Check if residuals follow a normal distribution:
ggplot(augmented_data, aes(sample = .std.resid)) + stat_qq(color = "darkslateblue", alpha = 0.7) + stat_qq_line(color = "firebrick") + labs(title = "Normal Q-Q Plot", x = "Theoretical Quantiles", y = "Standardized Residuals") + theme_minimal()
3. Scale-Location Plot
Another check for heteroscedasticity (spread of residuals):
ggplot(augmented_data, aes(x = .fitted, y = sqrt(abs(.std.resid)))) + geom_point(color = "darkslateblue", alpha = 0.7) + geom_smooth(method = "loess", se = FALSE, color = "firebrick") + labs(title = "Scale-Location Plot", x = "Fitted Values", y = expression(sqrt("|Standardized Residuals|"))) + theme_minimal()
4. Residuals vs Leverage
Identify influential observations (using Cook’s Distance):
# Add leverage and Cook's Distance to the data frame augmented_data$leverage <- hatvalues(my_model) augmented_data$cooks_distance <- cooks.distance(my_model) ggplot(augmented_data, aes(x = leverage, y = .std.resid)) + geom_point(aes(size = cooks_distance), color = "darkslateblue", alpha = 0.7) + geom_hline(yintercept = 0, linetype = "dashed", color = "firebrick") + labs(title = "Residuals vs Leverage", x = "Leverage", y = "Standardized Residuals", size = "Cook's Distance") + theme_minimal()
Final Takeaway
- Stick with
autoplot()if you need fast, standard diagnostic plots for initial model checks—it’s efficient and covers all the basics. - Use manual ggplot2 plotting when you need full control over plot customization, or when you only want to focus on specific diagnostic metrics.
内容的提问来源于stack exchange,提问作者Margaret

