Learn R Programming

dotwhisker (version 0.2.0.5)

dwplot: Dot-and-Whisker Plots of Regression Results

Description

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

Usage

dwplot(x, alpha = 0.05, dodge_size = 0.15, order_vars = NULL)

Arguments

x
Either a tidy data.frame (see 'Details'), a model object to be tidied with tidy, or a list of such model objects.
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 is small, while a higher value may be helpful when many models appear on the same plot.
order_vars
A vector of variable names that specifies the order in which the variables are to appear along the y-axis of the plot.

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.

Tidy data.frames to be plotted should include the variables term (names of predictors), estimate (corresponding estimates of coefficients or other quantities of interest), std.error (corresponding standard errors), and optionally model (when multiple models are desired on a single plot). In place of std.error one may substitute lb (the lower bounds of the confidence intervals of each estimate) and ub (the corresponding upper bounds).

For convenience, dwplot also accepts as input those model objects that can be tidied by tidy, or a list of such model objects.

Because the function takes a data.frame as input, it is easily employed for a wide range of models, including those not supported by tidy. And because the output is a ggplot object, it can easily be further customized with any additional arguments and layers supported by ggplot2. Together, these two features make dwplot extremely flexible.

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)
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