Echo94
Echo94

Reputation: 13

How do you combine multiple complexheatmap plots that are of different sizes? I have about 20 different heatmaps that need to go on same plot

I have tried

h1 <- grid.grabExpr(draw(Heatmap(as.matrix(df_1[2:1692]), col = rev(rainbow(10)), name = "C1", cluster_columns = TRUE, cluster_rows = FALSE, show_column_names = FALSE, show_column_dend = FALSE, row_title = "G", column_title = "C")))

h2 <- grid.grabExpr(draw(Heatmap(as.matrix(df_2[2:2269]), col = rev(rainbow(10)), name = "C2", cluster_columns = TRUE, cluster_rows = FALSE, show_column_names = FALSE, show_column_dend = FALSE, row_title = "G", column_title = "C", use_raster = FALSE)))

h3 <- draw(Heatmap(as.matrix(df_3[2:1384]), col = rev(rainbow(10)), name = "C3", cluster_columns = TRUE, cluster_rows = FALSE, show_column_names = FALSE, show_column_dend = FALSE, row_title = "G", column_title = "C"))

plot_grid(h1,  plot_grid(h2, h3))

But the error I get here is : Error in UseMethod("depth") : no applicable method for 'depth' applied to an object of class "NULL"

There's no direct way of combine ComplexHeatmap plots together if they are different sizes, I am guessing? Whats the best of making a plot together in a pdf?

Upvotes: 1

Views: 1954

Answers (1)

simonmo
simonmo

Reputation: 31

I tried your script with some random data, and it seems to work fine for me. I think as long as you also extract grob in h3 it works

library(cowplot)
library(ComplexHeatmap)
df_1 <- rnorm(3000)
df_2 <- rnorm(3000)
df_3 <- rnorm(3000)
h1 <- grid.grabExpr(draw(Heatmap(as.matrix(df_1[2:1692]), col = rev(rainbow(10)), name = "C1", cluster_columns = TRUE, cluster_rows = FALSE, show_column_names = FALSE, show_column_dend = FALSE, row_title = "G", column_title = "C")))

h2 <- grid.grabExpr(draw(Heatmap(as.matrix(df_2[2:2269]), col = rev(rainbow(10)), name = "C2", cluster_columns = TRUE, cluster_rows = FALSE, show_column_names = FALSE, show_column_dend = FALSE, row_title = "G", column_title = "C", use_raster = FALSE)))

h3 <- draw(Heatmap(as.matrix(df_3[2:1384]), col = rev(rainbow(10)), name = "C3", cluster_columns = TRUE, cluster_rows = FALSE, show_column_names = FALSE, show_column_dend = FALSE, row_title = "G", column_title = "C")) 
h3 <- grid.grabExpr(h3)

pdf('test.pdf', width = 30, height = 30)
plot_grid(h1,  plot_grid(h2, h3))
dev.off()

I personally like the patchwork package to compose my plots. Here's an example of combining multiple ComplexHeatmap of different sizes

library(tidyverse)
library(patchwork)
library(ComplexHeatmap)
# make list of 20 matrices with random size
mtx_list <- map(1:20, function(x){
    mat <- matrix(rnorm(1000), nrow = sample(1:100, 1))
    return(mat)
}) 

# check mtx size
map(mtx_list, dim)

# make heatmap and extract grob
hm_list <- map(mtx_list, function(mat){
    Heatmap(mat) %>% 
        draw() %>% 
        grid.grabExpr()
})

# Plot all with patchwork wrap_plots
pdf('test.pdf', width = 15, height = 15)
wrap_plots(hm_list, ncol = 4)
dev.off()

Patchwork also allow assign widths, height, and do some more complex plot assembly, so it's worth checking out their tutorials.

Upvotes: 2

Related Questions