Skip to content

Instantly share code, notes, and snippets.

@josherrickson
Created March 20, 2023 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josherrickson/5f264496e9e583207cecdcd8c056bceb to your computer and use it in GitHub Desktop.
Save josherrickson/5f264496e9e583207cecdcd8c056bceb to your computer and use it in GitHub Desktop.
library(forestplot)
data(mtcars)
mtcars$disp <- mtcars$disp/100
coefs <- summary(mod <- lm(mpg ~ disp + cyl + qsec + wt, data = mtcars))$coeff
cis <- confint(mod)
dat <- data.frame(cbind(coefs[, c(1, 4)], cis))
names(dat) <- c("mean", "pv", "lower", "upper")
dat <- dat[-1, ] # Drop intercept
dat$var <- rownames(dat)
# Inside `local` because of the <<-, to avoid polluting
fn <- local({
i <- 0
clrs <- ifelse(dat$pv < .05, "darkgreen",
ifelse(dat$pv < .1, "darkblue",
"darkgrey"))
function(..., clr.line, clr.marker) {
i <<- i + 1
fpDrawNormalCI(..., clr.line = clrs[i], clr.marker = clrs[i])
}
})
forestplot(dat,
labeltext = var,
fn.ci_norm = fn,
boxsize = .2
) |>
fp_set_style(txt_gp = fpTxtGp(ticks = gpar(fontfamily = "", cex = 1))) |>
fp_set_zebra_style("#EFEFEF")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment