ComplexHeatmap icon indicating copy to clipboard operation
ComplexHeatmap copied to clipboard

Adujusting the cell size in heatmap

Open tingchiafelix opened this issue 1 year ago • 1 comments

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:

Screenshot 2023-12-14 at 10 00 59 AM

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

Screenshot 2023-12-14 at 10 02 54 AM

tingchiafelix avatar Dec 14 '23 15:12 tingchiafelix

I'm guessing if you modify the value passed to width in Heatmap instead of grid.rect, that will fix things; i.e., width = heatmap_width / 2. The problem is that the heatmap is drawing rectangles using width = heatmap_width and then your cell function is placing another smaller set of rectangles with a grey border on top of that. Also, you could omit the grid.rect part of the cell function and just specify border = "grey" in Heatmap and not have to re-draw the rectangles.

TylerSagendorf avatar Dec 21 '23 15:12 TylerSagendorf