0
votes

I have used the following code successfully for a lot of my work but it is not working anymore and outputs blank files (instead of forest plots). Not sure if there is a bug within the forestplot function or it is a glitch in my code.

I have added the dataframe and code I used below.

library(forestplot)
tiff('subset2.tiff',units="cm",width=16,height=16,res=300)

tt1=cbind(results_1_2_3_4_5_subset2$Outcome)




forestplot <- forestplot(tt1, mean = cbind(results_1_2_3_4_5_subset2$coef.1,results_1_2_3_4_5_subset2$coef.2,results_1_2_3_4_5_subset2$coef.3,results_1_2_3_4_5_subset2$coef.4,results_1_2_3_4_5_subset2$coef.5), 
                         lower = cbind(results_1_2_3_4_5_subset2$lower.1, results_1_2_3_4_5_subset2$lower.2,results_1_2_3_4_5_subset2$lower.3,results_1_2_3_4_5_subset2$lower.4,results_1_2_3_4_5_subset2$lower.5),
                         upper = cbind(results_1_2_3_4_5_subset2$upper.1, results_1_2_3_4_5_subset2$upper.2,results_1_2_3_4_5_subset2$upper.3,results_1_2_3_4_5_subset2$upper.4,results_1_2_3_4_5_subset2$upper.5),
                         
                         lwd.xaxis=1,
                         
                         
                         xlog=FALSE,
                         
                         graph.pos = 2,
                         
                         
                         zero = 0,
                         lwd.zero = 1,
                         
                         boxsize = 0.06,
                         
                         
                         lty = 1,
                         
                         col = fpColors(lines=c("magenta","purple","black","blue","red"),box=c("magenta","purple","black","blue","red"),zero="black"), 
                         
                         legend = c("Childhood", "Early adulthood","45-60 years","60-68 years","68-81 years"),
                         
                         lwd.ci = 1,  ##area of the CI line,
                         
                         xticks = c(-0.60,-0.40,0,0.20,0.40,0.60),
                         
                         
                         
                         xlab ="SD change ",las = 10,
                         
                         is.summary = FALSE,
                         
                         
                         txt_gp = fpTxtGp(xlab = gpar(cex=0.67),##changes font size of xlabel
                                          
                                          ticks = gpar(cex=0.67),
                                          legend=gpar(cex=0.67),
                                          
                                          
                                          label = list(gpar(cex=0.67),
                                                       
                                                       gpar(0.67))),
                         
                         
                         
                         hrzl_lines=list("2" = gpar(lty=2, lwd=0.5), "3" = gpar(lty=2, lwd=0.5),"4" = gpar(lty=2, lwd=0.5), "5" = gpar(lty=2, lwd=0.5),"6" = gpar(lty=2, lwd=0.5),"7" = gpar(lty=2, lwd=0.5)),
                         legend_args=fpLegend(pos = list(x=0.5,y=-0.11, "inset"=c(-1,1), "align"="horizontal"), gpar(lwd=0.5, lty=1),padding=unit(ifelse(!is.null(forestplot), 0.6, 0.6), "mm")))




dev.off()


