Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change the color of nodes and edges in cnetplot and emapplot #66

Open
YyLu5 opened this issue Oct 5, 2020 · 8 comments
Open

change the color of nodes and edges in cnetplot and emapplot #66

YyLu5 opened this issue Oct 5, 2020 · 8 comments

Comments

@YyLu5
Copy link

YyLu5 commented Oct 5, 2020

Hi, is it possible to change the node color in emapplot? Also the cnetplot has default color red and green for the edges, can I change those to different colors, as the color blindness issue. Thank you!

@hreinwal
Copy link

hreinwal commented Jun 15, 2021

Hi Silvernano,

I just checked a snipped of the source code for the cnetplot function and the plot you create is basically a ggplot object. Hence you can modify the plot with any extension available for ggplot2.

Here is an example for how is used scale_color_manual() to turn genes red and nodes blue:

library(DOSE)
library(ggplot2)

data(geneList)
x <- enrichDO(names(geneList)[1:100])

g <- cnetplot(x, colorEdge = F )
g + scale_color_manual(values = c("red","blue"))

I hope this is of help for you :)

@MrModenait
Copy link

MrModenait commented Aug 3, 2021

I wasn't able to figure out how to use that scale_colour_manual(), so instead I dug up the source of the beige colour that is currently being used for the CNET plot nodes. Use this to set the colour of the nodes:
cnetplot[["layers"]][[2]][["mapping"]][["colour_new"]][[2]][[2]] <- "pink"
As for the colour of the edges, I am sure the source can also be found somewhere!

@GuangchuangYu
Copy link
Member

@MrModenait
Copy link

Dear Professor @GuangchuangYu ,
Thank you for your email. I too have tried to edit the colour of the categories in cnetplot using color_category in the past, but my attempts have been unsuccessful. For some reason, when specifying a vector of fold-changes for foldChange for cnetplot, the plot does not change the colour according to color_category. For example, when running with foldChange = fold_change_geneList , the following plot is produced:
CNET plot with fold change list
As you can see, the genes are coloured but the parameter color_category = "#97c497" is ignored. The plot is built using these fold_changes:

>   head(fold_change_geneList)
     Il1b    Wfdc17      Fgl2    Marcks    Ifitm1    Pla2g7 
1.7955272 1.1471514 1.0081012 1.0019730 0.9967532 0.9881016 

And this is the code for the plot. (Some parameters, such as number_of_upregulated_genes or CNET_gene_cutoff, are variables as the code is part of a loop.)

 cnet <- cnetplot(gsea_topgenes,
                   categorySize="pvalue",
                   foldChange = fold_change_geneList,
                   showCategory = 25,
                   circular = F,
                   node_label="all",
                   layout = "kk", color_category = "#97c497")+ 
    labs(title = paste("Significant Pathways in Cluster ",k,sep = ""),
         subtitle = "by Gene Set Enrichment Analysis",
         caption = paste0("Based on ",number_of_upregulated_genes," upregulated genes and ",number_of_downregulated_genes," downregulated genes.",
                          " Showing ",CNET_gene_cutoff," genes per pathway."))+
    theme(plot.title = element_text(hjust = 0.5,size=20),
          plot.subtitle = element_text(hjust = 0.5,size=15))

The following plot is produced when I set foldChange = NULL. What could be the reason for this behaviour?
CNET plot without fold change list
Best Regards,
Gustav C.

@adesalegn
Copy link

@GuangchuangYu and @MrModenait ,

Hi all,
Following the above discussion, how I am supposed to do if I want to color some of the gene nodes in the above plots.
For example, I want to color the following gene node(csf3r, kif2, cdkn1b, and sirpb) blue and the rest black.

Best,
Amare

@MrModenait
Copy link

Hey @adesalegn, I haven't compiled an answer as I don't know how to achieve your result. Perhaps using geom_node_point? It appears around line 80 in the source code for R/cnetplot.R.

@cjhatch
Copy link

cjhatch commented May 2, 2023

Hi, if people are still struggling with changing the node color, I believe I have a workable solution with example data to test it. You need to change the color location in two different places to get the nodes to change. I'm not exactly sure why that is the case, but this at least works for me.

## install library and load example dataset
library(DOSE)
require(gginnards)
library(ggrepel)
library(gridExtra)

data(geneList)
gene <- names(geneList)[abs(geneList) > 2]

## run clusterProfiler
ego <- enrichGO(gene          = gene,
                universe      = names(geneList),
                OrgDb         = org.Hs.eg.db,
                ont           = "CC",
                pAdjustMethod = "BH",
                pvalueCutoff  = 0.01,
                qvalueCutoff  = 0.05,
                readable      = F)

## save original plot for comparison
p0 <- cnetplot(ego, foldChange = geneList) + ggtitle("Original plot")

## update plot to change the node color to a color of your choice
p1 <- cnetplot(ego, foldChange = geneList) + ggtitle("Change node color")
p1[["layers"]][[2]][["mapping"]][["colour_new"]][[2]][[2]] <- "blue"
p1[["data"]][["color"]][which(!(p1[["data"]][["name"]] %in% gene) == T)] <- "blue"

## bonus section to geom_text_repel to prevent overlap of labels. This solution is curtousey of: https://support.bioconductor.org/p/127688/
## find layer classes. For this purpose it is the "GeomTextRepel" layer
lapply(p1$layers, function(x) class(x$geom))

## delete current label layer and replace with geom_text_repel layer
p2 <- gginnards::delete_layers(p1, "GeomTextRepel")
p2 <- p2 + ggrepel::geom_text_repel(aes(x = p1$data$x, y = p1$data$y,
                                   label = p1$data$name)) +
  ggtitle("Change node color and repel labels")


## visualize all plots side by side
gridExtra::grid.arrange(p0, p1, p2, ncol = 3) # visualize all plots side by side

cnetplotColoredNodes

@lumin2003
Copy link

following the link of https://support.bioconductor.org/p/p133748/, successfully changed the color of gene labeling with a code: cnetplot(data, categorySize="padj", foldChange=geneList) + scale_color_gradientn(colours = c("blue", "red"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants