Learn R Programming

dotwhisker (version 0.1.0.1)

add_brackets: Add Labelled Brackets to Group Predictors in a Dot-and-Whisker Plot

Description

add_brackets draws brackets along the y-axis beyond the plotting area of a dot-and-whisker plot generated by dwplot, useful for labelling groups of predictors

Usage

add_brackets(p, brackets, face = "italic")

Arguments

p
A dot-and-whisker plot generated by dwplot.
brackets
A list of brackets; each element of the list should be a character vector consisting of (1) a label for the bracket, (2) the name of the topmost variable to be enclosed by the bracket, and (3) the name of the bottommost variable to be enclosed by the brac
face
A typeface for the bracket labels; options are "plain", "bold", "italic", "oblique", and "bold.italic".

Value

  • The function returns a gtable object, which are viewed with grid.draw. To save, wrap the grid.draw command with pdf or png, etc., and dev.off. Alternately, the next release of ggplot2 (>1.0.1.9002) will enable ggsave for gtables; one can install the development version using devtools::install_github("hadley/ggplot2").

Examples

Run this code
library(broom)

data(mtcars)
m1 <- lm(mpg ~ wt + cyl + disp, data = mtcars)
m1_df <- tidy(m1) # create data.frame of regression results

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

two_brackets <- list(c("Engine", "cyl", "disp"), c("Not Engine", "(Intercept)", "wt"))

g <- p %>% add_brackets(two_brackets)

grid.draw(g)  # to display

#pdf("plot.pdf")  # to save (not run)
#grid.draw(g)
#dev.off()

Run the code above in your browser using DataLab