Skip to content

Instantly share code, notes, and snippets.

@mjadkins
Last active September 21, 2015 19:34
Show Gist options
  • Select an option

  • Save mjadkins/b138c1688556bed2ddda to your computer and use it in GitHub Desktop.

Select an option

Save mjadkins/b138c1688556bed2ddda to your computer and use it in GitHub Desktop.
ggplot2_coefplot
# Load libraries
lapply(c("datasets","arm","ggplot2","RColorBrewer","grid"),library, character.only=TRUE)
# Set working directory and load data
setwd("~/R")
data(mtcars)
# Fit a linear model of mpg ~ wt + disp + hp + qsec
fit.1 <-lm(mpg ~ wt + disp + hp + qsec, data=mtcars)
display(fit.1)
# Rescale the model
fit.2 <- standardize(fit.1)
# Extract parameters
coefs <- summary (fit.2)$coef[, 1]
sds <- summary(fit.2)$coef[, 2]
# Generate lower and upper bounds
coef.min <-coefs-(1.96*sds)
coef.max <-coefs+(1.96*sds)
coef.sd.min <-coefs-sds
coef.sd.max <-coefs+sds
# Generate variable names
longnames <-c("Intercept","Weight", "Displacement", "Horsepower", "Quarter-mile time")
# Create data frame
d <-data.frame(coefs,sds,coef.min, coef.max, coef.sd.min,coef.sd.max, longnames)
# Order parameter names
d$longnames.1 <- as.character(d$longnames)
d$longnames <- factor(d$longnames.1, levels=rev(unique(d$longnames.1)))
# Generate relevant shades
palette <- brewer.pal("Greys", n=9)
color.background = palette[2]
color.grid.major = palette[3]
color.axis.text = palette[6]
color.axis.title = palette[7]
color.title = palette[9]
# Plot
k <-ggplot() +
geom_pointrange(data=d, mapping=aes(x=d$longnames,y=d$coefs, ymin=d$coef.min, ymax=d$coef.max), position="identity", width=0.25, size=0.5, color="black") +
geom_pointrange(data=d, mapping=aes(x=d$longnames,y=d$coefs, ymin=d$coef.sd.min, ymax=d$coef.sd.max), position="identity", width=0.5, size=1, color="black") +
theme_bw() +
# Set the entire chart region to a light gray color
theme(panel.background=element_rect(fill=color.background, color=color.background)) +
theme(plot.background=element_rect(fill=color.background, color=color.background)) +
theme(panel.border=element_blank(), axis.line=element_line(), axis.line.y=element_blank()) +
# Format the grid
theme(panel.grid.major=element_line(colour=color.background,size=.75)) +
theme(axis.ticks=element_blank()) +
theme(axis.line.x =element_line(colour="#535353", size=.75))+
# Dispose of the legend
theme(legend.position="none") +
# Set title and axis labels, and format these and tick marks
ggtitle("Regression Estimates") +
theme(plot.title = element_text(size = 20, hjust=-.8, vjust=2.6, face="bold"))+
xlab("") +
ylab("Change in miles per gallon (US)") +
theme(axis.text.x=element_text(size=11,colour="#535353",face="bold")) +
theme(axis.text.y=element_text(size=11,colour="#535353",face="bold")) +
theme(axis.title.y=element_text(size=11,colour="#535353",face="bold",vjust=1.5)) +
theme(axis.title.x=element_text(size=11,colour="#535353",face="bold",vjust=-.5)) +
# Big bold line at y=0
geom_hline(yintercept=0,size=1.2, alpha=0.2,colour="#535353", linetype="twodash")+
# Plot margins
theme(plot.margin = unit(c(1, 1, .5, .7), "cm"))
k + coord_flip()
ggsave("coefplot_mtcars.png", dpi=150, width=7, height=3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment