Skip to content

Instantly share code, notes, and snippets.

@medewitt
Created September 15, 2019 20:45
Show Gist options
  • Save medewitt/381336670c3a8c40cdf5dc00318d27c3 to your computer and use it in GitHub Desktop.
Save medewitt/381336670c3a8c40cdf5dc00318d27c3 to your computer and use it in GitHub Desktop.
# Coefficient Plot Implemented in ggplot2 From Faraway's Linear Models with R
#'@description Building a Gelman-esque coefplot in ggplot based on the code
#' in Julian Faraway's Linear Models with R
#'@param fit a lm or glm object
#'@param ci the confidence levels to consider
#'@export
ggcoef_plot <- function(fit, ci = 0.95){
stopifnot(class(fit) %in% c("lm", "glm"))
edf <- data.frame(coef(fit), confint(fit, level = ci))[-1,]
names(edf) <- c("Estimate", "lb", "ub")
p <- ggplot2::ggplot(edf,
ggplot2::aes(y = Estimate, ymin = lb, ymax = ub,
x = row.names(edf)))+
ggplot2::geom_pointrange()+
ggplot2::coord_flip()+
ggplot2::geom_hline(yintercept = 0, col = "orange", alpha = .8)+
ggplot2::theme_minimal()+
ggplot2::xlab("Predictor")
print(p)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment