jokergoo / ComplexHeatmap

Make Complex Heatmaps
https://jokergoo.github.io/ComplexHeatmap-reference/book/
Other
1.29k stars 225 forks source link

Conditional coloring of heatmap #1158

Open StarkTS98 opened 8 months ago

StarkTS98 commented 8 months ago

I recently started working with the ComplexHeatmap package, the Heatmap function. I want to create heatmap with a color scheme of black and white. I have a matrix of 1500 rows and 384 columns. I want to have the values >=1 to be black and the rest to be white. Any suggestion on how I can achieve this would be great. Here is the code I am using to create the heatmap, but I'm not sure if I understood the cell_fun parameter correctly because it gives me unused argument error

`visualize_heatmap <- function(combined_df, chromosome_to_view) {

Extract rows where column 1 is equal to the user input value

selected_rows <- combined_df[combined_df[, 1] == chromosome_to_view, ]

Select columns from 4 to 387

selected_columns <- selected_rows[, -(1:3)]

Apply log2(1+x) to each individual value

log_transformed_values <- log2(1 + selected_columns)

Calculate column sums and rearrange columns in descending order

sorted_columns <- log_transformed_values[, order(-colSums(log_transformed_values))]

Transpose the dataframe to create a matrix

result_matrix <- t(sorted_columns)

Define a function for conditional coloring

my_col_fun <- function(x) { ifelse(x >= 1, "black", "white") }

Create a barplot for the top annotation

ha1 = HeatmapAnnotation( SumCounts = anno_barplot( colSums(result_matrix), bar_width = 1, gp = gpar(fill = "black"), border = FALSE, height = unit(1, "cm") ), show_annotation_name = TRUE )

Create and display the heatmap with conditional coloring

Heatmap( result_matrix, top_annotation = ha1, cluster_rows = FALSE, cluster_columns = FALSE, show_row_names = FALSE, show_column_names = FALSE, show_heatmap_legend = TRUE, heatmap_legend_param = list( title = "Log2Counts", at = c(-2, 0, 2)), cell_fun = my_col_fun ) }

chromosome_to_view <- 1 visualize_heatmap(result_OE_206, chromosome_to_view)`