# transform the matrix into a long vector
v = pindex((t(as.matrix(MAGS.class.module.reordered))), i, j)
# `j` here is also a vector with the same length of `v`
col = ifelse(functional.anno$header[j] == "C1", col_fun_C1(v),
ifelse(functional.anno$header[j] == "C1-methane", col_fun_C1_methane(v),
ifelse(functional.anno$header[j] == "Electron transport Chain", col_fun_ETC(v),
ifelse(functional.anno$header[j] == "Hydrogenases", col_fun_H(v),
ifelse(functional.anno$header[j] == "Metal Reduction", col_fun_MR(v),
ifelse(functional.anno$header[j] == "Nitrogen", col_fun_N(v),
ifelse(functional.anno$header[j] == "Oxygen", col_fun_O(v),
ifelse(functional.anno$header[j] == "Photosynthesis", col_fun_P(v),
col_fun_S(v)))))))))
grid.rect(x, y, w, h, gp = gpar(fill = col, col = col))
} , col = scalebr5, show_heatmap_legend = TRUE,
width = ncol(MAGS.class.module.reordered)*unit(8, "mm"), height = nrow(MAGS.class.module.reordered)*unit(8, "mm"))
I have managed to create a heatmap such that each slice has a different color using 'layer_fun" as follows:
col_fun_C1 = colorRamp2(c(0, 1606), c("white", "cadetblue")) col_fun_C1_methane = colorRamp2(c(0, 487), c("white", "indianred")) col_fun_ETC = colorRamp2(c(0, 2081), c("white", "purple")) col_fun_H = colorRamp2(c(0, 464), c("white", "coral")) col_fun_MR = colorRamp2(c(0, 47), c("white", "grey")) col_fun_N = colorRamp2(c(0, 156), c("white", "thistle")) col_fun_O = colorRamp2(c(0, 400), c("white", "skyblue")) col_fun_P = colorRamp2(c(0, 1152), c("white", "forestgreen")) col_fun_S = colorRamp2(c(0, 1152), c("white", "goldenrod2"))
MAGS.class.module.compheatmap.3 <- Heatmap(t(as.matrix(MAGS.class.module.reordered)), name = "class module-gene count",heatmap_legend_param = list(title = "class-Module-Gene count", title_gp = gpar(fontsize = 18, fontface = 'bold'), labels_gp = gpar(fontsize = 9),ncol = 3, legend_height = unit(7, "cm"),legend_width = unit(4, "cm"), legend_direction = "horizontal"), cluster_rows = FALSE, row_dend_side = "left",row_order = sort(rownames((t(as.matrix(MAGS.class.module.reordered))))), row_names_gp = gpar(fontsize = 15), column_names_gp = gpar(fontsize = 15) , cluster_columns = FALSE , column_split = data.frame(functional.anno$header), column_gap = unit(5, "mm"), border = TRUE, right_annotation = class.row.annotations.right , bottom_annotation = bottom.modules, row_names_side = "right", row_names_centered = FALSE, column_names_rot = 45, use_raster = TRUE, raster_by_magick = TRUE, layer_fun = function(j, i, x, y, w, h, fill) {
MAGS.class.module.compheatmap.3 <- draw(MAGS.class.module.compheatmap.3 , heatmap_legend_side="bottom", annotation_legend_side="right",legend_grouping = "original")
How do I generate legends for each of the slice as each has different colors with different value limits.