Search code examples
rtextmetafor

How to add text to forest plots


I am trying to create a forest plot using this dataframe with three columns: studies is the name of the study, n is the number of samples in the study and accuracy is % accuracy:

> dput(for.meta)
structure(list(studies = structure(c(11L, 5L, 13L, 3L, 14L, 12L, 
4L, 6L, 8L, 2L, 10L, 1L, 7L, 9L), .Label = c("EMTAB292", "GSE10327", 
"GSE12992", "GSE21140", "GSE30074", "GSE37382", "GSE41842", "GSE49243", 
"GSE50161", "GSE50765", "GSE62803", "GSE67850", "GSE73038", "GSE74195"
), class = "factor"), n = c(8L, 30L, 46L, 40L, 30L, 22L, 103L, 
50L, 58L, 62L, 12L, 19L, 19L, 24L), accuracy = c(85.71, 93.3, 
93.3, 94.9, 95, 95.5, 96.8, 97.8, 98.3, 98.3, 100, 100, 100, 
100), label = c("GSE62803 (n = 8)", "GSE30074 (n = 30)", "GSE73038 (n = 46)", 
"GSE12992 (n = 40)", "GSE74195 (n = 30)", "GSE67850 (n = 22)", 
"GSE21140 (n = 103)", "GSE37382 (n = 50)", "GSE49243 (n = 58)", 
"GSE10327 (n = 62)", "GSE50765 (n = 12)", "EMTAB292 (n = 19)", 
"GSE41842 (n = 19)", "GSE50161 (n = 24)")), row.names = c("GSE62803", 
"GSE30074", "GSE73038", "GSE12992", "GSE74195", "GSE67850", "GSE21140", 
"GSE37382", "GSE49243", "GSE10327", "GSE50765", "EMTAB292", "GSE41842", 
"GSE50161"), class = "data.frame")

Here is the code to create the forest plot:

library(metafor)
colfunc <- colorRampPalette(c("red", "darkred"))
metafor::forest(for.meta$accuracy, for.meta$n, 
                slab = for.meta$label, cex=.9,
                xlab = "Accuracy (%)", col = colfunc(14), 
                main = "Accuracy across 14 datasets\n(Median = 97.3%)", top = 3, refline = 97)
text(x = 70, y = 50, "Dataset", pos=4)
text(x = 90, y = 75, "Accuracy", pos = 3, font = 4)

The plot is created but no text (Label on top) shows up for the Dataset column and the Accuracy column.

enter image description here


Solution

  • The y-axis positions for the text you are adding are way too large. This should work:

    metafor::forest(for.meta$accuracy, for.meta$n, 
                    slab = for.meta$label, cex=.9,
                    xlab = "Accuracy (%)",
                    main = "Accuracy across 14 datasets\n(Median = 97.3%)", top = 3,
                    refline = 97,
                    xlim = c(30, 165))
    
    text(x =  30, y = 16, "Dataset",  pos=4)
    text(x = 165, y = 16, "Accuracy", pos = 2, font = 4)
    

    Note that I set the x-axis limits in the forest plot, which makes it easier to set the same limits for the text to add. Also, I had to remove col=colfunc(14) to get this to run. Not sure where that function is from.