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

Issue with KEGG Enrichment Visualization in cnetplot #302

Open
GakiChow opened this issue Dec 2, 2024 · 7 comments
Open

Issue with KEGG Enrichment Visualization in cnetplot #302

GakiChow opened this issue Dec 2, 2024 · 7 comments

Comments

@GakiChow
Copy link

GakiChow commented Dec 2, 2024

Dear Enrichplot Authors and Yu's Group,

I am reaching out for assistance with an issue I’ve encountered when visualizing the results of KEGG enrichment using layout = 'kk' in cnetplot. I consistently receive the following error:

"Error in storage.mode(mat) <- ‘integer’: can't force ‘list’ object to be typed as ‘integer’."

However, if I remove the layout argument, the plot works fine and the result is displayed correctly.

This problem has been causing difficulties for nearly a month now, and I would greatly appreciate any guidance or assistance you could provide to resolve it at your convenience.

Thank you very much for your time and help.

Best regards,
Gaki
屏幕截图 2024-12-02 233739

@guidohooiveld
Copy link

guidohooiveld commented Dec 3, 2024

Which version of clusterProfiler and enrichplot are you using?

I am asking because since v1.26.x the function cnetplot has been completely rewritten. As a consequence, the arguments that are used have also been changed a bit. See ?cnetplot for more info.

> library(clusterProfiler)
> library(enrichplot)
> library(igraph)
> 
> data(geneList, package="DOSE")
> gene <- names(geneList)[abs(geneList) > 2]
> 
> res <- enrichKEGG(gene         = gene,
+                  organism     = 'hsa',
+                  pvalueCutoff = 0.05)
> head(res)
                                     category
hsa04110                   Cellular Processes
hsa04114                   Cellular Processes
hsa04218                   Cellular Processes
hsa04061 Environmental Information Processing
hsa03320                   Organismal Systems
hsa04814                   Cellular Processes
                                 subcategory       ID
hsa04110               Cell growth and death hsa04110
hsa04114               Cell growth and death hsa04114
hsa04218               Cell growth and death hsa04218
hsa04061 Signaling molecules and interaction hsa04061
hsa03320                    Endocrine system hsa03320
hsa04814                       Cell motility hsa04814
                                                           Description
hsa04110                                                    Cell cycle
hsa04114                                                Oocyte meiosis
hsa04218                                           Cellular senescence
hsa04061 Viral protein interaction with cytokine and cytokine receptor
hsa03320                                        PPAR signaling pathway
hsa04814                                                Motor proteins
         GeneRatio  BgRatio RichFactor FoldEnrichment   zScore       pvalue
hsa04110    15/106 158/8875 0.09493671       7.948710 9.689176 4.706402e-10
hsa04114    10/106 139/8875 0.07194245       6.023483 6.562868 5.690347e-06
hsa04218    10/106 157/8875 0.06369427       5.332893 6.022224 1.672593e-05
hsa04061     8/106 100/8875 0.08000000       6.698113 6.300066 2.380917e-05
hsa03320     7/106  76/8875 0.09210526       7.711643 6.460359 3.157263e-05
hsa04814    10/106 197/8875 0.05076142       4.250072 5.071704 1.156684e-04
             p.adjust       qvalue
hsa04110 1.011877e-07 9.908216e-08
hsa04114 6.117123e-04 5.989839e-04
hsa04218 1.198691e-03 1.173749e-03
hsa04061 1.279743e-03 1.253114e-03
hsa03320 1.357623e-03 1.329374e-03
hsa04814 4.144784e-03 4.058540e-03
                                                                           geneID
hsa04110 8318/991/9133/10403/890/983/4085/81620/7272/9212/1111/9319/891/4174/9232
hsa04114                          991/9133/983/4085/51806/6790/891/9232/3708/5241
hsa04218                           2305/4605/9133/890/983/51806/1111/891/776/3708
hsa04061                                 3627/10563/6373/4283/6362/6355/9547/1524
hsa03320                                       4312/9415/9370/5105/2167/3158/5346
hsa04814                   9493/1062/81930/3832/3833/146909/10112/24137/4629/7802
         Count
hsa04110    15
hsa04114    10
hsa04218    10
hsa04061     8
hsa03320     7
hsa04814    10
> 
> 
> cnetplot(res, layout = layout_with_kk)
> 

image

> cnetplot(res, layout = layout_in_circle)
>

image

More info on igraph layouts: https://igraph.org/r/html/latest/layout_.html

