YuLab-SMU / enrichplot

Visualization of Functional Enrichment Result
https://yulab-smu.top/biomedical-knowledge-mining-book/
234 stars 65 forks source link

using `ggsave` after `gseaplots2` inside a function doesn't create the correct plot. #296

Closed yeroslaviz closed 3 weeks ago

yeroslaviz commented 2 months ago

Thanks you for really great packages. I'm using the enrichplot::gseaplot2 function to plot my gsea results. I'm doing it inside a function. After the plot I use ggsave to save the plot to my computer.

For some reason the plot is wrong. I only get the middle part, but not the top part. The image I get is attached below.

AD-vs-CTRL_multiple.pdf

I think it has something to do with the fact that the object created with gseaplot2 is a gglist object. Do you have any suggestion as to how to save the plot to the computer?

thanks

Assa

the code I use is something like that

enrichmentCalculation <- function(resFile, orgDB = org.Hs.eg.db, organism = 'hsa' ) {
#  resFile = "results/DEresults/E-vs-A.txt"
  name <- unlist(stringr::str_split(string = resFile, "/|\\."))[3]
  res.ord = suppressMessages(read_delim(file = resFile, delim = "\t"))
...
  enrichplot::gseaplot2(x, geneSetID = c(1:i), pvalue_table = TRUE,color = c("#E495A5", "#86B875", "#7DB0DD"))
  gggsave(filename = paste0("results/EnrichmentAnalysis/", name,"_multiple.gseaCurves_ReactomePA.pdf"), 
             width = 18, height = 17 )
...
}

x is the results from the call to ReactomePA::gsePathway function.

guidohooiveld commented 1 month ago

Does ggsave work for you when you run it manually? I am asking because for me it does...

Also, in the past I have read somewhere (I forgot which site) that in a similar case running dev.off() after generating a plot, and before calling ggsave(), solved some incompatibility issues.

> library(clusterProfiler)
> library(enrichplot)
> library(DOSE)
> library(ggplot2)
> 
> data(geneList, package="DOSE")
> 
> edo <- gseDO(geneList)
using 'fgsea' for GSEA analysis, please cite Korotkevich et al (2019).

preparing geneSet collections...
GSEA analysis...
leading edge analysis...
done...
Warning message:
In fgseaMultilevel(pathways = pathways, stats = stats, minSize = minSize,  :
  For some pathways, in reality P-values are less than 1e-10. You can set the `eps` argument to zero for better estimation.
> 
> 
> i=4
> enrichplot::gseaplot2(edo, geneSetID = c(1:i), pvalue_table = TRUE,
+             color = c("#E495A5", "#86B875", "#7DB0DD") )
> 
> ggsave(filename = "GSEAplot2.pdf", 
+              width = 18, height = 17 )
> 
> 

GSEAplot2.pdf.

For completeness: graphical output R console: image

yeroslaviz commented 1 month ago

When I do it manually it works, but not inside the function.

I manage to circumvent the problem using wrap_plot()

 p1 <- enrichplot::gseaplot2(x, geneSetID = c(1:i), pvalue_table = TRUE,color = c("#E495A5", "#86B875", "#7DB0DD"))
  combined_plot <- wrap_plots(p1,ncol = 1 )
  ggsave(plot = combined_plot, filename = paste0(outputFolder, "/EnrichmentAnalysis/", name,"_multiple.gseaCurves_ReactomePA.pdf"), 
          width = 21, height = 18 )

But this is not a real solution for the problem.

GuangchuangYu commented 3 weeks ago
> enrichplot::gseaplot2(edo, geneSetID = c(1:i), pvalue_table = TRUE,
+             color = c("#E495A5", "#86B875", "#7DB0DD") )
> 
> ggsave(filename = "GSEAplot2.pdf", 
+              width = 18, height = 17 )

This one works because you really plot the figure and then the plot can be accessed via last_plot() which is the default of ggsave.

> args(ggsave)
function (filename, plot = last_plot(), device = NULL, path = NULL,
    scale = 1, width = NA, height = NA, units = c("in", "cm",
        "mm", "px"), dpi = 300, limitsize = TRUE, bg = NULL,
    create.dir = FALSE, ...)

 p1 <- enrichplot::gseaplot2(x, geneSetID = c(1:i), pvalue_table = TRUE,color = c("#E495A5", "#86B875", "#7DB0DD"))
  combined_plot <- wrap_plots(p1,ncol = 1 )
  ggsave(plot = combined_plot, filename = paste0(outputFolder, "/EnrichmentAnalysis/", name,"_multiple.gseaCurves_ReactomePA.pdf"), 
          width = 21, height = 18 )

This one works is because you pass the graphic object to plot parameter. Which is of course missing in your function.

enrichmentCalculation <- function(resFile, orgDB = org.Hs.eg.db, organism = 'hsa' ) {
#  resFile = "results/DEresults/E-vs-A.txt"
  name <- unlist(stringr::str_split(string = resFile, "/|\\."))[3]
  res.ord = suppressMessages(read_delim(file = resFile, delim = "\t"))
...
  enrichplot::gseaplot2(x, geneSetID = c(1:i), pvalue_table = TRUE,color = c("#E495A5", "#86B875", "#7DB0DD"))
  gggsave(filename = paste0("results/EnrichmentAnalysis/", name,"_multiple.gseaCurves_ReactomePA.pdf"), 
             width = 18, height = 17 )
...
}

The reason is not because wrap_plots() but the plot parameter. Without the wrap_plots() and explicitly passing p1 to plot parameter should also works.

 p1 <- enrichplot::gseaplot2(x, geneSetID = c(1:i), pvalue_table = TRUE,color = c("#E495A5", "#86B875", "#7DB0DD"))
  ggsave(plot = p1, filename = paste0(outputFolder, "/EnrichmentAnalysis/", name,"_multiple.gseaCurves_ReactomePA.pdf"), 
          width = 21, height = 18 )