plotly / plotly.R

An interactive graphing library for R
https://plotly-r.com
Other
2.56k stars 626 forks source link

ggtree support #2380

Open Edmondbrn opened 2 months ago

Edmondbrn commented 2 months ago

Hello everyone. I wanted to plot a phylogeny tree thanks to ggtree R package. Yet, I have too many species (around 1000) and the graph is unreadable. Thus, I wanted to convert the plot into plotly format to be able to zoom in. But I got this error message (see below).

Thank you for your help !

tree = read.tree("ressources/phylogeny_tree.tree")
p <- ggtree(tree, layout = "circular") + 
  geom_tiplab(size = 2) + 
  theme_tree2()  
ggplotly(p)
5: In geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
  geom_GeomTextGGtree() has yet to be implemented in plotly.
  If you'd like to see this geom implemented,
  Please open an issue with your example code at
  https://github.com/ropensci/plotly/issues
6: In geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
  geom_GeomTextGGtree() has yet to be implemented in plotly.
  If you'd like to see this geom implemented,
  Please open an issue with your example code at
  https://github.com/ropensci/plotly/issues
yoelii commented 2 months ago

Same warning message when I try to convert the same kind od tree. The tree made with plotly doesn't show the annotation but only the tree.

library(ggimage)
library(ggtree)
library(TDbook)

# load `tree_boots`, `df_tip_data`, and `df_inode_data` from 'TDbook'
p <- ggtree(tree_boots) %<+% df_tip_data + xlim(-.1, 4)
p2 <- p + geom_tiplab(offset = .6, hjust = .5) +
  geom_tippoint(aes(shape = trophic_habit, color = trophic_habit, 
                    size = mass_in_kg)) + 
  theme(legend.position = "right") + 
  scale_size_continuous(range = c(3, 10))

my_plotly <- ggplotly(p2)
my_plotly <- ggplotly(p2)
Warning messages:
1: In geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
  geom_GeomTextGGtree() has yet to be implemented in plotly.
  If you'd like to see this geom implemented,
  Please open an issue with your example code at
  https://github.com/ropensci/plotly/issues
