Adding extra labels (lines and text) in pheatmap R
1
0
Entering edit mode
9 months ago

Hello, everyone

I have an issue when I'm trying to draw some lines and text in the output of pheatmap.

For example, I have the next heatmap from the matrix:

a <- matrix(rnorm(100), 10, 10)
pheatmap(a,
show_rownames = F,
border_color = NA,
scale = "row",
clustering_distance_rows = "euclidean",
clustering_distance_cols = "euclidean",
clustering_method = "single",
show_colnames = F,
main = "Something \n")


And then I'm using the next code to add the text:

grid.text("Group A" , x= 0.3, y = 0.01)
grid.text("Group B" , x= 0.75, y = 0.01)


Which prints the text over the heatmap. I need the text to be printed below the plot and then, add some separate lines above the text. For adding the lines I employed grid.lines. However, it drew me a continuous line.

Best regards

RNA-Seq R pheatmap code • 1.4k views
0
Entering edit mode

Try playing with cell height and width and change the x and y coordinates of the text. Or you can read more about grid.graphics used by pheatmap.

2
Entering edit mode
9 months ago
fracarb8 ▴ 420

Instead of adding your text, you could let pheatmap deal with the annotation.

# create a df with the samples grouped in the same way you want to show
anno <- data.frame(SampleID = c("col1","col2","col3","col4"),
Condition=c("groupA","groupA","groupB","groupB))

# set rownames so that anno and your data can be matched
rownames(anno) <- myData\$SampleID

# set colours
anno_colori <- list(Condition = c(groupA = 'red', groupB = 'blue')

# call pheatmap
pheatmap(a,
annotation_col = anno, # or annotation_row
annotation_colors = anno_colori,  ....)


This will add a bar on top of the heatmap with different colour based on your Condition. I have never moved the bar, but you should be able to put it at the bottom of your fiure.

0
Entering edit mode

You are right! Pheatmap can deal with annotation in this nice way!

Last night I tried with:

pheatmap(a,
show_rownames = F,
border_color = NA,
scale = "row",
clustering_distance_rows = "euclidean",
clustering_distance_cols = "euclidean",
clustering_method = "single",
labels_col = c("\nA", "", "", "", "", "", "", "","", "\nB", "", "", "", "", ""),
angle_col = 0)


But your solution is a way better