Forestplot 输出一个空白的森林图

Forestplot outputting a blank forestplot

我在很多工作中成功使用了以下代码,但它不再工作并输出空白文件(而不是森林图)。不确定 forestplot 函数中是否存在错误,或者它是我的代码中的一个小故障。

我添加了下面使用的数据框和代码。

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")

非常感谢任何帮助!

您编写的关于创建森林图的代码有效:

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

如果您想直接绘制您的森林图,您需要编写以下命令之一,因为您已通过编写 forestplot <- ... 将绘图存储在 forestplot(查看环境)中:

forestplot 
# or 
plot(forestplot) 

假设您的目标是将创建的绘图作为文件存储在您的工作目录 (wd) 中,tiff('subset2.tiff', ...) + dev.off() 完全可以。您需要使用以下结构:

## 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()

执行代码后,森林图作为文件存储在您的工作目录中,请参阅 ?getwd()?setwd() 以获得帮助。

或者,您可以使用@danlooo 在评论中提到的内容并在您的原始尝试中删除 forestplot <-。然后,您的绘图将直接存储到您的 wd 中,因为 tiff(...)dev.off() 围绕您用于创建绘图的代码。当然,您随后可以通过编写 plot(forestplot).

来绘制您的创作

另外,我建议在tiff(...)之前加上tt1=cbind(results_1_2_3_4_5_subset2$Outcome)