2: In geom2trace.default(dots[[1L]][[3L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
  geom_GeomPointGGtree() has yet to be implemented in plotly.
  If you'd like to see this geom implemented,
  Please open an issue with your example code at
  https://github.com/ropensci/plotly/issues
3: In geom2trace.default(dots[[1L]][[3L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
  geom_GeomPointGGtree() has yet to be implemented in plotly.
  If you'd like to see this geom implemented,
  Please open an issue with your example code at
  https://github.com/ropensci/plotly/issues
4: In geom2trace.default(dots[[1L]][[3L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
  geom_GeomPointGGtree() has yet to be implemented in plotly.
  If you'd like to see this geom implemented,
  Please open an issue with your example code at
  https://github.com/ropensci/plotly/issues
sessionInfo()
R version 4.4.1 (2024-06-14)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so;  LAPACK version 3.10.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Etc/UTC
tzcode source: system (glibc)

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

other attached packages:
 [1] TDbook_0.0.6       ggimage_0.3.3      plotly_4.10.4      pak_0.7.2          aplot_0.2.3        phytools_2.3-0     maps_3.4.2         ape_5.8            treeio_1.28.0      ggtree_3.12.0      scales_1.3.0       reshape2_1.4.4    
[13] factoextra_1.0.7   FactoMineR_2.11    ggcorrplot_0.1.4.1 corrplot_0.94      corrr_0.4.4        viridis_0.6.5      viridisLite_0.4.2  ggpubr_0.6.0       ggExtra_0.10.1     lubridate_1.9.3    forcats_1.0.0      stringr_1.5.1     
[25] dplyr_1.1.4        purrr_1.0.2        readr_2.1.5        tidyr_1.3.1        tibble_3.2.1       ggplot2_3.5.1      tidyverse_2.0.0   

loaded via a namespace (and not attached):
  [1] rstudioapi_0.16.0       jsonlite_1.8.8          magrittr_2.0.3          magick_2.8.4            estimability_1.5.1      farver_2.1.2            rmarkdown_2.27          fs_1.6.4                ragg_1.3.2             
 [10] vctrs_0.6.5             rstatix_0.7.2           htmltools_0.5.8.1       DEoptim_2.2-8           broom_1.0.6             gridGraphics_0.5-1      htmlwidgets_1.6.4       plyr_1.8.9              httr2_1.0.1            
 [19] emmeans_1.10.4          igraph_2.0.3            mime_0.12               lifecycle_1.0.4         iterators_1.0.14        pkgconfig_2.0.3         Matrix_1.7-0            R6_2.5.1                fastmap_1.2.0          
 [28] shiny_1.8.1.1           digest_0.6.35           numDeriv_2016.8-1.1     colorspace_2.1-0        patchwork_1.2.0         ps_1.7.6                textshaping_0.4.0       crosstalk_1.2.1         labeling_0.4.3         
 [37] clusterGeneration_1.3.8 fansi_1.0.6             timechange_0.3.0        httr_1.4.7              abind_1.4-5             compiler_4.4.1          withr_3.0.0             doParallel_1.0.17       backports_1.5.0        
 [46] optimParallel_1.0-2     carData_3.0-5           ggsignif_0.6.4          MASS_7.3-60.2           rappdirs_0.3.3          scatterplot3d_0.3-44    flashClust_1.01-2       tools_4.4.1             httpuv_1.6.15          
 [55] glue_1.7.0              quadprog_1.5-8          callr_3.7.6             nlme_3.1-164            promises_1.3.0          grid_4.4.1              cluster_2.1.6           generics_0.1.3          gtable_0.3.5           
 [64] tzdb_0.4.0              data.table_1.15.4       hms_1.1.3               car_3.1-2               utf8_1.2.4              ggrepel_0.9.5           foreach_1.5.2           pillar_1.9.0            yulab.utils_0.1.6      
 [73] later_1.3.2             lattice_0.22-6          tidyselect_1.2.1        miniUI_0.1.1.1          knitr_1.47              gridExtra_2.3           xfun_0.44               expm_1.0-0              DT_0.33                
 [82] stringi_1.8.4           lazyeval_0.2.2          ggfun_0.1.5             yaml_2.3.8              evaluate_0.24.0         codetools_0.2-20        multcompView_0.1-10     ggplotify_0.1.2         cli_3.6.2              
 [91] xtable_1.8-4            systemfonts_1.1.0       munsell_0.5.1           processx_3.8.4          Rcpp_1.0.12             coda_0.19-4.1           parallel_4.4.1          leaps_3.2               phangorn_2.11.1        
[100] mvtnorm_1.2-6           tidytree_0.4.6          combinat_0.0-8          rlang_1.1.4             fastmatch_1.1-4         mnormt_2.1.1 

p2 :

Rplot

my_plotly :

Capture d’écran du 2024-08-26 16-39-18

yoelii commented 2 months ago

Following the instructions in https://plotly-r.com/custom-geoms, I added new function to help the ggtree / ploty convertion:

New functions :

to_basic.GeomTextGGtree <- function(data, prestats_data, layout, params, p, ...) {
  data <- data[order(data[["x"]]), ]
  plotly:::prefix_class(data, "GeomText")
}

to_basic.GeomPointGGtree <- function(data, prestats_data, layout, params, p, ...) {
  data <- data[order(data[["x"]]), ]
  plotly:::prefix_class(data, "GeomPoint")
}

New execution :

p <- ggtree(tree_boots) %<+% df_tip_data + xlim(-.1, 4)
p2 <- p + geom_tiplab(offset = .6, hjust = .5) +
  geom_tippoint(aes(shape = trophic_habit, color = trophic_habit, 
                    size = mass_in_kg)) + 
  theme(legend.position = "right") + 
  scale_size_continuous(range = c(3, 10))

my_plotly <- ggplotly(p2)

Outputs : p2 == my_ploty

newplot (1)