I create a heatmap, I would like to add color on the row with a legend
I got this :
I would like this a legend with color from only row data :
My code is :
library(pheatmap)
test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")
# Draw heatmaps
pheatmap(test)
The native supported way to annotate in
pheatmap
is usingannotation_row
, as below, which will add an annotation track to the left of the heatmap:However, if you want to have it the way you're proposing, you can do that using
grid
andgridExtra
, which is a pretty manual process: make the heatmap, modify thegtable
object directly, then make the legend, then add the legend to the table.library(grid) library(gridExtra)