Setup

1. Introduction

Phylogenetic trees was often used with associated data in various biological studies. ggtree, a flexible R package to visualize phylogenetic tree, had been developed by GuangChuang Yu (Yu et al. 2017). It provided geom_facet function to align associated graphs to the tree (Yu et al. 2018; Yu 2020). However, This function did not support the tree created using circular, fan or radial layout. To solve the problem, We developed ggtreeExtra, which can align associated graphs to circular, fan or radial and other rectangular layout tree. ggtreeExtra provides function, geom_fruit to align graphs to the tree. But the associated graphs will align in different position. So we also developed geom_fruit_list to add multiple layers in the same position. Furthermore, axis of external layers can be added using the axis.params=list(axis="x",...) in geom_fruit. The grid lines of external layers can be added using the grid.params=list() in geom_fruit. These functions are based on ggplot2 using grammar of graphics (Wickham 2016). More vignettes can be found on the chapter10 of online book.

2. Install

You can use the following to install it

3. Usage

To show the package profiling, I will use a tree file downloaded from plotTree. The associated datasets were simulated.

3.1 add single layer

ID Location Length Group Abundance
DE0655_HCMC_2001 HK 0.1786629 Yes 12.331055
MS0111_HCMC_1996 HK 0.2105236 Yes 9.652052
MS0063_HCMC_1995 HK 1.4337983 Yes 11.584822
DE0490_HCMC_2000 HK 0.3823731 Yes 7.893231
DE0885_HCMC_2001 HK 0.8478901 Yes 12.117232
DE0891_HCMC_2001 HK 1.5038646 Yes 10.819734
ID Pos Type
DE0846_HCMC_2001 8 type2
MS0034_HCMC_1995 8 type2
EG1017_HCMC_2009 6 type2
KH18_HCMC_2009 5 type2
10365_HCMC_2010 7 type2
EG1021_HCMC_2009 1 type1
ID Type2 Alpha
MS0004_HCMC_1995 p3 0.2256195
DE1150_HCMC_2002 p2 0.2222086
MS0048_HCMC_1995 p2 0.1881510
HUE57_HCMC_2010 p3 0.1939088
DE1486_HCMC_2002 p2 0.2018493
DE1165_HCMC_2002 p3 0.1812997


# We can also add heatmap layer for continuous values.
p4 <- p3 + 
      new_scale_fill() +
      geom_fruit(
          data=dat3,
          geom=geom_tile,
          mapping=aes(y=ID, x=Type2, alpha=Alpha, fill=Type2),
          pwidth=0.15,
          axis.params=list(
                          axis="x", # add axis text of the layer.
                          text.angle=-45, # the text size of axis.
                          hjust=0  # adust the horizontal position of text of axis.
                      )
      ) +
      scale_fill_manual(
          values=c("#b22222", "#005500", "#0000be", "#9f1f9f"),
          guide=guide_legend(keywidth=0.5, keyheight=0.5, order=4)
      ) +
      scale_alpha_continuous(
          range=c(0, 0.4), # the range of alpha
          guide=guide_legend(keywidth=0.5, keyheight=0.5, order=5)
      ) 

# Then we add a bar layer outside of the tree.
p5 <- p4 + 
      new_scale_fill() +
      geom_fruit(
          data=dat1, # The abundance of dat1 will be mapped to x, 
          geom=geom_bar,
          mapping=aes(y=ID, x=Abundance, fill=Location),
          pwidth=0.4,
          stat="identity",
          orientation="y", # the orientation of axis.
          axis.params=list(
                          axis="x", # add axis text of the layer.
                          text.angle=-45, # the text size of axis.
                          hjust=0  # adust the horizontal position of text of axis.
                      ),
          grid.params=list() # add the grid line of the external bar plot.
      ) + 
      scale_fill_manual(
          values=c("#F8766D", "#C49A00", "#53B400", "#00C094", "#00B6EB", "#A58AFF", "#FB61D7"),
          guide=guide_legend(keywidth=0.5, keyheight=0.5, order=6)
      ) +
      theme(#legend.position=c(0.96, 0.5), # the position of legend.
          legend.background=element_rect(fill=NA), # the background of legend.
          legend.title=element_text(size=7), # the title size of legend.
          legend.text=element_text(size=6), # the text size of legend.
          legend.spacing.y = unit(0.02, "cm")  # the distance of legends (y orientation).
      ) 
p5

4. Need helps?

If you have questions/issues, please visit github issue tracker. You also can post to google group. Users are highly recommended to subscribe to the mailing list.

5. Session information

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

#> R version 4.0.3 (2020-10-10)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 18.04.5 LTS
#> 
#> Matrix products: default
#> BLAS:   /home/biocbuild/bbs-3.12-bioc/R/lib/libRblas.so
#> LAPACK: /home/biocbuild/bbs-3.12-bioc/R/lib/libRlapack.so
#> 
#> 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       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggnewscale_0.4.3  treeio_1.14.0     ggtree_2.4.0      ggplot2_3.3.2    
#> [5] ggstar_0.0.9      ggtreeExtra_1.0.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.5          highr_0.8           BiocManager_1.30.10
#>  [4] pillar_1.4.6        compiler_4.0.3      prettydoc_0.4.0    
#>  [7] tools_4.0.3         aplot_0.0.6         digest_0.6.27      
#> [10] jsonlite_1.7.1      tidytree_0.3.3      nlme_3.1-150       
#> [13] evaluate_0.14       lifecycle_0.2.0     tibble_3.0.4       
#> [16] gtable_0.3.0        lattice_0.20-41     pkgconfig_2.0.3    
#> [19] rlang_0.4.8         rvcheck_0.1.8       patchwork_1.0.1    
#> [22] parallel_4.0.3      yaml_2.2.1          xfun_0.18          
#> [25] gridExtra_2.3       withr_2.3.0         stringr_1.4.0      
#> [28] dplyr_1.0.2         knitr_1.30          generics_0.0.2     
#> [31] vctrs_0.3.4         grid_4.0.3          tidyselect_1.1.0   
#> [34] glue_1.4.2          R6_2.4.1            rmarkdown_2.5      
#> [37] farver_2.0.3        tidyr_1.1.2         purrr_0.3.4        
#> [40] magrittr_1.5        scales_1.1.1        ellipsis_0.3.1     
#> [43] htmltools_0.5.0     colorspace_1.4-1    ape_5.4-1          
#> [46] labeling_0.4.2      stringi_1.5.3       lazyeval_0.2.2     
#> [49] munsell_0.5.0       crayon_1.3.4

6. Reference

Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.

Yu, Guangchuang. 2020. “Using Ggtree to Visualize Data on Tree-Like Structures.” Current Protocols in Bioinformatics 69 (1):e96. https://doi.org/10.1002/cpbi.96.

Yu, Guangchuang, Tommy Tsan-Yuk Lam, Huachen Zhu, and Yi Guan. 2018. “Two Methods for Mapping and Visualizing Associated Data on Phylogeny Using Ggtree.” Molecular Biology and Evolution 35 (2). Oxford University Press:3041–3. https://doi.org/10.1093/molbev/msy194.

Yu, Guangchuang, David Smith, Huachen Zhu, Yi Guan, and Tommy Tsan-Yuk Lam. 2017. “Ggtree: An R Package for Visualization and Annotation of Phylogenetic Trees with Their Covariates and Other Associated Data.” Methods in Ecology and Evolution 8 (1). Wiley Online Library:28–36. https://doi.org/10.1111/2041-210X.12628.