ggtreeSpace-Getting Started

Introduction

In the evolving field of evolutionary biology and phylogenetics, visualizing phylomorphospace plays a pivotal role in understanding the diversification of traits across species within a phylogenetic framework. Phylomorphospace is a graphical representation that combines phylogenetic information and morphological (trait) data, mapping the evolutionary trajectories of species in a multidimensional space. However, as research progresses, datasets not only increase in size but also in the complexity of their relationships, which makes the visualization process more challenging and demands advanced visualization solutions.

Here, we introduce ggtreeSpace, a comprehensive visualization tool designed for plotting fully annotated phylomorphospaces using the grammar of graphics, offering researchers with extensive control and customization options for their phylomorphospace plots.

Currently, there are other Bioconductor packages like phytools that also support creating a phylomorphospace. phytools facilitates plotting with its phylomorphospace function, which allows for customization and annotation, including the ability to set edge and node colors. And it also supports plotting 3d phylomorphospace with phylomorphospace3d function.

Compares to phytools, ggtreeSpace focus on crafting 2D phylomorphospaces using the grammar of graphics, enabling the creation of fully annotated visualizations through a layer-by-layer annotation approach. This method provides researchers with a more intuitive and user-friendly experience in plotting, enhancing the logic and visualization clarity. ggtreeSpace not only includes unique layers specifically designed for phylomorphospace annotations but also supports layers from the ggplot2 and ggtree communities, offering a wide range of customization options. Additionally, it supports adding phylomorphospace as a graphic layer, enabling the combination of tree-like structures with various types of spaces, such as maps or histological staining images, thus broadening the applications of phylomorphospace analysis.

Installation

You can use the following commands to install ggtreeSpace:

if (!require("BiocManager"))
    install.packages("BiocManager")
BiocManager::install("ggtreeSpace")

Demonstration

library(ggtree)
library(ggtreeSpace)
library(phytools)
library(ggplot2)
library(dplyr)

ggtreeSpace serves as a wrapper for ggtree package. In the past, when users tried to plot phylomorphospace with ggtree, they needed to be familiar with the infrastructure of it and take multiple steps to achieve the most basic effects. This was not only inconvenient but also limited its usage in more complex analyses.

tr <- rtree(15)
td <- fastBM(tr, nsim = 2)

tda1 <- fastAnc(tr, td[, 1])
tda2 <- fastAnc(tr, td[, 2])
tda <- cbind(tda1, tda2)
tdn <- rbind(td, tda)


trd <- fortify(tr)
trd <- trd |>
        select(-c("x", "y")) |>
        mutate(
            x = tdn[, 1],
            y = tdn[, 2])

p <- ggtree(tr = trd,
            layout = "unrooted") +
        theme_bw()
## "daylight" method was used as default layout for unrooted tree.
p

Now with ggtreeSpace, users can plot basic phylomorphospace easily with the ggtreespace function, and add annotation to it with the + operator. In this example, we add symbolic point to the tip of phylomorphospace. From the phylomorphospace plot, we can observe the evolutionary trajectories of different species, illustrating how they diverge and adapt in their respective trait dimensions. This visual representation allows us to identify patterns of convergence and divergence among species, highlight instances of adaptive radiation, and so on.

ggtreespace(tr, td) +
    geom_tippoint()

ggtreeSpace also supports adding phylomorphospace as a graphic layer. This can broaden the applications of phylomorphospace analysis by combine the tree-like structure with different types of spaces.

tr1 <- rtree(15)
td1 <- fastBM(tr1, nsim = 2)
ggplot() +
    geom_treespace(tr1, td1)

You can also introduce an additional heatmap layer based on your data, adding another dimension of data to better elucidate how this data can affect evolutionary patterns. For example, species may cluster together in the morphospace due to shared environmental adaptations, and we can visualize this through the heatmap. This can provide insights into the underlying mechanisms driving species evolution and diversification.

