Multiple plots using for loops in R
1
0
Entering edit mode
4.1 years ago

Hi!

I'm working with a GSEA analysis using the fGSEA package. In this case, the output of the GSEA analysis is stored in list that looks like:

> result
$p1prnkpositional.db
# A tibble: 68 x 9
   pathway      pval    padj    ES   NES nMoreExtreme  size leadingEdge condition
   <chr>       <dbl>   <dbl> <dbl> <dbl>        <dbl> <int> <list>      <chr>    
 1 chr16p11 0.000345 0.00707 0.508  2.23            0    96 <chr [47]>  Activated
 2 chr9q34  0.000420 0.00707 0.426  2.05            0   177 <chr [67]>  Activated
 3 chr19p13 0.000747 0.00917 0.384  2.04            0   448 <chr [176]> Activated
 4 chr16p13 0.000437 0.00707 0.405  1.98            0   197 <chr [81]>  Activated
 5 chr11q13 0.000457 0.00707 0.387  1.90            0   215 <chr [80]>  Activated
 6 chr1q22  0.000597 0.00776 0.501  1.90            1    47 <chr [25]>  Activated
 7 chr17p13 0.000433 0.00707 0.381  1.84            0   185 <chr [61]>  Activated
 8 chr17q25 0.000405 0.00707 0.374  1.78            0   159 <chr [82]>  Activated
 9 chr1q21  0.000403 0.00707 0.366  1.73            0   157 <chr [39]>  Activated
10 chr11q12 0.00195  0.0172  0.397  1.68            5    80 <chr [38]>  Activated
# … with 58 more rows

$p2prnkmir.db
# A tibble: 1,933 x 9
   pathway            pval   padj     ES   NES nMoreExtreme  size leadingEdge condition 
   <chr>             <dbl>  <dbl>  <dbl> <dbl>        <dbl> <int> <list>      <chr>     
 1 CTGAGCC_MIR24    0.0287 0.0363 -0.310 -1.34          219   187 <chr [61]>  Suppressed
 2 MIR1207_5P       0.0200 0.0259 -0.300 -1.34          159   252 <chr [90]>  Suppressed
 3 MIR608           0.0325 0.0407 -0.322 -1.35          241   149 <chr [42]>  Suppressed
 4 MIR4270          0.0372 0.0460 -0.333 -1.36          269   125 <chr [64]>  Suppressed
 5 MIR6132          0.0405 0.0496 -0.345 -1.37          286   103 <chr [41]>  Suppressed
 6 MIR6778_3P       0.0202 0.0262 -0.320 -1.38          153   180 <chr [56]>  Suppressed
 7 MIR504_3P        0.0360 0.0446 -0.340 -1.38          259   115 <chr [42]>  Suppressed
 8 CAGCCTC_MIR4855P 0.0333 0.0416 -0.340 -1.39          240   121 <chr [27]>  Suppressed
 9 MIR6799_3P       0.0377 0.0466 -0.355 -1.39          264    92 <chr [38]>  Suppressed
10 MIR7113_5P       0.0298 0.0375 -0.346 -1.39          213   112 <chr [36]>  Suppressed
# … with 1,923 more rows

>

In this case I want to generate a dotplot respect to NES value using a own function. When a run the next code for only one tibble in the list:

> barplot.NES(result[["p1prnkmir.db"]])

The plot is showed. However, when I'm trying run a for loop to save the output in distinct pdf files an error message is generated:

samps <- names(result)

> for (i in samps) {
+   pdf(paste0(i, ".pdf"), width = 1000, height = 800)
+   print(barplot.NES(result[[i]]))
+   dev.off()
+ }
Error: Faceting variables must have at least one value

Does anyone have an idea what I'm doing wrong?

Best regards.

Rodo

RNA-Seq R ggplot loop • 1.5k views
ADD COMMENT
1
Entering edit mode
4.1 years ago

Something crashed inside the loop.

Probably one of the result items is invalid or incomplete. You can print(i) before doing anything else inside the loop to have it tell you which ones pass. You can do another dev.off() after the crash to finish the last entry. You can do a tryCatch() to consume the error and continue. Or you can inspect the result items and write an if() to catch and prevent the broken item from trying to print.

tryCatch() is a little tricky to use, I would go with the if() strategy, which presumes you can figure out what is wrong with the offending result item.

ADD COMMENT
0
Entering edit mode

Thanks for your help and suggestions.

You were right, some of the tibbles inside the list were empty and, thus, caused a crash inside the loop.

ADD REPLY

Login before adding your answer.

Traffic: 1764 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6