layout_stack()
put plots horizontally or vertically. You
can also use the alias ggstack()
.
The data input can be a numeric or character vector, a matrix, and a data frame. Simple vector will be converted into a one column matrix.
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)))
By default, ggstack()
will create the layout, but no
plot will be drawn until you add a plot element:
We can add any align_*()
function to customize the
layout or integrate plots into the stack.
ggstack(small_mat) +
align_kmeans(centers = 3L) +
ggalign(rowSums) +
geom_bar(aes(value, fill = .panel), orientation = "y", stat = "identity") +
facet_grid(switch = "y") +
theme(strip.text = element_text()) +
align_dendro(aes(color = branch))
By default, ggstack()
arranges the plots horizontally.
To change the direction to vertical, use the direction
argument:
Unlike layout_heatmap()
/ggheatmap()
, data
frames are not automatically converted into a matrix within
ggstack()
.
When using data frames, be cautious as many align_*()
functions only accept matrices. If the necessary data is not explicitly
provided to an align_*()
function, the data frame from
ggstack()
will be passed to the function and internally
converted into a matrix, which may result in missing values.
An exception is the align_gg()
/ggalign()
function, which can handle both matrix and data frames. When the input
is a matrix (or a simple vector), it is automatically transformed into a
long-format data frame. When the input is a data frame, only the
necessary panel and axis information is added to the data frame.
Note align_gg()
/ggalign()
always applies a
default mapping for the parallel axes of the data index within the
layout. This mapping is aes(y = .data$.y)
for horizontal
stack and aes(x = .data$.x)
for vertical stack. So here we
only provide mapping for the x-axis
, for the horizontal
stack, we default use the data index as the y-axis
.
For more information on adding plots, refer to the vignette:
vignette("align-plot")
.
Besides the align_*()
functions, we can also add the
layout_heatmap()
/ggheatmap()
into the stack
layout.
sessionInfo()
#> R version 4.4.0 (2024-04-24)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04 LTS
#>
#> Matrix products: default
#> BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/libmkl_rt.so; LAPACK version 3.8.0
#>
#> locale:
#> [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
#> [4] LC_COLLATE=C 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: Asia/Shanghai
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] ggalign_0.0.3 ggplot2_3.5.1
#>
#> loaded via a namespace (and not attached):
#> [1] gtable_0.3.5 jsonlite_1.8.8 dplyr_1.1.4 compiler_4.4.0
#> [5] highr_0.11 tidyselect_1.2.1 tidyr_1.3.1 jquerylib_0.1.4
#> [9] scales_1.3.0 yaml_2.3.8 fastmap_1.2.0 ggh4x_0.2.8
#> [13] R6_2.5.1 labeling_0.4.3 generics_0.1.3 knitr_1.47
#> [17] tibble_3.2.1 munsell_0.5.1 RColorBrewer_1.1-3 bslib_0.7.0
#> [21] pillar_1.9.0 rlang_1.1.4 utf8_1.2.4 cachem_1.1.0
#> [25] xfun_0.45 sass_0.4.9 viridisLite_0.4.2 cli_3.6.3
#> [29] withr_3.0.0 magrittr_2.0.3 digest_0.6.36 grid_4.4.0
#> [33] lifecycle_1.0.4 vctrs_0.6.5 evaluate_0.24.0 glue_1.7.0
#> [37] farver_2.1.2 fansi_1.0.6 colorspace_2.1-0 rmarkdown_2.27
#> [41] purrr_1.0.2 tools_4.4.0 pkgconfig_2.0.3 htmltools_0.5.8.1