13  Complete examples

Code
library(ggalign)
#> Loading required package: ggplot2
set.seed(123)
small_mat <- matrix(rnorm(56), nrow = 7)
rownames(small_mat) <- paste0("row", seq_len(nrow(small_mat)))
colnames(small_mat) <- paste0("column", seq_len(ncol(small_mat)))

13.1 Simple heatmap

ggheatmap(small_mat)
#> → heatmap built with `geom_tile()`

13.2 heatmap layout customize

13.2.1 Based on dendrogram

ggheatmap(small_mat) +
    anno_top() +
    align_dendro(aes(color = branch), k = 3) +
    geom_point(aes(color = branch, y = y)) +
    scale_color_brewer(palette = "Dark2")
#> → heatmap built with `geom_tile()`

13.2.2 Based on kmeans

ggheatmap(small_mat) +
    anno_top() +
    align_kmeans(3L)
#> → heatmap built with `geom_tile()`

13.2.3 Based on a group variable

ggheatmap(small_mat) +
    anno_top() +
    align_group(sample(letters[1:4], ncol(small_mat), replace = TRUE))
#> → heatmap built with `geom_tile()`

13.2.4 Based on an ordering weights

Here, we ordered the heatmap rows based on the row means.

ggheatmap(small_mat) +
    anno_left() +
    align_order(rowMeans)
#> → heatmap built with `geom_tile()`

13.3 Heatmap annotation plot

ggheatmap(small_mat) +
    anno_top() +
    align_dendro(aes(color = branch), k = 3) +
    geom_point(aes(color = branch, y = y)) +
    scale_color_brewer(palette = "Dark2") +
    ggalign(mapping = aes(y = value)) +
    geom_boxplot(aes(factor(.x), fill = .panel)) +
    scale_fill_brewer(palette = "Dark2")
#> → heatmap built with `geom_tile()`

ggheatmap(small_mat) +
    anno_top(size = 0.5) +
    align_dendro(aes(color = branch), k = 3L) +
    ggalign(rowSums, aes(y = value)) +
    geom_bar(stat = "identity", aes(fill = factor(.panel))) +
    scale_fill_brewer(name = NULL, palette = "Dark2") +
    anno_left(size = 0.5) +
    align_dendro(aes(color = branch), size = 0.5, k = 4L) +
    ggalign(rowSums, aes(x = value)) +
    geom_bar(
        aes(y = .y, fill = factor(.y)),
        stat = "identity",
        orientation = "y"
    ) +
    scale_fill_brewer(name = NULL, palette = "Paired", guide = "none")
#> → heatmap built with `geom_tile()`

13.4 Multiple heatmaps

13.4.1 Horizontal layout

(stack_alignh(small_mat) +
    ggheatmap() +
    ggheatmap() &
    theme(axis.text.x = element_text(angle = -60, hjust = 0))) +
    stack_active() +
    align_dendro(aes(color = branch), k = 4L, size = 0.2) +
    scale_color_brewer(palette = "Dark2")
#> → heatmap built with `geom_tile()`
#> → heatmap built with `geom_tile()`

13.4.2 Vertical layout

stack_alignv(small_mat) -
    scheme_theme(
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank()
    ) +
    align_dendro(aes(color = branch), k = 4L, size = 0.2) +
    scale_color_brewer(palette = "Dark2") +
    ggheatmap() +
    ggheatmap() +
    theme(axis.text.x = element_text(angle = -60, hjust = 0))
#> → heatmap built with `geom_tile()`
#> → heatmap built with `geom_tile()`

13.5 marginal plots

ggside(mpg, aes(displ, hwy, colour = class)) -
    # set default theme for all plots in the layout
    scheme_theme(theme_bw()) +
    geom_point(size = 2) +
    # add top annotation
    anno_top(size = 0.3) -
    # set default theme for the top annotation
    scheme_theme(theme_no_axes("tb")) +
    # add a plot in the top annotation
    ggfree() +
    geom_density(aes(displ, y = after_stat(density), colour = class), position = "stack") +
    anno_right(size = 0.3) -
    # set default theme for the right annotation
    scheme_theme(theme_no_axes("lr")) +
    # add a plot in the right annotation
    ggfree() +
    geom_density(aes(x = after_stat(density), hwy, colour = class),
        position = "stack"
    ) +
    theme(axis.text.x = element_text(angle = 90, vjust = .5)) &
    scale_color_brewer(palette = "Dark2")