Skip to content

Instantly share code, notes, and snippets.

@joelnitta
Last active January 27, 2021 05:22
Show Gist options
  • Save joelnitta/97a03ab1f32af99cc0d80ade4e4a444b to your computer and use it in GitHub Desktop.
Save joelnitta/97a03ab1f32af99cc0d80ade4e4a444b to your computer and use it in GitHub Desktop.
How to print bootstrap support values with ggtree
# How to print bootstrap support values with ggtree
library(ape)
library(ggtree)
library(tibble)
# phy is a list of class 'phylo' as defined in ape, with node labels
# as typically obtained after running ape::read.tree() on a tree in newick format
# (here, filmy ferns from Moorea, French Polynesia)
phy <- read.tree("https://gist.githubusercontent.com/joelnitta/11b80d76cc44b7d0492cc6f7fe0acaa8/raw")
# Make a dataframe (tibble) with node IDs (integers) and their corresponding bootstrap support values
# The tibble has two columns: one called "node", the other can be named as we like (here, "bootstrap")
bs_tibble <- tibble(
# hard-code node ID: internal nodes start after tip nodes,
# and phy$node.label is in the same order as internal nodes
node=1:Nnode(phy) + Ntip(phy),
# Don't print BS < 50%
# (or to show all BS, just do `bootstrap = phy$node.label`)
bootstrap = ifelse(phy$node.label < 50, "", phy$node.label))
# Use the ggtree::`%<+%` operator to map the bootstrap values onto the ggtree object
ggtree(phy) %<+% bs_tibble +
# now we can show BS values using geom_text()
geom_text(aes(label=bootstrap), hjust=-.25, size = 3) +
# add tip labels
geom_tiplab(aes(label=label))
# output can be viewed here https://i.imgur.com/9ceY0L9.png
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.0.2 (2020-06-22)
#> os Ubuntu 20.04 LTS
#> system x86_64, linux-gnu
#> ui X11
#> language (EN)
#> collate en_US.UTF-8
#> ctype en_US.UTF-8
#> tz Etc/UTC
#> date 2021-01-27
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date lib source
#> ape * 5.4-1 2020-08-13 [1] CRAN (R 4.0.2)
#> aplot 0.0.6 2020-09-03 [1] CRAN (R 4.0.2)
#> assertthat 0.2.1 2019-03-21 [1] RSPM (R 4.0.0)
#> backports 1.1.9 2020-08-24 [1] CRAN (R 4.0.2)
#> BiocManager 1.30.10 2019-11-16 [1] RSPM (R 4.0.0)
#> callr 3.4.3 2020-03-28 [1] CRAN (R 4.0.2)
#> cli 2.0.2 2020-02-28 [1] CRAN (R 4.0.2)
#> colorspace 1.4-1 2019-03-18 [1] RSPM (R 4.0.0)
#> crayon 1.3.4 2017-09-16 [1] RSPM (R 4.0.0)
#> curl 4.3 2019-12-02 [1] RSPM (R 4.0.0)
#> desc 1.2.0 2018-05-01 [1] RSPM (R 4.0.0)
#> devtools 2.3.2 2020-09-18 [2] RSPM (R 4.0.2)
#> digest 0.6.25 2020-02-23 [1] RSPM (R 4.0.0)
#> dplyr 1.0.2 2020-08-18 [1] RSPM (R 4.0.2)
#> ellipsis 0.3.1 2020-05-15 [1] RSPM (R 4.0.0)
#> evaluate 0.14 2019-05-28 [1] RSPM (R 4.0.0)
#> fansi 0.4.1 2020-01-08 [1] RSPM (R 4.0.0)
#> farver 2.0.3 2020-01-16 [1] RSPM (R 4.0.0)
#> fs 1.5.0 2020-07-31 [1] RSPM (R 4.0.2)
#> generics 0.0.2 2018-11-29 [1] RSPM (R 4.0.0)
#> ggplot2 3.3.2 2020-06-19 [1] RSPM (R 4.0.1)
#> ggtree * 2.5.1 2021-01-26 [1] Github (YuLab-SMU/ggtree@652e1e0)
#> glue 1.4.2 2020-08-27 [1] RSPM (R 4.0.2)
#> gtable 0.3.0 2019-03-25 [1] RSPM (R 4.0.0)
#> highr 0.8 2019-03-20 [1] RSPM (R 4.0.0)
#> htmltools 0.5.0 2020-06-16 [1] RSPM (R 4.0.1)
#> httr 1.4.2 2020-07-20 [1] RSPM (R 4.0.2)
#> jsonlite 1.7.0 2020-06-25 [1] CRAN (R 4.0.2)
#> knitr 1.29 2020-06-23 [1] CRAN (R 4.0.2)
#> labeling 0.3 2014-08-23 [1] RSPM (R 4.0.0)
#> lattice 0.20-41 2020-04-02 [3] CRAN (R 4.0.2)
#> lazyeval 0.2.2 2019-03-15 [1] RSPM (R 4.0.0)
#> lifecycle 0.2.0 2020-03-06 [1] RSPM (R 4.0.0)
#> magrittr 1.5 2014-11-22 [1] RSPM (R 4.0.0)
#> memoise 1.1.0 2017-04-21 [1] RSPM (R 4.0.0)
#> mime 0.9 2020-02-04 [1] RSPM (R 4.0.0)
#> munsell 0.5.0 2018-06-12 [1] RSPM (R 4.0.0)
#> nlme 3.1-149 2020-08-23 [1] CRAN (R 4.0.2)
#> patchwork 1.1.0.9000 2021-01-26 [1] Github (thomasp85/patchwork@79223d3)
#> pillar 1.4.6 2020-07-10 [1] RSPM (R 4.0.2)
#> pkgbuild 1.1.0 2020-07-13 [1] RSPM (R 4.0.2)
#> pkgconfig 2.0.3 2019-09-22 [1] RSPM (R 4.0.0)
#> pkgload 1.1.0 2020-05-29 [1] RSPM (R 4.0.0)
#> prettyunits 1.1.1 2020-01-24 [1] RSPM (R 4.0.0)
#> processx 3.4.3 2020-07-05 [1] CRAN (R 4.0.2)
#> ps 1.3.4 2020-08-11 [1] CRAN (R 4.0.2)
#> purrr 0.3.4 2020-04-17 [1] RSPM (R 4.0.0)
#> R6 2.4.1 2019-11-12 [1] RSPM (R 4.0.0)
#> Rcpp 1.0.5 2020-07-06 [1] RSPM (R 4.0.2)
#> remotes 2.2.0 2020-07-21 [2] RSPM (R 4.0.2)
#> renv 0.12.3 2020-11-25 [1] CRAN (R 4.0.2)
#> rlang 0.4.7 2020-07-09 [1] CRAN (R 4.0.2)
#> rmarkdown 2.4 2020-09-30 [1] RSPM (R 4.0.2)
#> rprojroot 1.3-2 2018-01-03 [1] RSPM (R 4.0.0)
#> rvcheck 0.1.8 2020-03-01 [1] CRAN (R 4.0.2)
#> scales 1.1.1 2020-05-11 [1] RSPM (R 4.0.0)
#> sessioninfo 1.1.1 2018-11-05 [2] RSPM (R 4.0.0)
#> stringi 1.4.6 2020-02-17 [1] CRAN (R 4.0.2)
#> stringr 1.4.0 2019-02-10 [1] RSPM (R 4.0.0)
#> testthat 2.3.2 2020-03-02 [1] RSPM (R 4.0.0)
#> tibble * 3.0.3 2020-07-10 [1] CRAN (R 4.0.2)
#> tidyr 1.1.2 2020-08-27 [1] RSPM (R 4.0.2)
#> tidyselect 1.1.0 2020-05-11 [1] RSPM (R 4.0.0)
#> tidytree 0.3.3 2020-04-02 [1] CRAN (R 4.0.2)
#> treeio 1.15.2 2021-01-26 [1] Github (GuangchuangYu/treeio@28e6adc)
#> usethis 2.0.0 2020-12-10 [1] CRAN (R 4.0.2)
#> vctrs 0.3.6 2020-12-17 [1] CRAN (R 4.0.2)
#> withr 2.4.0 2021-01-16 [1] CRAN (R 4.0.2)
#> xfun 0.16 2020-07-24 [1] CRAN (R 4.0.2)
#> xml2 1.3.2 2020-04-23 [1] RSPM (R 4.0.0)
#> yaml 2.2.1 2020-02-01 [1] RSPM (R 4.0.0)
#>
#> [1] /renv/R-4.0/x86_64-pc-linux-gnu
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/local/lib/R/library
#> [4] /tmp/RtmprLr5qu/renv-system-library
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment