R 在pheatmap中的行上添加带有图例的颜色

R 在pheatmap中的行上添加带有图例的颜色,r,pheatmap,R,Pheatmap,我创建了一个热图,我想在带有图例的行上添加颜色 我明白了: 我希望这是一个图例,颜色仅来自行数据: 我的代码是: 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

我创建了一个热图,我想在带有图例的行上添加颜色

我明白了:

我希望这是一个图例,颜色仅来自行数据:

我的代码是:

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)

pheatmap
中,本机支持的注释方式是使用
annotation\u row
,如下所示,这将在热图左侧添加注释轨迹:

gene_annot <- data.frame(Experiment = rep(c("Exp1", "Exp2"), each = 10), 
                         row.names = rownames(test))
pheatmap(test, annotation_row = gene_annot)

谢谢您的回答,但我正在寻找一种非手工方式。我想要自动方式。与ggplot2上的函数颜色相同的方法。您可以使用函数使网格自动填充。或者试试复杂的热图。尽管说实话,我认为注释轨迹比给基因名上色更好
hm <- pheatmap(test, silent = TRUE)
hm$gtable$grobs[[5]]$gp$col <- rep(c("black", "red"), each = 10)
leg <- legendGrob(c("Exp1", "Exp2"), nrow = 2, pch = 15, gp = gpar(fontsize = 10, col = c("black", "red")))
hm2 <- arrangeGrob(hm$gtable, leg, ncol = 2, widths = c(5,1))
grid.draw(hm2)