dput(results_1_2_3_4_5_subset1)
structure(list(Outcome = c("Caudal middle frontal (Frontal)", 
"Frontal pole (Frontal)", "Lateral orbitofrontal (Frontal)", 
"Medial orbitofrontal (Frontal)", "Paracentral (Frontal)", "Pars opercularis (Frontal)", 
"Pars orbitalis (Frontal)", "Pars triangularis (Frontal)", "Precentral (Frontal)", 
"Rostral middle frontal (Frontal)", "Superior frontal (Frontal)", 
"Cuneus (Occipital)", "Lateral occipital (Occipital)", "Lingual (Occipital)", 
"Pericalcarine (Occipital)"), coef.1 = c(0.00596106575281553, 
-0.0905250217811289, 0.0660569263073628, 0.0481044143308602, 
0.0456783991989004, 0.0235670041390381, 0.0338255824113253, 0.0214182478793023, 
0.00908022806533528, -0.0238442630112621, 0.0223193392140302, 
-0.0241908366015421, -0.0063076393430955, -0.0319540850238135, 
-0.038608297957189), lower.1 = c(-0.110071772272919, -0.365912396617595, 
-0.050391800026708, -0.025577130962662, -0.0553131450086836, 
-0.186179332698401, -0.024953298500158, -0.129410578610542, -0.0801971287907857, 
-0.0683443120032106, -0.0740974336018582, -0.108200274885407, 
-0.120191721109095, -0.25681103039746, -0.180287581663642), upper.1 = c(0.121924589060494, 
0.184862353055337, 0.182505652641434, 0.121785959624382, 0.146669943406484, 
0.233382655694534, 0.0926737780408647, 0.172247074369146, 0.0983575849214562, 
0.0206557859806864, 0.118736112029919, 0.0598186016823233, 0.10764575714096, 
0.192972175067889, 0.103001671031208), coef.2 = c(-0.109933142836807, 
0.0931589810672566, 0.141263395398117, 0.0639774847656829, -0.00693147180559945, 
0.136896568160589, 0.103625503493712, 0.10452659482844, -0.0407570542169248, 
0.0239828924473741, -0.116656670488239, -0.0196853799279024, 
-0.0294587551737977, 0.0601651752726033, 0.0869899711602731), 
    lower.2 = c(-1.49657407754698, -0.699454819903041, -1.11388751915983, 
    -2.04741814193797, -1.71172696239278, -2.38407972753593, 
    -0.14639268453426, -2.63042423550694, -0.0411729425252607, 
    -0.329106281329862, -1.78748794922799, -1.77008995499593, 
    -2.74001080475346, -0.331809555334046, -0.986417752654858
    ), upper.2 = c(1.27670779187336, 0.885772782037554, 1.39648362467412, 
    2.17537311146933, 1.69779470406353, 2.65794217857517, 0.35371300623974, 
    2.83947742516382, -0.0403411659085888, 0.377141380942666, 
    1.55417460825151, 1.73071919514013, 2.68102397968781, 0.452139905879252, 
    1.1603976949754), coef.3 = c(-0.0300608497949493, -0.0425266557285549, 
    0.00203405476131828, 0.00505274924033682, 0.0025182072288489, 
    -0.0452468135468767, 0.0174013561937757, -0.0254369711604783, 
    0.0215690069807117, -0.0157618937169474, -0.0177464002239964, 
    -0.0696609890428624, -0.00630310505611284, 0.00662584516795135, 
    -0.0785711359046796), lower.3 = c(-0.0937993992956002, -0.106259530283223, 
    -0.0475385390705965, -0.0448349055761413, -0.0621475842518707, 
    -0.109291501101384, -0.0436509774776596, -0.0882790169555314, 
    -0.0345311064869931, -0.0646454393139387, -0.0598080481341427, 
    -0.138356840908691, -0.0660177950470119, -0.0727771769210863, 
    -0.171916302033368), upper.3 = c(0.0336776997057017, 0.0212062188261131, 
    0.051606648593233, 0.0549404040568149, 0.0671839987095685, 
    0.0187978740076303, 0.078453689865211, 0.0374050746345747, 
    0.0776691204484165, 0.033121651880044, 0.0243152476861499, 
    -0.000965137177033508, 0.0534115849347862, 0.086028867256989, 
    0.0147740302240089), coef.4 = c(-0.00879578700124551, 0.00881639637002055, 
    0.056922005179303, 0.015643022580852, 0.0483283177388849, 
    -0.0396125063203703, 0.0418694133053446, -0.0538693389435695, 
    0.0143268588078972, 0.00779716807980812, 0.0250818018315034, 
    -0.104280372253727, -0.00646077284542365, -0.058463123535302, 
    -0.09423856876891), lower.4 = c(-0.0688794667890817, -0.0563580811481513, 
    -0.000595209377422851, -0.0366051866261116, -0.00789527650829705, 
    -0.105910550780405, -0.0257558417991132, -0.120870339822347, 
    -0.0384603803056182, -0.0389581966198825, -0.0216656065565743, 
    -0.179064562638096, -0.0662218282099502, -0.140367535375048, 
    -0.192301303992188), upper.4 = c(0.0512878927865907, 0.0739908738881924, 
    0.114439219736029, 0.0678912317878156, 0.104551911986067, 
    0.0266855381396641, 0.109494668409803, 0.013131661935208, 
    0.0671140979214126, 0.0545525327794987, 0.0718292102195812, 
    -0.0294961818693575, 0.0533002825191029, 0.0234412883044444, 
    0.00382416645436762), coef.5 = c(0.0350007027923865, 0.0136852699482875, 
    0.0453917660093661, 0.0498609998184113, -0.0110021613854642, 
    -0.00890973827794782, 0.0239750345490126, -0.0385017706318439, 
    0.00174782965277247, -0.00881744346677425, 0.00370270449422563, 
    -0.00389514434632695, 0.00582269923380381, -0.0209633175450171, 
    -0.0267807343690945), lower.5 = c(-0.0299957636157803, -0.0531332349443674, 
    -0.0114106321716617, 0.000662925032235563, -0.0713191058580599, 
    -0.0773094617466496, -0.0351688463126914, -0.108379009202782, 
    -0.0493480125235737, -0.0527781651240845, -0.0418160533189797, 
    -0.0736321554747554, -0.0586963289839296, -0.0964890480217128, 
    -0.120223699511677), upper.5 = c(0.0999971692005533, 0.0805037748409423, 
    0.102194164190394, 0.0990590746045871, 0.0493147830871315, 
    0.0594899851907539, 0.0831189154107166, 0.0313754679390942, 
    0.0528436718291187, 0.035143278190536, 0.049221462307431, 
    0.0658418667821015, 0.0703417274515372, 0.0545624129316785, 
    0.0666622307734879), lobe = c("Frontal", "Frontal", "Frontal", 
    "Frontal", "Frontal", "Frontal", "Frontal", "Frontal", "Frontal", 
    "Frontal", "Frontal", "Occipital", "Occipital", "Occipital", 
    "Occipital")), row.names = c(3L, 6L, 13L, 15L, 17L, 19L, 
20L, 21L, 25L, 28L, 29L, 4L, 12L, 14L, 22L), class = "data.frame")

