Free Access Week - Data Engineering + BI
Data Engineering and BI courses are free this week!
Free Access Week - Jun 2-8

dotwhisker (version 0.1.0.1)

dwplot: Dot-and-Whisker Plots of Regression Coefficients from Tidy Data Frames

Description

dwplot is a function for quickly and easily generating dot-and-whisker plots of regression models saved in tidy data frames.

Usage

dwplot(df, alpha = 0.05, dodge_size = 0.15)

Arguments

df
A data.frame including the variables term (names of independent variables), estimate (corresponding coefficient estimates), std.error (corresponding standard errors), and optionally model (when multiple
alpha
A number setting the criterion of the confidence intervals. The default value is .05, corresponding to 95-percent confidence intervals.
dodge_size
A number (typically between 0 and 0.3) indicating how much vertical separation should be between different models' coefficients when multiple models are graphed in a single plot. Lower values tend to look better when the number of independent variables i

Value

  • The function returns a ggplot object.

Details

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.

References

Kastellec, Jonathan P. and Leoni, Eduardo L. 2007. "Using Graphs Instead of Tables in Political Science." Perspectives on Politics, 5(4):755-771.

Examples

Run this code
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