Multiple plots outputs using for loop in R
1
2
Entering edit mode
5.4 years ago
Barry Digby ★ 1.3k

Hi,

I have been using the package fgsea, and I would like to use a for loop in R to output multiple Enrichment plots.

So far I can generate a plot of the desired pathway using:

plotEnrichment(pathway = 
pathways.hallmark[["HALLMARK_EPITHELIAL_MESENCHYMAL_TRANSITION"]], 
gseaParam = 1, ticksSize = 0.5, stats= ranks) 
+ labs(title="Epithelial to Mesenchymal Transition") 
+ theme(plot.title = element_text(hjust = 0.5, face="bold"))

Screen-Shot-2019-07-20-at-16-19-18

I decided to capture the statistically relevant pathways, and attempted to use a for loop to output them all at once:

filtered_pathway <- subset(fgseaResTidy, pval < 0.05)

filt_p <- as.vector(filtered_pathway$pathway)

filt_p
 [1] "HALLMARK_APICAL_JUNCTION"                   "HALLMARK_MYOGENESIS"                       
 [3] "HALLMARK_HEDGEHOG_SIGNALING"                "HALLMARK_PI3K_AKT_MTOR_SIGNALING"
...

for (i in filt_p){
    plotEnrichment(pathway = pathways.hallmark[[i]], 
    gseaParam = 1, ticksSize = 0.5, stats= ranks) + 
    labs(title=i) + theme(plot.title = element_text(hjust = 0.5, face="bold"))
}

This code does not generate anything. I am aware this is not a bioinformatics question, however I have no doubt many would be able to point out the problem in 2 seconds. Hopefully this post will be helpful to people using fgsea in the future.

Kind regards,

Barry

fgsea loop R • 16k views
ADD COMMENT
0
Entering edit mode

I'd think that this code is writing to the same device in such quick succession that you don't get to see the output, or maybe the last time the loop is executed, a blank plot is generated. Are you using RStudio?

ADD REPLY
0
Entering edit mode

Hi Ram,

Yes I am using R studio. To test that theory I placed

 outfile="/Users/b/Desktop/Hallmarks_plots.pdf"
 pdf(file=outfile) 
 ..FOR LOOP..
 dev.off()

To try and capture all plots in 1 pdf. The resulting pdf cannot be opened (is damaged).

ADD REPLY
9
Entering edit mode
5.4 years ago
Prakash ★ 2.2k

The loop you are running will generate plot but will not save anything . I have modified the code, I hope it helps.

for (i in filt_p){
    pdf(paste0(i,".pdf"),height=5,width=5) # change height and width parameter
    plt <- plotEnrichment(pathway = pathways.hallmark[[i]], 
    gseaParam = 1, ticksSize = 0.5, stats= ranks) + 
    labs(title=i) + theme(plot.title = element_text(hjust = 0.5, face="bold"))
    print(plt)
    dev.off()
}
ADD COMMENT
0
Entering edit mode

Many thanks Prakash!!

ADD REPLY
0
Entering edit mode

I have a similar problem when I'm trying to generate multiple plots using a for loop. In this case, my GSEA results are stored as tibbles inside a 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

However, when run the next code to generate dotplots respect to NES value:

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

The error message is displayed and only three plots are saved in my wd. Do you have an idea what's going on wrong in the code?

ADD REPLY

Login before adding your answer.

Traffic: 2152 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