Skip to contents
library(ggalign)
#> Loading required package: ggplot2
set.seed(123)
small_mat <- matrix(rnorm(81), nrow = 9)
rownames(small_mat) <- paste0("row", seq_len(nrow(small_mat)))
colnames(small_mat) <- paste0("column", seq_len(ncol(small_mat)))

Why I cannot add a dendrogram with reorder_group = TRUE behind a dendrogram with reorder_group = FALSE?

Note: We always prevent users from reordering the layout axis twice.

When reorder_group = FALSE is used, reordering of the heatmap occurs within each group. As long as the ordering within each group remains consistent, these two dendrograms can be placed on the same axis of the heatmap.

Let’s assume that panels represents the group for each observation and index represents the ordered index of the observations.

In the code provided, panels is initialized as ungrouped and index as unordered:

panels <- NULL
index <- NULL
set.seed(2L)
group <- sample(letters[1:3], ncol(small_mat), replace = TRUE)
h <- ggheatmap(small_mat) +
    hmanno("t") +
    align_group(group)

This dose initialized the panels.

panels <- factor(group)

When we add a dendrogram with reorder_group = FALSE; this’ll do something like this (Since reorder_group = FALSE, the panels won’t be changed):

h <- h + align_dendro(reorder_group = FALSE)
# always remember dendrogram will initialize the index, here, we extract the
# index from the underlying dendrogram
index <- stats::order.dendrogram(ggalign_stat(h, position = "top", what = 2L))

However, if a dendrogram with reorder_group = TRUE is added, it will reorder the panels. Here, the panel levels are sampled to create new groups:

panels <- factor(panels, sample(levels(panels)))
new_index <- unlist(split(index, panels[index]),
    recursive = FALSE, use.names = FALSE
)
all(index == new_index)
#> [1] FALSE
h + align_dendro(reorder_group = TRUE)
#> Error in `align_dendro()`:
#> ! `align_dendro()` disrupt the previously established order of the
#>   layout x-axis

To prevent reordering of the layout axis twice, we ensure that the new_index matches the index (if index is not NULL). This is done to maintain consistency and prevent conflicts in the ordering of the heatmap.

Can I change the default data for all geoms?

All function in ggalign requires specific data formats for its operations. If you need to transform or filter data for individual geoms, you can use the data argument within each geom. However, if you have multiple geoms and want a consistent transformation applied across all, you can utilize the plot_data argument in the ggalign() or ggpanel() function. This allows you to transform the default data for all additive geoms.

Session information

sessionInfo()
#> R version 4.4.1 (2024-06-14)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 22.04.5 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.20.so;  LAPACK version 3.10.0
#> 
#> locale:
#>  [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
#>  [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
#>  [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
#> [10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   
#> 
#> time zone: UTC
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggalign_0.0.4 ggplot2_3.5.1
#> 
#> loaded via a namespace (and not attached):
#>  [1] vctrs_0.6.5       cli_3.6.3         knitr_1.48        rlang_1.1.4      
#>  [5] xfun_0.47         generics_0.1.3    textshaping_0.4.0 jsonlite_1.8.8   
#>  [9] glue_1.7.0        colorspace_2.1-1  htmltools_0.5.8.1 ragg_1.3.3       
#> [13] sass_0.4.9        fansi_1.0.6       scales_1.3.0      rmarkdown_2.28   
#> [17] grid_4.4.1        tibble_3.2.1      evaluate_1.0.0    munsell_0.5.1    
#> [21] jquerylib_0.1.4   fastmap_1.2.0     yaml_2.3.10       lifecycle_1.0.4  
#> [25] compiler_4.4.1    dplyr_1.1.4       fs_1.6.4          pkgconfig_2.0.3  
#> [29] systemfonts_1.1.0 digest_0.6.37     R6_2.5.1          tidyselect_1.2.1 
#> [33] utf8_1.2.4        pillar_1.9.0      magrittr_2.0.3    bslib_0.8.0      
#> [37] withr_3.0.1       tools_4.4.1       gtable_0.3.5      pkgdown_2.1.1    
#> [41] cachem_1.1.0      desc_1.4.3