Created
September 26, 2023 10:01
-
-
Save wviechtb/988f4382ba50928c856b251a0cd0df11 to your computer and use it in GitHub Desktop.
Diamond plot with metafor::forest() and metafor::addpoly()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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