YuLab-SMU / enrichplot

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

Treeplot + group label reordering #259

Closed BaylorSci closed 1 year ago

BaylorSci commented 1 year ago

I'm really enjoying the use of your package and wonder if you can shed some light. Following comparecluster of GO terms in an experiment with 5 treatments (ordered factors)(eg., 12, 146, 682, 1450, 1960), I am attempting to apply a treeplot to simplify the output, with the treatments in numeric order.

Unfortunately, the treatments keep on getting reorganized in the plot to 12,1450,146,1960 and 682. The use of scale_x_discrete(limits = c("12","146","682","1450","1960")) does not do anything to the figure, nor does modifying the levels to be read as characters or integers. Checking the compareClusterResult, the output is ordered integer, so not sure why it is being reordered in the plot. Similiarily, when the data is set as a factor, the output of the comparecluster is an ordered factor, but the same behaviour continues.

The script I am using is

treeplot(data.out.bp,showCategory=8, font.size=5,
                      cluster.params = list(color=c("red", "#006600", "#300000", "#ff9900", "#1d3e59")),
                      clusterPanel.params = list(clusterPanel = "dotplot",
                                                 method="ward.D",
                                                 hexpand=0.1,
                                                 label_words_n=4,
                                                 label_format=30))+
  scale_colour_gradient(low="grey", high="black", 
                        labels = function(x) format(x, scientific = TRUE, digit=2))

I have also attempted to use the scale_x_ggtree, presuming this was a ggtree issue, but this also did not result in any changes.

Any help with this would be very appreciated.

guidohooiveld commented 1 year ago

You did not show your full code, but if you create the input list for the function compareCluster using the order you would like to, these order is retained in the resulting treeplot.

Admittedly, the resulting plot is not nice; ideally the labels should be rotated 90 degrees, and also the dots are positioned outside the colored clusterPanel...

@huerqiang : could you please have a look at these 2 issues (option to rotate labels, and correct position dots)? Thanks!

> ## load libraries
> library(clusterProfiler)
> library(enrichplot)
> library(org.Hs.eg.db)
> library(ggplot2)
> 
> ## load sample data
> data(gcSample)
> 
> ## keep first 5 samples
> gcSample1 <- gcSample[c(1:5)]
> 
> ## rename first 5 samples
> ## use increasing order
> names(gcSample1)[1:5] <- c("12", "146", "682", "1450", "1960")
> 
> ## run enrichGO
> data.out.bp1 <- compareCluster(gcSample1, fun="enrichGO",
+                      OrgDb="org.Hs.eg.db", ont="BP", pvalueCutoff=0.05) 
> 
> data.out.bp1 <- pairwise_termsim(data.out.bp1)
> 
> 
> ## make treeplot
> ## note: changed showCategory to 3
> 
> 
> p1 <- treeplot(data.out.bp1,showCategory=3,
+                       clusterPanel.params = list(clusterPanel = "dotplot") )
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
ℹ invalid tbl_tree object. Missing column: parent,node.
ℹ invalid tbl_tree object. Missing column: parent,node.
> 
> ## show picture
> p1
> 

image

> ## rename samples, but now use random order
> gcSample2 <- gcSample[c(1:5)]
> names(gcSample2)[1:5] <- c("146", "12", "682", "1960", "1450")
> 
> ## run enrichGO again
> data.out.bp2 <- compareCluster(gcSample2, fun="enrichGO",
+                      OrgDb="org.Hs.eg.db", ont="BP", pvalueCutoff=0.05) 
> 
> data.out.bp2 <- pairwise_termsim(data.out.bp2)
> 
> p2 <- treeplot(data.out.bp2,showCategory=3,
+                       clusterPanel.params = list(clusterPanel = "dotplot") )
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
! # Invaild edge matrix for <phylo>. A <tbl_df> is returned.
ℹ invalid tbl_tree object. Missing column: parent,node.
ℹ invalid tbl_tree object. Missing column: parent,node.
> 
> ## show picture
> p2
> 
> 

image

huerqiang commented 1 year ago

@guidohooiveld It will be fixed in a few days.

huerqiang commented 1 year ago

@guidohooiveld Will be fixed in #260 test_treeplot.pdf

guidohooiveld commented 1 year ago

@huerqiang : thanks for implementing a fix for this so quickly! Much appreciated.