8

Here is my issue: I am looping around my data. For each iteration of the loop, I create 3 plots that I organize on the same page, and I want to have one page per iteration stored in the same pdf file.

I am using ggarrange from the ggpubr package (I want plot 3 to be displayed on the second row and to span 2 columns).

Here is how I proceed (on some test data):

I create test data frames:

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

I create and store the plots in objects:

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

This works:

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

This doesn't work (it does not throw an error but the pdf file does not contain any page):

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

I thought maybe the nested ggarrange could be an issue, but this also doesn't work:

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

I also realized that opening and closing the pdf file inside the for loop fails as well:

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

EDIT! I found that the alternative using grid.arrange from the gridExtra package works:

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

But I'm still interested to know why it fails with the ggarrange code so does anyone know how to deal with this?

Thank you!

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            
sargg
  • 323
  • 3
  • 8
  • 1
    How about putting all plots in a `list` then use `marrangeGrob`? See these: https://stackoverflow.com/a/51772409/786542 & https://stackoverflow.com/a/53049704/786542 – Tung Nov 08 '18 at 14:45
  • 1
    I think this is the old "explicit printing needed" problem. You need to wrap your plotting code within the loop in `print()`. See, e.g., [here](https://stackoverflow.com/questions/4811106/cant-print-to-pdf-ggplot-charts) and [here](https://stackoverflow.com/questions/15678261/r-ggplot-does-not-work-if-it-is-inside-a-for-loop-although-it-works-outside-of) – aosmith Nov 08 '18 at 14:55

1 Answers1

2

You need to wrap a print() around ggarrange like aosmith commented above.

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