Skip to content

Instantly share code, notes, and snippets.

@yuifu
Last active May 20, 2021 14:45
Show Gist options
  • Save yuifu/81fa3f08262c6ae60a2e647505d57da7 to your computer and use it in GitHub Desktop.
Save yuifu/81fa3f08262c6ae60a2e647505d57da7 to your computer and use it in GitHub Desktop.
Align multiple heatmaps generated by ComplexHeatmap using pushViewport()
library(ComplexHeatmap)
# test data
list_ht <- list()
for(i in 1:8){
mat1 = matrix(rnorm(80, 2), 8, 10)
mat1 = rbind(mat1, matrix(rnorm(40, -2), 4, 10))
rownames(mat1) = paste0("R", 1:12)
colnames(mat1) = paste0("C", 1:10)
ht = Heatmap(mat1, name = "rnorm",
column_title = i)
list_ht <- append(list_ht, list(ht))
}
# Determine the numbers of rows and columns
n_plot <- length(list_ht)
n_row <- ceiling(sqrt(n_plot))
n_col <- ceiling(n_plot/n_row)
# Generate n_row x n_col plot subspace
grid.newpage()
pushViewport(viewport(layout = grid.layout(nr = n_row, nc = n_col)))
for(i in 1:n_plot){
# Determine the plot subspace
current_row <- ceiling(i / n_col)
current_col <- i - (current_row-1) * n_col
# Plot heatmap in the subspace
pushViewport(viewport(layout.pos.row = current_row, layout.pos.col = current_col))
draw(list_ht[[i]], newpage = FALSE)
upViewport()
}
upViewport()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment