Phylobar draws a phylogenetic tree alongside a stacked bar chart. This vignette shows how to customize visual style: text sizes, color palettes, layout ratios, legend placement, and more.
We will use a small random dataset:
library(ape)
library(phylobar)
set.seed(1)
tree <- rtree(20)
samples <- matrix(rpois(100 * 20, 1), nrow = 100, ncol = 20)
phylobar(samples, tree)The function signature and key styling parameters we will explore:
palette: colors used for painted subtrees (stacked
bars)width, height: widget size in pixelssample_font_size, sample_label_margin,
sample_label_spacesample_magnify, sample_show_allrel_width, rel_height,
rel_spacelegend_mode, legend_x_start,
legend_spacinghclust_order: optional hierarchical reordering of
rows/columnsInternally, phylobar() ensures that the tree has node
labels and that the abundance matrix has row/column names. If they’re
missing, helper check_inputs() creates sensible defaults
(e.g., sample_1, sample_2).
If a palette is not provided, phylobar uses a default set of six colors. It is possible to supply own vector of hex colors or R color names.
By default the widget adapts to the container. Fix its size by:
Control sample label font size, spacing, and hover magnification.
Change how much horizontal and vertical space the tree occupies:
rel_width: fraction of total width reserved for the
tree panel (default 0.4)rel_height: fraction of total height for the tree panel
(default 0.85)rel_space: pixels between the tree and bar panels
(default 10)Choose whether painted subtree labels appear in a separate legend
(legend_mode = TRUE, default) or placed inside the tree
(legend_mode = FALSE). legend_x_start and
legend_spacing can also be adjusted to control the legen
position.
# Legend below the tree (default)
phylobar(
samples, tree,
width = 800, height = 500,
sample_label_space = 100,
sample_magnify = 1.3,
legend_mode = TRUE,
legend_x_start = 20, # horizontal start in pixels
legend_spacing = 20 # vertical spacing between legend items in pixels
)# Put labels inside the tree instead of a separate legend
phylobar(
samples, tree,
width = 800, height = 500,
sample_label_space = 100,
sample_magnify = 1.3,
legend_mode = FALSE,
legend_x_start = 20, # horizontal start in pixels
legend_spacing = 20 # vertical spacing between legend items in pixels
)hclust_order = TRUE (default) reorders samples/features
by hierarchical clustering, which can make patterns more visible. Turn
it off to preserve original order:
sessionInfo()
#> R version 4.6.0 (2026-04-24)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.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.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=en_US.UTF-8
#> [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] zen4R_0.10.5 seriation_1.5.8 tidyr_1.3.2 tibble_3.3.1
#> [5] readr_2.2.0 dplyr_1.2.1 microbiome_1.35.0 ggplot2_4.0.3
#> [9] phyloseq_1.57.0 phylobar_0.99.12 ape_5.8-1 BiocStyle_2.41.0
#>
#> loaded via a namespace (and not attached):
#> [1] phangorn_2.12.1 permute_0.9-10
#> [3] rlang_1.2.0 magrittr_2.0.5
#> [5] ade4_1.7-24 otel_0.2.0
#> [7] matrixStats_1.5.0 compiler_4.6.0
#> [9] mgcv_1.9-4 vctrs_0.7.3
#> [11] reshape2_1.4.5 quadprog_1.5-8
#> [13] stringr_1.6.0 pkgconfig_2.0.3
#> [15] crayon_1.5.3 fastmap_1.2.0
#> [17] XVector_0.53.0 utf8_1.2.6
#> [19] ca_0.71.1 rmarkdown_2.31
#> [21] tzdb_0.5.0 bit_4.6.0
#> [23] purrr_1.2.2 xfun_0.58
#> [25] cachem_1.1.0 jsonlite_2.0.0
#> [27] biomformat_1.41.0 DelayedArray_0.39.3
#> [29] parallel_4.6.0 cluster_2.1.8.2
#> [31] R6_2.6.1 bslib_0.11.0
#> [33] stringi_1.8.7 RColorBrewer_1.1-3
#> [35] GenomicRanges_1.65.0 jquerylib_0.1.4
#> [37] Rcpp_1.1.1-1.1 Seqinfo_1.3.0
#> [39] SummarizedExperiment_1.43.0 iterators_1.0.14
#> [41] knitr_1.51 IRanges_2.47.2
#> [43] Matrix_1.7-5 splines_4.6.0
#> [45] igraph_2.3.2 tidyselect_1.2.1
#> [47] abind_1.4-8 yaml_2.3.12
#> [49] vegan_2.7-5 TSP_1.2.7
#> [51] codetools_0.2-20 curl_7.1.0
#> [53] lattice_0.22-9 plyr_1.8.9
#> [55] Biobase_2.73.1 withr_3.0.2
#> [57] S7_0.2.2 evaluate_1.0.5
#> [59] Rtsne_0.17 survival_3.8-6
#> [61] xml2_1.5.2 Biostrings_2.81.3
#> [63] pillar_1.11.1 BiocManager_1.30.27
#> [65] MatrixGenerics_1.25.0 foreach_1.5.2
#> [67] stats4_4.6.0 generics_0.1.4
#> [69] vroom_1.7.1 S4Vectors_0.51.3
#> [71] hms_1.1.4 scales_1.4.0
#> [73] glue_1.8.1 maketools_1.3.2
#> [75] tools_4.6.0 sys_3.4.3
#> [77] data.table_1.18.4 registry_0.5-1
#> [79] buildtools_1.0.0 XML_3.99-0.23
#> [81] fastmatch_1.1-8 grid_4.6.0
#> [83] nlme_3.1-169 cli_3.6.6
#> [85] S4Arrays_1.13.0 keyring_1.4.1
#> [87] gtable_0.3.6 sass_0.4.10
#> [89] digest_0.6.39 BiocGenerics_0.59.7
#> [91] SparseArray_1.13.2 htmlwidgets_1.6.4
#> [93] farver_2.1.2 htmltools_0.5.9
#> [95] multtest_2.69.0 lifecycle_1.0.5
#> [97] httr_1.4.8 bit64_4.8.2
#> [99] MASS_7.3-65