Customizing Style in phylobar

Overview

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 pixels
  • sample_font_size, sample_label_margin, sample_label_space
  • sample_magnify, sample_show_all
  • rel_width, rel_height, rel_space
  • legend_mode, legend_x_start, legend_spacing
  • hclust_order: optional hierarchical reordering of rows/columns

Internally, 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).

Color Palette

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.

# Custom qualitative palette (as many colors as you want; repeats if needed)
my_palette <- c(
    "#4E79A7", "#F28E2B", "#E15759",
    "#76B7B2", "#59A14F", "#EDC948"
)
phylobar(samples, tree, palette = my_palette)

Widget Size

By default the widget adapts to the container. Fix its size by:

phylobar(samples, tree,
    width = 800,
    height = 500
)

Sample Label Styling

Control sample label font size, spacing, and hover magnification.

phylobar(samples, tree,
    width = 800, height = 500,
    sample_font_size = 10,
    sample_label_margin = 10, # space between labels and bars
    sample_label_space = 100, # reserved margin for labels
    sample_magnify = 1.3, # how much to enlarge labels on hover
    sample_show_all = TRUE
)

Tree-bar Layout Ratio

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)
phylobar(
    samples, tree,
    width = 800, height = 500,
    sample_label_space = 100,
    sample_magnify = 1.3,
    rel_width = 0.2, # narrower tree
    rel_height = 0.70, # shorter tree (more space for legend)
    rel_space = 14 # larger gap between panels
)

Legend Placement

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
)

Optional Hierarchical Reordering

hclust_order = TRUE (default) reorders samples/features by hierarchical clustering, which can make patterns more visible. Turn it off to preserve original order:

# Keep the input order as-is
phylobar(
    samples, tree,
    width = 800, height = 500,
    sample_label_space = 100,
    sample_magnify = 1.3,
    hclust_order = FALSE
)

Session Info

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                 matrixStats_1.5.0          
#>  [7] compiler_4.6.0              mgcv_1.9-4                 
#>  [9] vctrs_0.7.3                 reshape2_1.4.5             
#> [11] quadprog_1.5-8              stringr_1.6.0              
#> [13] pkgconfig_2.0.3             crayon_1.5.3               
#> [15] fastmap_1.2.0               XVector_0.53.0             
#> [17] ca_0.71.1                   utf8_1.2.6                 
#> [19] rmarkdown_2.31              tzdb_0.5.0                 
#> [21] bit_4.6.0                   purrr_1.2.2                
#> [23] xfun_0.57                   cachem_1.1.0               
#> [25] jsonlite_2.0.0              biomformat_1.41.0          
#> [27] DelayedArray_0.39.1         parallel_4.6.0             
#> [29] cluster_2.1.8.2             R6_2.6.1                   
#> [31] bslib_0.10.0                stringi_1.8.7              
#> [33] RColorBrewer_1.1-3          GenomicRanges_1.65.0       
#> [35] jquerylib_0.1.4             Rcpp_1.1.1-1.1             
#> [37] Seqinfo_1.3.0               SummarizedExperiment_1.43.0
#> [39] iterators_1.0.14            knitr_1.51                 
#> [41] IRanges_2.47.0              Matrix_1.7-5               
#> [43] splines_4.6.0               igraph_2.3.1               
#> [45] tidyselect_1.2.1            abind_1.4-8                
#> [47] yaml_2.3.12                 vegan_2.7-3                
#> [49] TSP_1.2.7                   codetools_0.2-20           
#> [51] curl_7.1.0                  lattice_0.22-9             
#> [53] plyr_1.8.9                  Biobase_2.73.1             
#> [55] withr_3.0.2                 S7_0.2.2                   
#> [57] evaluate_1.0.5              Rtsne_0.17                 
#> [59] survival_3.8-6              xml2_1.5.2                 
#> [61] Biostrings_2.81.1           pillar_1.11.1              
#> [63] BiocManager_1.30.27         MatrixGenerics_1.25.0      
#> [65] foreach_1.5.2               stats4_4.6.0               
#> [67] generics_0.1.4              vroom_1.7.1                
#> [69] S4Vectors_0.51.1            hms_1.1.4                  
#> [71] scales_1.4.0                glue_1.8.1                 
#> [73] maketools_1.3.2             tools_4.6.0                
#> [75] sys_3.4.3                   data.table_1.18.4          
#> [77] registry_0.5-1              buildtools_1.0.0           
#> [79] XML_3.99-0.23               fastmatch_1.1-8            
#> [81] grid_4.6.0                  nlme_3.1-169               
#> [83] cli_3.6.6                   S4Arrays_1.13.0            
#> [85] keyring_1.4.1               gtable_0.3.6               
#> [87] sass_0.4.10                 digest_0.6.39              
#> [89] BiocGenerics_0.59.0         SparseArray_1.13.2         
#> [91] htmlwidgets_1.6.4           farver_2.1.2               
#> [93] htmltools_0.5.9             multtest_2.69.0            
#> [95] lifecycle_1.0.5             httr_1.4.8                 
#> [97] bit64_4.8.0                 MASS_7.3-65