R ComplexHeatmap - Adujusting the cell size in heatmap
1
0
Entering edit mode
4 months ago
TC_Chang ▴ 10

Hi,

I aim to narrow the width of each cell and minimize the space within them. Although I attempted to do this by modifying 'width = width/2' in cell_fun1, this reduced the width but the color block did not properly fit to the cell. Any advice or solutions would be highly appreciated.

cell_fun1 = function(j, i, x, y, width, height, fill) {
  grid.rect(x = x, y = y, width = width, height = height,
            gp = gpar(col = "gray", fill = fill))
  if(mut_mat[i, j] != 0) {
    if(mut_mat[i, j] > 9) {
      grid.text(mut_mat[i, j], x, y, gp = gpar(col = "white"))
    } else {
      grid.text(mut_mat[i, j], x, y, gp = gpar(col = "black"))
    }
  }
}

col_fun1 = colorRamp2(c(0, 5, 10, 20, 40), c("white","#bae4b3","#74c476", "#31a354", "#006d2c"))


hm1 <- Heatmap(mut_mat, 
               name = "#_of_LOE_mutations",
               cluster_rows = FALSE, 
               cluster_columns = FALSE, 
               show_row_names = TRUE,
               show_column_names = TRUE,
               column_names_side = "top",
               row_names_side = "left",
               column_names_gp = gpar(fontsize=13,fontface="bold"),
               row_names_gp = gpar(fontsize=10,fontface="bold"),
               cell_fun = cell_fun1,
               col = col_fun1,
               show_heatmap_legend = FALSE,
               width = heatmap_width,
               height = heatmap_height
               )
hm1

Original heatmap:

enter image description here

After modifying "width = width" to "width = width/2" in cell_fun1

enter image description here

R complexheatmap • 512 views
ADD COMMENT
1
Entering edit mode

I may be missing something, but can you not just shrink the width of the heatmap in general? That is how one would typically do this.

ADD REPLY
0
Entering edit mode

How are you setting heatmap_width and heatmap_height? That's what you'll need to tweak.

ADD REPLY
0
Entering edit mode
4 months ago
jv ★ 1.8k

I think the main thing is not to use "width = width/2" in cell_fun1. Using ComplexHeatmap 2.14.0 on R 4.2.1 i was able to modify cell width and height via width and height params in call to Heatmap. I tried a few different widths and heights and each time the cell borders adjusted accordingly.

cell_fun1 = function(j, i, x, y, width, height, fill) {
  grid.rect(x = x, y = y, width = width, height = height,
            gp = gpar(col = "gray", fill = fill))
  if(mat[i, j] != 0) {
    if(mat[i, j] > 9) {
      grid.text(mat[i, j], x, y, gp = gpar(col = "white"))
    } else {
      grid.text(mat[i, j], x, y, gp = gpar(col = "black"))
    }
  }
}

mat <- matrix(c(0,25,0,1,12,2,9,1,4,3,1,12,3,0,1), byrow = TRUE, nrow = 3)
rownames(mat) <- c("KRAS", "ARID1A", "PIK3CA")

library(ComplexHeatmap)
col_fun1 = circlize::colorRamp2 (c(0, 5, 10, 20, 40), 
                                 c("white","#bae4b3","#74c476", "#31a354", "#006d2c"))

hm1 <- Heatmap(mat, 
               name = "#_of_LOE_mutations",
               cluster_rows = FALSE, 
               cluster_columns = FALSE, 
               show_row_names = TRUE,
               show_column_names = TRUE,
               column_names_side = "top",
               row_names_side = "left",
               column_names_gp = gpar(fontsize=13,fontface="bold"),
               row_names_gp = gpar(fontsize=10,fontface="bold"),
               cell_fun = cell_fun1,
               col = col_fun1,
               show_heatmap_legend = FALSE,
               width = unit(5, "cm"),
               height = unit(2, "cm")
)
hm1

enter image description here

The only difference in my code and yours is that I explicitly set width and height using unit since you didn't show how you defined heatmap_width and heatmap_height in your example.

ADD COMMENT

Login before adding your answer.

Traffic: 1653 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