Search code examples
rexpressionscientific-notationr-forestplotmetafor

Adding Labels in Scientific Notation to Forest Plots Using the metafor package


So I'm doing a meta-analysis using the meta.for package in R. I am preparing figures for publication in a scientific journal and i would like to add p-values to my forest plots but with scientific annotation formatted as

x10-04
rather than standard

e-04

However the argument ilab in the forest function does not accept expression class objects but only vectors

Here is an example :

library(metafor)
data(dat.bcg)

## REM 
res <- rma(ai = tpos, bi = tneg, ci = cpos, di = cneg, data = dat.bcg,
           measure = "RR",
           slab = paste(author, year, sep = ", "), method = "REML")
# MADE UP PVALUES
set.seed(513)
p.vals <- runif(nrow(dat.bcg), 1e-6,0.02)

# Format pvalues so only those bellow 0.01 are scientifically notated
p.vals <- ifelse(p.vals < 0.01, 
                 format(p.vals,digits = 3,scientific = TRUE,trim = TRUE),
                 format(round(p.vals, 2), nsmall=2, trim=TRUE))

## Forest plot
forest(res, ilab = p.vals, ilab.xpos = 3, order = "obs", xlab = "Relative Risk")

enter image description here

I want the scientific notation of the p-values to be formatted as

x10-04
All the answers to similar questions that i've seen suggest using expression() but that gives Error in cbind(ilab) : cannot create a matrix from type 'expression' which makes sense because the help file on forest specifies that the ilab argument should be a vector.

Any ideas on how I can either fix this or work around it?


Solution

  • A hacky solution would be to

    forest.rma <- edit(forest.rma)
    

    Go to line 574 and change

    ## line 574
    text(ilab.xpos[l], rows, ilab[, l], pos = ilab.pos[l],
    

    to

    text(ilab.xpos[l], rows, parse(text = ilab[, l]), pos = ilab.pos[l],
    

    fix your p-values and plot

    p.vals <- gsub('e(.*)', '~x~10^{"\\1"}', p.vals)
    forest(res, ilab = p.vals, ilab.xpos = 3, order = "obs", xlab = "Relative Risk")
    

    enter image description here