YuLab-SMU / ggtree

:christmas_tree:Visualization and annotation of phylogenetic trees
https://yulab-smu.top/treedata-book/
830 stars 172 forks source link

Not able to plot a daylight unrooted tree #568

Open barizona opened 1 year ago

barizona commented 1 year ago

Hi,

I use ggtree a lot and it seems like ggtree is unable to plot an unrooted (or daylight which is equivalent to unrooted) tree over 100 tips in a "normal" amount of time or even crash. I've checked it with random trees:

10 tips:

set.seed(0)
ptm <- proc.time()
ggtree(ape::rtree(10), layout = "unrooted")

warning:

"daylight" method was used as default layout for unrooted tree. Average angle change [1] 0.145675100848931 Average angle change [2] 0.0262816196738423

proc.time() - ptm

user system elapsed 0.744 0.028 0.769

100 tips:

ptm <- proc.time()
ggtree(ape::rtree(100), layout = "unrooted")

warning:

"daylight" method was used as default layout for unrooted tree. Average angle change [1] 0.106828210702785 Average angle change [2] 0.0859877115464232 Average angle change [3] 0.0258362495087449

proc.time() - ptm

user system elapsed 13.370 0.509 13.865

1000 tips:

ptm <- proc.time()
ggtree(ape::rtree(1000), layout = "unrooted")

warning:

"daylight" method was used as default layout for unrooted tree. Average angle change [1] 0.149900633269656 Average angle change [2] 0.50743670812811 Average angle change [3] 0.456384124412058 Average angle change [4] 0.440281555357076 Average angle change [5] 0.488928769907407 Error in grid.newpage() : could not open file '/home/.../.Rproj.user/shared/notebooks/9CF56B34-Tree_manipulation/1/790389C0867a41f1/cab4z9vrnplx2_t/_rs_chunk_plot_001.png'

In the case of _equalangle unrooted or rooted tree outputs processing time is not a problem:

ptm <- proc.time()
ggtree(ape::rtree(1000), layout = "equal_angle")
proc.time() - ptm

user system elapsed 6.702 0.233 6.927

ptm <- proc.time()
ggtree(ape::rtree(100))
proc.time() - ptm

user system elapsed 0.130 0.001 0.130

Is it possible to improve this?

Best regards, Eszter

sessionInfo()

R version 4.2.1 (2022-06-23) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 22.04.2 LTS

Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

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

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

other attached packages: [1] ggtree_3.7.1.003 phytools_1.5-1 maps_3.4.1 ape_5.7-1 treeio_1.23.1 lubridate_1.9.2 forcats_1.0.0 stringr_1.5.0 dplyr_1.1.1 purrr_1.0.1 readr_2.1.4 tidyr_1.3.0
[13] tibble_3.2.1 ggplot2_3.4.1 tidyverse_2.0.0

loaded via a namespace (and not attached): [1] Rcpp_1.0.10 lattice_0.20-45 digest_0.6.31 foreach_1.5.2 utf8_1.2.3 R6_2.5.1 coda_0.19-4 ggfun_0.0.9 pillar_1.9.0
[10] yulab.utils_0.0.6 rlang_1.1.0 lazyeval_0.2.2 rstudioapi_0.14 phangorn_2.11.1 Matrix_1.5-3 combinat_0.0-8 igraph_1.4.1 munsell_0.5.0
[19] compiler_4.2.1 numDeriv_2016.8-1.1 xfun_0.38 gridGraphics_0.5-1 pkgconfig_2.0.3 mnormt_2.1.1 optimParallel_1.0-2 tidyselect_1.2.0 expm_0.999-7
[28] quadprog_1.5-8 codetools_0.2-19 fansi_1.0.4 tzdb_0.3.0 withr_2.5.0 MASS_7.3-58.3 grid_4.2.1 nlme_3.1-162 jsonlite_1.8.4
[37] gtable_0.3.3 lifecycle_1.0.3 magrittr_2.0.3 scales_1.2.1 tidytree_0.4.2 cli_3.6.1 stringi_1.7.12 scatterplot3d_0.3-43 doParallel_1.0.17
[46] generics_0.1.3 vctrs_0.6.1 fastmatch_1.1-3 iterators_1.0.14 tools_4.2.1 ggplotify_0.1.0 glue_1.6.2 hms_1.1.3 plotrix_3.8-2
[55] parallel_4.2.1 timechange_0.2.0 colorspace_2.1-0 aplot_0.1.10 clusterGeneration_1.3.7 knitr_1.42 patchwork_1.1.2

nelsonruth11 commented 6 months ago

I see this as well with ggtree 3.10.0

brj1 commented 6 months ago

You can use the MAX_COUNT parameter to reduce the amount of time the daylight algorithm takes. Basically, the algorithm starts with an equal angle layout and then perturbs the branches to get a better arrangement. MAX_COUNT is the maximum number of iterations that the algorithm does (passes through each branch). The default is MAX_COUNT = 5, but you can set it to MAX_COUNT = 1 to only do one iteration (this will result in a more equal angle like layout).

ggtree(tree, layout = 'daylight', MAX_COUNT = 1)

Also, the daylight layout is not really intended for trees with 1000s of tips.

dariotommasini commented 6 months ago

Hi there!

I am also having trouble with the daylight visualization. My unrooted tree looks very ugly. However, it's just a typical unrooted tree:

Screenshot 2024-03-09 at 7 36 57 PM

Screenshot 2024-03-09 at 7 35 50 PM

Do you have any recommendations for how to improve the daylight visualization? I like the equal_angle but the tip labels end up being too close together so I wouldn't mind getting some more space between tips.

brj1 commented 6 months ago

@dariotommasini Did you try using a small MAX_COUNT (1 or 2)? The daylight algorithm works by rotating the branches away from an equal angle layout. It often overcompensates and the branches overlap.

ggtree(tree, layout = 'daylight', MAX_COUNT = 1)