library(broom)
library(dplyr)
# Plot regression coefficients from a single model object
data(mtcars)
m1 <- lm(mpg ~ wt + cyl + disp, data = mtcars)
dwplot(m1) +
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 on the fly
m2 <- update(m1, . ~ . - disp)
dwplot(list(full=m1,nodisp=m2))
# Plot regression coefficients from multiple models in a tidy data.frame
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