Skip to content

Instantly share code, notes, and snippets.

@wviechtb
Created September 26, 2023 10:01
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 wviechtb/988f4382ba50928c856b251a0cd0df11 to your computer and use it in GitHub Desktop.
Save wviechtb/988f4382ba50928c856b251a0cd0df11 to your computer and use it in GitHub Desktop.
Diamond plot with metafor::forest() and metafor::addpoly()
# inspired by: https://github.com/mjskay/uncertainty-examples/blob/master/diamond_plot.md
# to see if we can abuse metafor::forest() and metafor::addpoly() to create such a plot
library(palmerpenguins)
library(metafor)
set.seed(1234)
dat <- penguins
dat <- dat[!is.na(dat$sex),]
dat$row <- as.numeric(interaction(dat$sex, dat$species))
dat$row <- jitter(dat$row, amount=0.15)
dat$vars <- 0
cols.base <- c("#f8766d", "#00bfc4")
cols <- apply(col2rgb(cols.base), 2, function(x) rgb(x[1], x[2], x[3], 100, maxColorValue=255))
dat$col <- cols[as.numeric(dat$sex)]
png("forest_diamond_plot.png", res=250, width=2500, height=1600, type="cairo")
f <- with(dat, forest(body_mass_g, vars, xlim=c(1000,7500), rows=row, ylim=c(0.5,6),
slab=NA, annotate=FALSE, top=0, psize=1, pch=16, col=col, cex=1,
efac=0, lty=c("blank","blank"), xlab="Body Mass"))
text(f$xlim[1], c(1.5, 3.5, 5.5), levels(dat$species), pos=4)
legend("right", pch=16, col=cols.base, legend=c("female", "male"), bty="n")
res <- lm(body_mass_g ~ 0 + sex:species, data = dat)
summary(res)
cols <- apply(col2rgb(cols.base), 2, function(x) rgb(x[1], x[2], x[3], 180, maxColorValue=255))
addpoly(coef(res), diag(vcov(res)), rows=1:6, col=rep(cols, 3), efac=3, lwd=1.2)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment