Center multiple pheatmap
2
0
Entering edit mode
4.0 years ago
5utr ▴ 370

I have two versions of the same heatmap, clustered and unclustered. The unclustered version doesn't have the dendrogram on top and when I combine them with patchwork the don't align. How can I center the second heatmap so it aligns with the first one?

library(ggplot2)
library(ggplotify)
library(pheatmap)
library(patchwork)

# dummy df
mat = data.frame(matrix(rnorm(100), nrow=20))
# pheatmap as ggplot
g1 <- as.ggplot(pheatmap(mat))
g2 <- as.ggplot(pheatmap(mat,cluster_cols = F))
# combine 
g1+g2
pheatmap patchwork • 1.8k views
ADD COMMENT
1
Entering edit mode
4.0 years ago
prabin.dm ▴ 260

I think you can use CompleHeatmap package

library(ComplexHeatmap)
mat = data.frame(matrix(rnorm(100), nrow=20))
a <- pheatmap(as.matrix(mat))
b <- pheatmap(as.matrix(mat), cluster_cols = F)
a + b

This aligns very well.

ADD COMMENT
0
Entering edit mode

Indeed. ComplexHeatmap renders all other heatmap packages useless by providing any features one might want. pheatmap especially is pretty obsolete, as CH was built to address its shortcomings - as the developer puts it:

The ComplexHeatmap package is inspired from the pheatmap package. You can find many arguments in ComplexHeatmap have the same names as in pheatmap. Also you can find this old package that I tried to develop by modifying pheatmap.

ADD REPLY
0
Entering edit mode

FWIW pheatmaps can be readily aligned with cowplot. But yes, in general it makes sense to move onto ComplexHeatmap. Historically it wasn't the fastest which made me stick to pheatmap for a while. But it has since overtaken it in most regards.

ADD REPLY
0
Entering edit mode

Thank you - I was unaware of the speed difference. I loved the features and how intuitive it was. Has it gotten better than pheatmap in terms of speed as well?

ADD REPLY
1
Entering edit mode

I mean I'm talking like years ago back when I was doing my PhD. We use ComplexHeatmap a lot for plotting single-cell and CNA data so the speed is definitely much better now.

You have to factor in that before I started my degree I never touched R so there might have been an element of competence at play also.

ADD REPLY
1
Entering edit mode
7 weeks ago
Yun ▴ 50

Another option: ggalign package

The usage of ggalign is simple if you're familiar with ggplot2 syntax, the typical workflow includes:

  • Initialize the layout using ggheatmap() or ggstack().
  • Customize the layout with:
    • align_group(): Group layout axis into panel with a group variable.
    • align_kmeans(): Group layout axis into panel by kmeans.
    • align_reorder(): Reorder layout observations based on statistical weights or by manually specifying the observation index.
    • align_dendro(): Reorder or Group layout based on hierarchical clustering.
  • Adding plots with ggalign() or ggpanel(), and then layer additional ggplot2 elements such as geoms, stats, or scales.

Here is the solution for this issue:

library(ggalign)
#> Loading required package: ggplot2
mat <- data.frame(matrix(rnorm(100), nrow = 20))
ggstack(mat, "h", sizes = unit(c(2, 1, 1), c("cm", "null", "null"))) +
    ggheatmap() +
    hmanno("t", free_spaces = "l") +
    align_dendro(aes(color = branch), k = 3L) +
    scale_color_brewer(palette = "Set2") +
    hmanno("l", size = unit(2, "cm")) +
    align_dendro(aes(color = branch), k = 4L) +
    scale_color_brewer(palette = "Set1") +
    ggheatmap() +
    hmanno("t") +
    align_dendro(aes(color = branch), k = 3L) +
    scale_color_brewer(palette = "Set2") +
    hmanno("r", size = unit(2, "cm")) +
    align_dendro(aes(color = branch), k = 4L) +
    scale_color_brewer(palette = "Set1")

enter image description here

Created on 2024-10-09 with [reprex v2.1.0](https://reprex.tidyverse.org) ~

ADD COMMENT

Login before adding your answer.

Traffic: 2258 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6