Hi,

I have a question about customizing the fold change legend values applied on a data matrix in R. I am plotting the heatmap on my data matrix using the library(ComplexHeatmap) and library(circlize). However, when I try to input values in col_fun in the range of (-2,0,2) or even (-2,0,5). It always displays (-4,0,4) in the legend bar. Please assist me in customizing fold change legend values. Here is my code.

class(my_matrix)
[1] "matrix" "array" 

str(my_matrix)
 num [1:12, 1:10] 0.436 -0.143 -0.097 0 0.168 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:12] "Gene A" "Gene B" "Gene C" "Gene D" ..

col_fun = circlize::colorRamp2(c(-2,0,2), c("blue", "white", "red"))

pdf("Heatmap.pdf",height = 5, width = 7)
ht=Heatmap(my_matrix,
           name = "Fold Change Values", 
           na_col = "grey",
           cluster_rows = F,
           cluster_columns = F,
           #height = unit(4, "mm")*nrow(df_plot), 
           #width  = unit(4, "mm")*ncol(df_plot), 
           rect_gp = gpar(type = "none"),
           top_annotation = column_ha,
           #name = "% Response",
           row_names_max_width = unit(10,"in"),
           row_title_gp = gpar(fontsize = 0.1),
           column_names_gp = gpar(fontsize = 12),
           row_names_gp = gpar(fontsize = 13),
           cell_fun = function(j, i, x, y, width, height, fill) {
             grid.circle(x = x, y = y, r = unit(1.85, "mm") ,gp = gpar(fill = col_fun(my_matrix[i, j]), col = NA))
           }
)
draw(ht,heatmap_legend_side = "left", annotation_legend_side = "right", padding = unit(c(2, 20, 2, 2), "mm"))

dev.off()

Thank you,

Toufiq



Source link