Customize fold change legend values in ComplexHeatmap
0
1
Entering edit mode
4.1 years ago

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

ComplexHeatmap circlize colorRamp2 R matrix • 2.6k views
ADD COMMENT
1
Entering edit mode

You are overwriting the custom values you set on top of the script with col_fun(my_matrix[i, j]) are you aware of that?

ADD REPLY
0
Entering edit mode

Hi @ATpoint, thank you for your prompt reply.

I tried running this code, however,

Error in Heatmap(my_matrix, name = "Fold Change Values", col_fun = circlize::colorRamp2(c(-2,  : 
  unused argument (col_fun = circlize::colorRamp2(c(-2, 0, 2), c("blue", "white", "red")))
draw(ht,heatmap_legend_side = "left", annotation_legend_side = "right", padding = unit(c(2, 20, 2, 2), "mm"))
Error in col_fun(my_matrix[i, j]) : could not find function "col_fun"
Called from: numnotnull("fontsize")
Browse[1]> 
dev.off()
pdf 
  2 

pdf("Heatmap_v1.pdf",height = 5, width = 7)
ht=Heatmap(my_matrix,
           name = "Fold Change Values", 
           col_fun = circlize::colorRamp2(c(-2,0,2), c("blue", "white", "red")),
           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()
ADD REPLY
2
Entering edit mode

It's col = circlize::colorRamp2(c(-2,0,2), c("blue", "white", "red")) not col_fun. Use ?Heatmap to check parameters correct name.

ADD REPLY
0
Entering edit mode

I wanted to input (-3,0,6), now I can see in the legend. Thank you so much.

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", 
           col = circlize::colorRamp2(c(-3,0,6), c("blue", "white", "red")),
           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()
ADD REPLY

Login before adding your answer.

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