This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Heatmap Annotation in R

Hello, Stars.

I would like to annotate text in my heatmap, but not easy with my trials...

First my data frame is like this: which is saved as inte33.csv enter image description here

This is the R code I used for generation of heatmap:

gene_expression_df = read.csv("inte33.csv")
gene_matrix <- as.matrix(gene_expression_df[, c("F7", "F7R")])
color_fun <- colorRamp2(c(-4, 0, 4), c("red", "black", "green"))
Heatmap(gene_matrix,
        col = color_fun,
        show_row_names = F,
        show_column_names = T,
        show_row_dend = F,
        show_column_dend = F,
        row_names_side = "left",
        width = unit(4, "cm"))

After this code, what I get is:

enter image description here

And what I want to do in this heatmap is that I would like to add some text. example is like this:

enter image description here

So, I would like to add text from 1: 10 rows, 11: 30, to 30: 60 and so on....

Please, help me.

Thank you.

heatmap annotation

1 answer

You can do something like this-

set.seed(123)
library(ComplexHeatmap)
library(colorRamp2)

#Creating random gene expression matrix based on dimensions of your data
gene_expression_df = matrix(rnorm(100), 40, ncol = 2) 
rownames(gene_expression_df) = paste0("R", 1:40)
colnames(gene_expression_df) = paste0("C", 1:2)

head(gene_expression_df)
C1         C2
R1 -0.56047565 -0.6947070
R2 -0.23017749 -0.2079173
R3  1.55870831 -1.2653964
R4  0.07050839  2.1689560
R5  0.12928774  1.2079620
R6  1.71506499 -1.1231086

color_fun <- colorRamp2(c(-4, 0, 4), c("red", "black", "green"))

##Here Split your heatmap according to your size of block   
split = c(rep("A:1-10", 10), rep("B:11-20", 10), rep("C:21-30", 10), rep("D:31-40",10))

Heatmap(gene_expression_df, row_split = split, cluster_rows = FALSE,cluster_columns = FALSE, 
    row_title = NULL, show_row_names = FALSE, col=color_fun,
    right_annotation = rowAnnotation(foo = anno_block(labels = c("A:1-10", "B:11-20", "C:21-30", "D:31-40"))))

enter image description here

It doesn't look like OP wants to split their heatmap - zoom-annotations annotate rows without having to split the heatmap itself. Also, there is a much better way to write your split vector:

split <- rep(c("A:1-10", "B:11-20", "C:21-30", "D:31-40"), each = 10)

Without splitting the data, it could be done sth like this then-

set.seed(123)
library(ComplexHeatmap)
library(colorRamp2)
gene_expression_df = matrix(rnorm(100), 40, ncol = 2)
rownames(gene_expression_df) = paste0("R", 1:40)
colnames(gene_expression_df) = paste0("C", 1:2)

color_fun <- colorRamp2(c(-4, 0, 4), c("red", "black", "green"))
align_to = list("A:1-10" = paste("A:1-10"), "B:11-20" = paste("B:11-20"), "C:21-30"=paste("C:21-30"), "D:31-40"= paste("D:31-40"))
Heatmap(gene_expression_df, name="mat", cluster_rows = F, cluster_columns = F, show_row_names = F, col=color_fun,
    right_annotation = rowAnnotation(
      textbox=anno_textbox(
        list("A:1-10" = 1:10, "B:11-20" = 11:20, "C:21-30" = 21:30, "D:31-40" = 31:40), 
        align_to[c("A:1-10","B:11-20", "C:21-30", "D:31-40")], by="anno_block"),width = unit(2, "cm")))

enter image description here

Thank you, sir. It really helpful. God Bless to you.

What is paste doing in this line:

align_to = list("A:1-10" = paste("A:1-10"), "B:11-20" = paste("B:11-20"), "C:21-30"=paste("C:21-30"), "D:31-40"= paste("D:31-40"))

Log in to answer this question.