YuLab-SMU / enrichplot

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

How can we customize the color in the dotplot? I want to use the color from the darjeeling1 palette. #294

Open rachanapandey250 opened 1 week ago

guidohooiveld commented 6 days ago

Please provide some more info on your use case... Firstly, what is the darjeeling1 palette? Is the darjeeling1 palette included in R? If so, how to access it?

guidohooiveld commented 5 days ago

Here you go...

AFAIK there are 2 options to do this: 1) Modify the dotplot, which is is a ggplot2 object, after it is generated and therefore is relatively easy to fine-tune. Yet, this should be done for each plot.

2) Change the color scale for the whole R-session using options(), so all plots made through enrichplot will apply this color palette.

Google suggested me that the darjeeling1 palette is part of the CRAN package wesanderson (A Wes Anderson Palette Generator). https://cran.r-project.org/web/packages/wesanderson/index.html

The definition of the darjeeling1 colors can be found in the source code of wesanderson:

Darjeeling1 = c("#FF0000", "#00A08A", "#F2AD00", "#F98400", "#5BBCD6") https://github.com/karthik/wesanderson/blob/02e4129c185e9d3c49b05b580717f35be2eef5c3/R/colors.R#L15

> ## install 'wasanderson package' from GitHub
> BiocManager::install(c('karthik/wesanderson'), force=TRUE)
> ## load libraries and generate dotplot
> library(clusterProfiler)
> library(enrichplot)
> library(ggplot2)
> 
> library(wesanderson)
> 
> ## Load example data
> data(geneList, package="DOSE")
> 
> res <- gseKEGG(geneList)
Reading KEGG annotation online: "https://rest.kegg.jp/link/hsa/pathway"...
Reading KEGG annotation online: "https://rest.kegg.jp/list/pathway/hsa"...
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.
> 
> p1 <- dotplot(res)
> print(p1)
> 

image

> ## OPTION 1: ad hoc modification of ggplot2 object
> ##
> ## define and apply Darjeeling1 palette
> pal <- wes_palette(name="Darjeeling1", n=5, type="continuous")
> 
> p2 <- p1 + scale_fill_gradientn(colours = pal)
Scale for fill is already present.
Adding another scale for fill, which will replace the existing scale.
> print(p2)
> 

image

> ## OPTION 2: change colors for whole R-session
> ##
> ## use: options()
> 
> Darjeeling1 = c("#FF0000", "#00A08A", "#F2AD00", "#F98400", "#5BBCD6")
> options(enrichplot.colours = Darjeeling1)
> 
> p3 <- dotplot(res)
> print(p3)
> 
> p4 <- heatplot(res, foldChange=geneList, showCategory=5)
> print(p4)

p3: image

p4: image

> ## for comparison, show palette
> pal
>

image