dwplot
is a function for quickly and easily generating dot-and-whisker plots of regression models saved in tidy data frames.dwplot(df, alpha = 0.05, dodge_size = 0.15)
term
(names of independent variables), estimate
(corresponding coefficient estimates), std.error
(corresponding standard errors), and optionally model
(when multiple ggplot
object.dwplot
visualizes regression results saved in tidy data.frames by, e.g., tidy
as dot-and-whisker plots generated by ggplot
.
Because the function takes a data.frame as input, it is easily employed for a wide range of models, and because the output is a ggplot
object, it can be further customized with any additional arguments and layers supported by ggplot2
.library(broom)
# Plot regression coefficients from a single model
data(mtcars)
m1 <- lm(mpg ~ wt + cyl + disp, data = mtcars)
m1_df <- tidy(m1) # create data.frame of regression results
dwplot(m1_df) +
scale_y_discrete(breaks = 4:1, labels=c("Intercept", "Weight", "Cylinders", "Displacement")) +
theme_bw() + xlab("Coefficient") + ylab("") +
geom_vline(xintercept = 0, colour = "grey50", linetype = 2) +
theme(legend.position="none")
# Plot regression coefficients from multiple models
library(dplyr)
by_trans <- mtcars %>% group_by(am) %>%
do(tidy(lm(mpg ~ wt + cyl + disp, data = .))) %>% rename(model=am)
dwplot(by_trans, dodge_size = .05) +
scale_y_discrete(breaks = 4:1, labels=c("Intercept", "Weight", "Cylinders", "Displacement")) +
theme_bw() + xlab("Coefficient Estimate") + ylab("") +
geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
ggtitle("Predicting Gas Mileage, OLS Estimates") +
theme(plot.title = element_text(face="bold"),
legend.justification=c(1,0), legend.position=c(1,0),
legend.background = element_rect(colour="grey80"),
legend.title.align = .5) +
scale_colour_grey(start = .4, end = .8,
name = "Transmission",
breaks = c(0, 1),
labels = c("Automatic", "Manual"))
Run the code above in your browser using DataLab