Conditional coloring of heatmap
1
1
Entering edit mode
3 months ago
ilomilo ▴ 10

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)
genomics heatmap ComplexHeatmap single-cell • 434 views
ADD COMMENT
3
Entering edit mode
3 months ago
Ram 43k

Use a matrix that has a binary "black" or "white" based on your condition and supply that matrix to the Heatmap function. That might be easier than having a legend figure out coloring logic on the fly.

mat <- matrix(rnorm(25), nrow=5)
mat
        [,1]    [,2]    [,3]    [,4]    [,5]
[1,]  0.4553  0.5507  0.2856 -0.8464  1.7926
[2,]  0.4550 -1.0676  0.1855 -1.5958  1.9490
[3,] -0.3369  0.4353 -0.7619  1.8817 -1.1267
[4,]  0.2370 -1.5448 -0.3632  1.2400 -1.1667
[5,] -1.9567  1.2798  0.6549 -0.3256  0.7296

ifelse(mat >= 1,"black","white")
     [,1]    [,2]    [,3]    [,4]    [,5]
[1,] "white" "white" "white" "white" "black"
[2,] "white" "white" "white" "white" "black"
[3,] "white" "white" "white" "black" "white"
[4,] "white" "white" "white" "black" "white"
[5,] "white" "black" "white" "white" "white"
ADD COMMENT
0
Entering edit mode

That was clever haha. Thank you so much.

ADD REPLY
0
Entering edit mode

A small educational note: if an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one answer if they work. This will help future users that might find this post find the right answer.

upvote_bookmark_accept

ADD REPLY

Login before adding your answer.

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