This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to combine multiple pheatmap figures and use common legend?

I currently have four heatmaps generated by pheatmap(): hp1.hp2.hp3, and hp4. I know that these heat maps can be combined in one canvas using grid.arrange(). But how can I use a common legend for all heatmaps? This can be achieved using ggarrange() when setting common.legend = TRUE.However, since these heatmaps are not ggplot items, ggarrange() cannot be used. What should I do?

r pheatmap ggarrange gridarrange

Hi, I used the grad.arrange() function from gridExtra package. I just tried to mute the legends of each individual figure and added a common one for the whole plot, it worked well. Thank you

1 answer

Hi! You can use ggplotify and patchwork . Here a reproducible example. It doesn't collect the legends though..

# load libraries
library(ggplot2)
library(ggplotify)
library(pheatmap)
library(patchwork)

# create random dataframe
df = data.frame(matrix(rnorm(20), nrow=10))

# save plots
p1 <- as.ggplot(pheatmap(df))
p2 <- as.ggplot(pheatmap(df))

# use patchwork to arrange them together
p1 + p2

enter image description here

Thanks, converting pheatmap into a ggplot allows us to use ggarrange neatly.

Log in to answer this question.