For inspiration some examples:

384031246-f7b89eed-0099-4253-8154-1913d857b8a5

384031393-6e399612-11fe-4c33-bd3b-e2b7074b2c35

384032508-00593467-62cc-4244-a094-3f4298a5df0d

The first 2 example pictures are taken from: https://biosakshat.github.io/network-analysis.html#network-layouts
Last example picture taken from: https://dshizuka.github.io/networkanalysis/03_plots.html

> packageVersion("clusterProfiler")
[1] ‘4.14.3’
> packageVersion("enrichplot")
[1] ‘1.26.2’
> packageVersion("ggtangle")
[1] ‘0.0.5’
> R.Version()$version.string
[1] "R version 4.4.2 (2024-10-31 ucrt)"
> BiocManager::version()
[1] ‘3.20’
>
> sessionInfo()
R version 4.4.2 (2024-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19042)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8 
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: Europe/Amsterdam
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] igraph_2.1.1           enrichplot_1.26.2      clusterProfiler_4.14.3

loaded via a namespace (and not attached):
 [1] tidyselect_1.2.1        dplyr_1.1.4             farver_2.1.2           
 [4] blob_1.2.4              R.utils_2.12.3          Biostrings_2.74.0      
 [7] lazyeval_0.2.2          fastmap_1.2.0           digest_0.6.37          
[10] lifecycle_1.0.4         KEGGREST_1.46.0         tidytree_0.4.6         
[13] RSQLite_2.3.8           magrittr_2.0.3          compiler_4.4.2         
[16] rlang_1.1.4             tools_4.4.2             utf8_1.2.4             
[19] data.table_1.16.2       ggtangle_0.0.5          labeling_0.4.3         
[22] bit_4.5.0               gson_0.1.0              plyr_1.8.9             
[25] RColorBrewer_1.1-3      aplot_0.2.3             BiocParallel_1.40.0    
[28] withr_3.0.2             purrr_1.0.2             BiocGenerics_0.52.0    
[31] R.oo_1.27.0             grid_4.4.2              stats4_4.4.2           
[34] fansi_1.0.6             GOSemSim_2.32.0         colorspace_2.1-1       
[37] GO.db_3.20.0            ggplot2_3.5.1           scales_1.3.0           
[40] cli_3.6.3               crayon_1.5.3            treeio_1.30.0          
[43] generics_0.1.3          ggtree_3.14.0           httr_1.4.7             
[46] reshape2_1.4.4          ape_5.8                 DBI_1.2.3              
[49] qvalue_2.38.0           cachem_1.1.0            DOSE_4.0.0             
[52] stringr_1.5.1           zlibbioc_1.52.0         splines_4.4.2          
[55] parallel_4.4.2          ggplotify_0.1.2         AnnotationDbi_1.68.0   
[58] BiocManager_1.30.25     XVector_0.46.0          yulab.utils_0.1.8      
[61] vctrs_0.6.5             Matrix_1.7-1            jsonlite_1.8.9         
[64] gridGraphics_0.5-1      IRanges_2.40.0          patchwork_1.3.0        
[67] S4Vectors_0.44.0        bit64_4.5.2             ggrepel_0.9.6          
[70] tidyr_1.3.1             glue_1.8.0              codetools_0.2-20       
[73] cowplot_1.1.3           stringi_1.8.4           gtable_0.3.6           
[76] GenomeInfoDb_1.42.1     UCSC.utils_1.2.0        munsell_0.5.1          
[79] tibble_3.2.1            pillar_1.9.0            fgsea_1.32.0           
[82] GenomeInfoDbData_1.2.13 R6_2.5.1                lattice_0.22-6         
[85] Biobase_2.66.0          R.methodsS3_1.8.2       png_0.1-8              
[88] memoise_2.0.1           ggfun_0.1.8             Rcpp_1.0.13-1          
[91] fastmatch_1.1-4         nlme_3.1-166            fs_1.6.5               
[94] pkgconfig_2.0.3        
> 

@czz404
Copy link

czz404 commented Dec 4, 2024

it seems like should be
layout = igraph::layout_in_circle, # 设置为圆形布局

@czz404
Copy link

czz404 commented Dec 4, 2024

@guidohooiveld
Hello, sorry to bother you
Can I ask how to adjust the font size?
I tested the cex_label_category and cex_label_gene parameters, but they don't seem to work.

clusterProfiler 4.14.3
enrichplot 1.26.2

