Heatmap Annotation in R
1
0
Entering edit mode
15 months ago
hellokwmin • 0

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.3k views
ADD COMMENT
0
Entering edit mode

I think using ComplexHeatmap with annotations could accomplish this well.

ADD REPLY
0
Entering edit mode

It looks like they're already using ComplexHeatmap. They'd probably benefit from this section of the manual though: https://jokergoo.github.io/ComplexHeatmap-reference/book/heatmap-annotations.html#zoom-annotation

ADD REPLY
1
Entering edit mode
15 months ago
bk11 ★ 3.0k

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

ADD COMMENT
1
Entering edit mode

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)
ADD REPLY
1
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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"))
ADD REPLY

Login before adding your answer.

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