Any help is much appreciated!

1
Without having tested your code, you know that tiff() + dev.off() effect the plot to be stored in your working directory? - Pax
You must either remove forestplot <- or print the object afterwards (e.g. tiff(); plt <- plot(); plt) otherwise it will not appear in the file - danlooo
@Pax can you elaborate please? - rkl
Yes, let me write an answer. One second. - Pax

1 Answers

0
votes

The code you have written regarding the creation of a forestplot works:

library(forestplot)
tt1=cbind(results_1_2_3_4_5_subset2$Outcome)
forestplot <- forestplot(tt1, ...)

If you want to directly plot your forestplot, you need to write one of the following commands, since you have stored the plot in forestplot (see environment) by writing forestplot <- ...:

forestplot 
# or 
plot(forestplot) 

Assuming your goal is to store the created plot as file in your working directory (wd), tiff('subset2.tiff', ...) + dev.off()is totally fine for that. You need to use the following structure:

## as before ##
library(forestplot)
tt1=cbind(results_1_2_3_4_5_subset2$Outcome)
forestplot <- forestplot(tt1, ...)
## ## 
# then:
tiff('subset2.tiff',units="cm",width=16,height=16,res=300)
plot(forestplot)
dev.off()

After executing the code, the forestplot is stored as file in your working directory, see ?getwd() and ?setwd() for help.

Alternatively, you can use what @danlooo mentions in the comments and delete forestplot <- in your orignal attempt. Then, your plot will directly be stored to your wd, as tiff(...) and dev.off() surrounding the code you use to create the plot. Of course, you can subsequently plot your creation by writing plot(forestplot).

In addition, I suggest to put tt1=cbind(results_1_2_3_4_5_subset2$Outcome) before tiff(...).