tr <- rtree(15)
td <- fastBM(tr, nsim = 2, bounds = c(0, Inf))
col <- colorRampPalette(c(
    "#FFFFCC", "#FFEDA0", "#FED976", "#FEB24C",
    "#FD8D3C", "#FC4E2A", "#E31A1C", "#B10026"
))(24)
tdex <- data.frame(
    z = fastBM(tr, nsim = 1, bounds = c(0, Inf)),
    node = 1:15
)
p <- ggtreespace(tr, td)
p %<+% tdex +
    geom_tippoint() +
    geom_tsheatmap(
        trait = "z", alpha = 0.7,
        resolution = 0.01, bin = 24
    ) +
    scale_fill_manual(
        values = col,
        guide = guide_colorsteps(show.limits = TRUE)
    ) +
    theme_treespace2() +
    theme(
        legend.key.height = unit(1, "null"),
        legend.justification.top = "right"
    )
## Warning: Removed 32878 rows containing non-finite outside the scale range
## (`stat_contour_filled()`).

Session information

Here is the output of sessionInfo() on the system on which this document was compiled:

## R version 4.4.2 (2024-10-31)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.1 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.26.so;  LAPACK version 3.12.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=C              
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] 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] dplyr_1.1.4       ggplot2_3.5.1     phytools_2.3-0    maps_3.4.2.1     
## [5] ape_5.8           ggtreeSpace_1.3.0 ggtree_3.15.0     BiocStyle_2.35.0 
## 
## loaded via a namespace (and not attached):
##  [1] fastmatch_1.1-4         gtable_0.3.6            xfun_0.49              
##  [4] bslib_0.8.0             lattice_0.22-6          numDeriv_2016.8-1.1    
##  [7] quadprog_1.5-8          vctrs_0.6.5             tools_4.4.2            
## [10] generics_0.1.3          yulab.utils_0.1.8       parallel_4.4.2         
## [13] tibble_3.2.1            fansi_1.0.6             pkgconfig_2.0.3        
## [16] Matrix_1.7-1            ggplotify_0.1.2         scatterplot3d_0.3-44   
## [19] lifecycle_1.0.4         compiler_4.4.2          farver_2.1.2           
## [22] deldir_2.0-4            treeio_1.31.0           mnormt_2.1.1           
## [25] munsell_0.5.1           combinat_0.0-8          codetools_0.2-20       
## [28] ggfun_0.1.7             htmltools_0.5.8.1       sys_3.4.3              
## [31] buildtools_1.0.0        sass_0.4.9              yaml_2.3.10            
## [34] lazyeval_0.2.2          pillar_1.9.0            jquerylib_0.1.4        
## [37] tidyr_1.3.1             MASS_7.3-61             cachem_1.1.0           
## [40] clusterGeneration_1.3.8 iterators_1.0.14        foreach_1.5.2          
## [43] nlme_3.1-166            phangorn_2.12.1         tidyselect_1.2.1       
## [46] aplot_0.2.3             digest_0.6.37           purrr_1.0.2            
## [49] labeling_0.4.3          maketools_1.3.1         fastmap_1.2.0          
## [52] grid_4.4.2              colorspace_2.1-1        expm_1.0-0             
## [55] cli_3.6.3               magrittr_2.0.3          patchwork_1.3.0        
## [58] optimParallel_1.0-2     utf8_1.2.4              withr_3.0.2            
## [61] scales_1.3.0            DEoptim_2.2-8           rmarkdown_2.29         
## [64] igraph_2.1.1            interp_1.1-6            coda_0.19-4.1          
## [67] evaluate_1.0.1          knitr_1.49              doParallel_1.0.17      
## [70] gridGraphics_0.5-1      rlang_1.1.4             isoband_0.2.7          
## [73] Rcpp_1.0.13-1           glue_1.8.0              tidytree_0.4.6         
## [76] BiocManager_1.30.25     jsonlite_1.8.9          R6_2.5.1               
## [79] fs_1.6.5