Search code examples
rr-forestplotmetafor

How can I move the metafor plot without moving the rest of the text?


I am trying to get my forest plot centered in the white space using the metafor package. I am trying to move just the plot alone without moving the text columns. Does anyone have any suggestions? The plot is coded using the forest() function with the metafor package.


library(metafor)

labela <- as.character(c("Sex (male vs. female)", "   50 to <60", "   60", 
                         "   <18.5", "   25 to <30", "   30 to <35", "   35 to <40", 
                         "   40", "Cancer diagnosis (yes vs. no)", "   Previous", "   Current", 
                         "   Special occasions only", "   One to three times a month", 
                         "   Once or twice a week", "   Three or four times a week", 
                         "   Daily or almost daily", "Oral contraceptive pill (ever vs. never)", 
                         "Hormone replacement therapy (ever vs. never)", "Fracture in the last 5 years (yes vs. no)", "Previous deep vein thrombosis (yes vs. no)", "Previous pulmonary embolism (yes vs. no)"))
labela <- factor(labela, levels = unique(labela))
hazardsa <- log(c(1.44, 1.57, 2.46, 0.78, 1.35, 1.78, 2.29, 3.30, 1.58, 1.03, 1.24, 0.99, 0.87, 0.89, 0.74, 0.80, 0.88, 1.07, 1.19, 3.34, 1.82))
lowera <- log(c(1.29, 1.41, 2.22, 0.43, 1.25, 1.62, 2.02, 2.81, 1.45, 0.96, 1.12, 0.87, 0.76, 0.79, 0.65, 0.71, 0.79, 0.97, 1.08, 2.96, 1.51))
uppera <- log(c(1.60, 1.75, 2.73, 1.40, 1.47, 1.95, 2.60, 3.88, 1.73, 1.10, 1.36, 1.12, 0.99, 1.00, 0.83, 0.90, 0.99, 1.18, 1.31, 3.77, 2.20))
patient_numa <- c(229134, 167158, 217493, 2626, 212127, 87557, 24994, 9704, 41700, 173070, 52979, 58012, 55858, 129297, 115445, 101774, 220446, 103919, 47466, 9323, 3955)
event_numa <- c(2685, 1347, 2935, 11, 2007, 1145, 415, 227, 674, 1863, 572, 675, 525, 1252, 937, 985, 1565, 1030, 559, 440, 175)
pa <- c("<0.001", NA, NA, NA, NA, NA, NA, NA, "<0.001","0.47", "<0.001", "0.87", "0.04", "0.05", "<0.001", "<0.001", "0.03", "0.15", "<0.001", "<0.001", "<0.001")
forestplota <- data.frame(labela, hazardsa, lowera, uppera, event_numa, patient_numa, pa)
forestplota$patient_numa <- rev(forestplota$patient_numa)

par(mar=c(4,4,1,2))
pdf(file = "figure1.pdf", width = 15, height = 8.5)
pdf.options(encoding='ISOLatin2.enc')
forest(rev(hazardsa), ci.lb = rev(lowera), ci.ub = rev(uppera), slab = rev(labela), xlim = c(-5,7), 
       xlab = "Adjusted Hazard Ratio for Venous Thromboembolism Event", 
       refline = 0, annotate = T, ylim = c(-1, 29), ilab = cbind(forestplota$patient_numa, rev(event_numa), rev(pa)), 
       ilab.xpos=c(-1,-0.25, 6.5), at = log(c(0.25, 0.5, 1, 2, 4, 6)), rows = c(2:11,13:14, 16:21, 23:24,26), 
       cex = 1, yaxs = "i", textpos = c(NA, 6), atransf = exp)
text(-5, 27.29, pos = 4, "Variable")
text(-1.3, 27.67, pos = 4, "No. of \nPatients")
text(-0.5, 27.67, pos = 4, "No. of \nEvents")
text(4.82, 27.69, pos = 4, "Hazard Ratio \n(95% CI)")
text(6.2, 27.32, pos = 4, "P-value")
text(6.21, 23.5, pos = 4, "<0.001")
text(6.21, 19, pos = 4, "<0.001")

text(-5, c(25, 22, 15, 12), pos=4, c("Age, years (vs. <50)", "Body mass index* (vs. 18.5 to <25)", "Smoking (vs. never)", "Alcohol (vs. never)"))
dev.off()



Solution

  • You need to adjust xlim = c(-5,7). Maybe xlim = c(-8,5) and then of course you need to adjust the other positions, like ilab.xpos=c(-4, -2.25, 4.5) and textpos = c(NA, 4), and then position the rest.

    P.S.: The par(mar=c(4,4,1,2)) should go inside the pdf() [...] dev.off() part.