ggarrange for循环和pdf中的多页

时间:2018-11-08 11:47:17

标签: r for-loop ggplot2 ggpubr

这是我的问题: 我正在遍历数据。 对于循环的每次迭代,我创建3个在同一页面上组织的图,并且希望每次迭代将一页存储在同一pdf文件中。

我正在使用ggpubr软件包中的ggarrange(我希望图3显示在第二行并跨越2列)。

这是我的处理方式(根据一些测试数据):

我创建测试数据帧:

tt <- data.frame(group=rep(c("A", "B"), 3), value=1:6)
tt2 <- data.frame(x=1:10, y=1:10)

我将图创建并存储在对象中

p1 <- ggplot(tt, aes(x=group, y=value, fill=group)) +
    geom_dotplot(binaxis="y")
p2 <- ggplot(tt, aes(x=group, y=value, fill=group)) + geom_boxplot()
p3 <- ggplot(tt2, aes(x=x, y=y)) + geom_point()

这有效:

pdf("test.pdf")
ggarrange(ggarrange(p1, p2),
        p3,
        nrow=2, 
        heights=c(2, 1)) 
dev.off()

这不起作用(它不会引发错误,但pdf文件不包含任何页面):

pdf("test.pdf")
for(i in 1:2){
ggarrange(ggarrange(p1, p2),
        p3,
        nrow=2, 
        heights=c(2, 1)) 
}
dev.off()

我认为嵌套ggarrange可能是个问题,但这也行不通:

pdf("test.pdf")
for(i in 1:2){
ggarrange(p1, p2) 
}
dev.off()

我还意识到在for循环中打开和关闭pdf文件也会失败:

for(i in 1:2){
pdf(paste0("test",i,".pdf"))
ggarrange(p1, p2) 
dev.off()
}

编辑!我发现从gridExtra包中使用grid.arrange的替代方法有效:

pdf("test.pdf")
for(i in 1:2){
grid.arrange(
  p1, p2, p3,
  nrow=2, ncol=2,
  layout_matrix = rbind(c(1, 2),
                        c(3, 3))
)
}
dev.off()

但是我仍然想知道为什么它在ggarrange代码中失败,所以有人知道如何处理吗?

谢谢!

R version 3.5.0 (2018-04-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Scientific Linux 7.2 (Nitrogen)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] ggpubr_0.1.8         magrittr_1.5         BiocInstaller_1.30.0
 [4] gtable_0.2.0         gplots_3.0.1         RColorBrewer_1.1-2  
 [7] scales_0.5.0         gridExtra_2.3        cowplot_0.9.2       
[10] reshape2_1.4.3       ggplot2_2.2.1       

loaded via a namespace (and not attached):
 [1] Category_2.46.0             bitops_1.0-6               
 [3] matrixStats_0.53.1          bit64_0.9-7                
 [5] progress_1.1.2              httr_1.3.1                 
 [7] GenomeInfoDb_1.16.0         Rgraphviz_2.24.0           
 [9] tools_3.5.0                 backports_1.1.2            
[11] R6_2.2.2                    KernSmooth_2.23-15         
[13] DBI_1.0.0                   lazyeval_0.2.1             
[15] BiocGenerics_0.26.0         colorspace_1.3-2           
[17] prettyunits_1.0.2           compiler_3.5.0             
[19] bit_1.1-12                  sendmailR_1.2-1            
[21] graph_1.58.0                Biobase_2.40.0             
[23] DelayedArray_0.6.1          labeling_0.3               
[25] rtracklayer_1.40.3          caTools_1.17.1             
[27] checkmate_1.8.5             BatchJobs_1.7              
[29] genefilter_1.62.0           RBGL_1.56.0                
[31] stringr_1.3.0               digest_0.6.15              
[33] Rsamtools_1.32.0            AnnotationForge_1.22.0     
[35] XVector_0.20.0              base64enc_0.1-3            
[37] pkgconfig_2.0.1             limma_3.36.2               
[39] rlang_0.2.0                 RSQLite_2.0                
[41] BBmisc_1.11                 bindr_0.1.1                
[43] GOstats_2.46.0              hwriter_1.3.2              
[45] BiocParallel_1.14.1         gtools_3.5.0               
[47] dplyr_0.7.4                 RCurl_1.95-4.10            
[49] GO.db_3.6.0                 GenomeInfoDbData_1.1.0     
[51] Matrix_1.2-12               Rcpp_0.12.16               
[53] munsell_0.4.3               S4Vectors_0.18.3           
[55] stringi_1.1.7               edgeR_3.22.3               
[57] SummarizedExperiment_1.10.1 zlibbioc_1.26.0            
[59] plyr_1.8.4                  blob_1.1.0                 
[61] ggrepel_0.8.0               parallel_3.5.0             
[63] gdata_2.18.0                lattice_0.20-35            
[65] Biostrings_2.48.0           splines_3.5.0              
[67] GenomicFeatures_1.32.0      annotate_1.58.0            
[69] locfit_1.5-9.1              pillar_1.2.1               
[71] tcltk_3.5.0                 GenomicRanges_1.32.3       
[73] rjson_0.2.15                systemPipeR_1.14.0         
[75] DiffBind_2.8.0              biomaRt_2.36.1             
[77] stats4_3.5.0                glue_1.2.0                 
[79] XML_3.98-1.10               ShortRead_1.38.0           
[81] latticeExtra_0.6-28         data.table_1.10.4-3        
[83] purrr_0.2.4                 amap_0.8-16                
[85] assertthat_0.2.0            xtable_1.8-2               
[87] survival_2.41-3             tibble_1.4.2               
[89] pheatmap_1.0.8              GenomicAlignments_1.16.0   
[91] AnnotationDbi_1.42.1        memoise_1.1.0              
[93] IRanges_2.14.10             bindrcpp_0.2               
[95] brew_1.0-6                  GSEABase_1.42.0            

1 个答案:

答案 0 :(得分:0)

您需要像上面的aosmith一样在ggarrange周围包裹一个print()。

for(i in 1:2){
pdf(paste0("test",i,".pdf"))
print(ggarrange(p1, p2))
dev.off()
}