@guidohooiveld
Copy link

guidohooiveld commented Dec 4, 2024

I agree that this is currently not very well documented... Yet, Guangchuang provided a pointer on how to do this in this post: #194 (comment)

Key is to recall that a cnetplot is a ggplot2-object, that can easily be further modified.

To achieve your goal first a default cnetplot should be generated. Yet, whether or not the names of the categories (gene sets) and/or genes (items) should be plotted in this (default) plot depends on your goal. The one that is fine should be plotted by/like default, the one that you would like to modify should then not be plotted. Next you use the function geom_cnet_label (defined in the ggtangle package) to modify the font.

By checking the help page of geom_cnet_label (type ?geom_cnet_label) you will see how to use it.
The last argument, the 3 dots, is important (... : parameters that passed to geom_text_repel), since A) it shows the function geom_text_repel is used under the hood, and B) that additional arguments can be provided that will be used by geom_text_repel. See here to see which additional arguments can be provided: https://ggrepel.slowkow.com/articles/examples.html

Below some code + 8 plots to illustrate all of the above.

> library(clusterProfiler)
> library(enrichplot)
> library(org.Hs.eg.db)
> library(igraph)
> 
> data(geneList, package="DOSE")
> gene <- names(geneList)[abs(geneList) > 2]
> 
> res <- enrichKEGG(gene         = gene,
+                   organism     = 'hsa',
+                   pvalueCutoff = 0.05)
Reading KEGG annotation online: "https://rest.kegg.jp/link/hsa/pathway"...
Reading KEGG annotation online: "https://rest.kegg.jp/list/pathway/hsa"...
> 
> ## to increase readability, convert entrezid into symbols
> ## also calculate pairwise similarities (is in essence only applicable
> ## to GO terms but just to be sure
> res <- setReadable(res, "org.Hs.eg.db", "ENTREZID")
> res <- pairwise_termsim(res)
>
> #### default cnetplot
> p.default <- cnetplot(res)
> 
> print(p.default)
> 

image

> ## note that this default plot can also be obtained 
> ## by using 2 lines of code; thus using function 'geom_cnet_label'
> 
> ## load required library
> library(ggtangle)
ggtangle v0.0.5 Learn more at https://yulab-smu.top/

> 
> p2 <- cnetplot(res, node_label="none")
> ## note no labels are plotted
> print(p2)

image

> ## step 2; add all labels. Note that p3 is identical to p.default
>
> p3 <- p2 + geom_cnet_label(node_label = "all")
> print(p3)
>

image

> 
> #### Goal 1: increase font size of both categories and genes
> 
> ## first generate default cnetplot **without** any text labels (=p2, see above),
> ## then add and modify labels using geom_cnet_label().
> ## for example, to increase size to 7, and bold.
>
> p4 <- p2 + geom_cnet_label(node_label = "all",  size = 7, fontface = "bold")
> print(p4)
>

image

> #### Goal 2: increase font size of genes only, color these red, and italics
> 
> ## first create default cnetplot with categories plotted
> ## then modify plot to show genes (items)
>
> p5 <- cnetplot(res,  node_label="category")
> print(p5)

image

>
> p6 <- p5 + geom_cnet_label(node_label = "item",  size = 7, color = "red", fontface = "italic")
> print(p6)
> 

image

> #### Goal 3: increase font size of gene sets only, color these blue, italics, and bold,
> ####         also increase size of gene set node (using argument 'size_category'), and color
> ####         genes by fold change (argument 'foldChange')
> 
> ## first create default cnetplot with genes plotted,
> ## then modify plot to show genes (items).
>
> p7 <- cnetplot(res, size_category = 2, node_label="item", foldChange = geneList)
> print(p7)

image

> p8 <- p7 + geom_cnet_label(node_label = "category",  size = 5, color = "blue", fontface = "bold.italic")
> print(p8)
> 

image

@czz404
Copy link

czz404 commented Dec 4, 2024

@guidohooiveld
Thank you very much for your detailed answer, which allowed me to refer to the code and solve the problem independently.😊

@gaospecial
Copy link
Contributor

@gaospecial Thank you very much for your detailed answer, which allowed me to refer to the code and solve the problem independently.😊

should refer to @guidohooiveld

@GakiChow
Copy link
Author

GakiChow commented Dec 5, 2024

@guidohooiveld
Thank you very much for your patient and detailed response, which helped me resolve my issue. I wish you all the best!

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

4 participants