r - Saving Forest Plots (metafor) -
i have forest plot (using metafor) number of rows change depending on input conditions. code saves plot after each run using:
dev.copy(png,'myplot.png') dev.off()
depending on number of rows in plot, data can squashed. anymore 30 rows , data becomes unreadable, see below examples. first chart readable, second chart useless , gets worse want include more data.
is there way save charts height automatically update fit in rows correctly? there no specific need chart square, in fact envisage rectangular fit in data. chart code included @ base of question reference.
forest(x = final$avgpts, ci.lb = final$min, ci.ub = final$max, slab = final$players, ilab = final$value, ilab.xpos = max(final$max)+10,ilab.pos =4, alim = c(min(final$min)-5, max(final$max)+5), xlim = c(min(final$min)-170, 2*(max(final$max)+5)), xlab = "moneyball points spread", efac = 1, cex = 0.75, mgp = c(1, 1, 0), refline=mean(final$avgpts),digits=1,col="dark blue",pch = 19, main=paste("2016 moneyball summary (position =",pos,"and min average >",points,")"))
instead of using dev.copy()
, this:
- determine
k
(number of studies = number of rows) included in forest plot. - open plotting device
png()
height
attribute appropriate function ofk
(the largerk
, larger valueheight
). - draw forest plot.
- close plotting device
dev.off()
. - profit!
step 2. bit tricky. trial , error, found height = 200 + 40*k^.85
seems work alright. in forest()
, want use cex=1
character/symbol expansion factor stays constant , yaxs="i"
otherwise 4% padding start strange when k
large. want adjust ylim
there bit more space between x-axis , first row. , finally, have make efac
appropriate decreasing function of k
. again, after trial , error, found efac=30/(k+10)
seems work.
here code test out:
library(metafor) ks <- c(5, 10, 20, 40, 80, 160, 320) (k in ks) { vi <- runif(k, .05, 1) yi <- rnorm(k, 0, sqrt(vi)) png(paste0("forest_k=", formatc(k, format="f", flag="0", width=3, digits=0), ".png"), height = 200 + 40*k^.85) forest(yi, vi, xlim=c(-10,10), alim=c(-4,4), efac=30/(k+10), cex=1, yaxs="i", ylim=c(0,k+3)) text(-10, k+2, "study", pos=4) text( 10, k+2, "estimate [95% ci]", pos=2) dev.off() }
okay, in end, bit of hackish solution, appears work.
there https://stackoverflow.com/a/28239750/2615367 may more elegant have check how make work.
Comments
Post a Comment