我有以下MWE,我在其中制作热图而不执行任何聚类并显示任何树形图。我想把我的行(基因)按类别分组,比现在更好看。
这是MWE:
#MWE
library(gplots)
mymat <- matrix(rexp(600, rate=.1), ncol=12)
colnames(mymat) <- c(rep("treatment_1", 3), rep("treatment_2", 3), rep("treatment_3", 3), rep("treatment_4", 3))
rownames(mymat) <- paste("gene", 1:dim(mymat)[1], sep="_")
rownames(mymat) <- paste(rownames(mymat), c(rep("CATEGORY_1", 10), rep("CATEGORY_2", 10), rep("CATEGORY_3", 10), rep("CATEGORY_4", 10), rep("CATEGORY_5", 10)), sep=" --- ")
mymat #50x12 MATRIX. 50 GENES IN 5 CATEGORIES, ACROSS 4 TREATMENTS WITH 3 REPLICATES EACH
png(filename="TEST.png", height=800, width=600)
print(
heatmap.2(mymat, col=greenred(75),
trace="none",
keysize=1,
margins=c(8,14),
scale="row",
dendrogram="none",
Colv = FALSE,
Rowv = FALSE,
cexRow=0.5 + 1/log10(dim(mymat)[1]),
cexCol=1.25,
main="Genes grouped by categories")
)
dev.off()
产生这个:
我想将行中的CATEGORIES组合在一起(如果可能的话,还要将列中的处理组合在一起),所以它看起来如下所示:
或者甚至更好,左边的CATEGORIES,与执行聚类和显示树状图的方式相同;然而更容易和更清楚...
有什么办法吗?谢谢!
修改!!
我在评论中了解了RowSideColors,我在下面制作了MWE。但是,我似乎无法在输出png中打印图例,加上图例中的颜色不正确,我也无法获得正确的位置。所以请帮助我下面的MWE中的传奇。
另一方面,我使用了由12种颜色组成的调色板“Set3”,但如果我需要超过12种颜色(如果我有超过12种颜色)怎么办?
新MWE
library(gplots)
library(RColorBrewer)
col1 <- brewer.pal(12, "Set3")
mymat <- matrix(rexp(600, rate=.1), ncol=12)
colnames(mymat) <- c(rep("treatment_1", 3), rep("treatment_2", 3), rep("treatment_3", 3), rep("treatment_4", 3))
rownames(mymat) <- paste("gene", 1:dim(mymat)[1], sep="_")
mymat
mydf <- data.frame(gene=paste("gene", 1:dim(mymat)[1], sep="_"), category=c(rep("CATEGORY_1", 10), rep("CATEGORY_2", 10), rep("CATEGORY_3", 10), rep("CATEGORY_4", 10), rep("CATEGORY_5", 10)))
mydf
png(filename="TEST.png", height=800, width=600)
print(
heatmap.2(mymat, col=greenred(75),
trace="none",
keysize=1,
margins=c(8,6),
scale="row",
dendrogram="none",
Colv = FALSE,
Rowv = FALSE,
cexRow=0.5 + 1/log10(dim(mymat)[1]),
cexCol=1.25,
main="Genes grouped by categories",
RowSideColors=col1[as.numeric(mydf$category)]
)
#THE LEGEND DOESN'T WORK INSIDE print(), AND THE POSITION AND COLORS ARE WRONG
#legend("topright",
# legend = unique(mydf$category),
# col = col1[as.numeric(mydf$category)],
# lty= 1,
# lwd = 5,
# cex=.7
# )
)
dev.off()
产生:
请帮我解说这个传说,对于假设的情况,我需要超过12种颜色。谢谢!
答案 0 :(得分:2)
我会使用pheatmap包。你的例子看起来像那样:
library(pheatmap)
library(RColorBrewer)
# Generte data (modified the mydf slightly)
col1 <- brewer.pal(12, "Set3")
mymat <- matrix(rexp(600, rate=.1), ncol=12)
colnames(mymat) <- c(rep("treatment_1", 3), rep("treatment_2", 3), rep("treatment_3", 3), rep("treatment_4", 3))
rownames(mymat) <- paste("gene", 1:dim(mymat)[1], sep="_")
mydf <- data.frame(row.names = paste("gene", 1:dim(mymat)[1], sep="_"), category = c(rep("CATEGORY_1", 10), rep("CATEGORY_2", 10), rep("CATEGORY_3", 10), rep("CATEGORY_4", 10), rep("CATEGORY_5", 10)))
# add row annotations
pheatmap(mymat, cluster_cols = F, cluster_rows = F, annotation_row = mydf)
# Add gaps
pheatmap(mymat, cluster_cols = F, cluster_rows = F, annotation_row = mydf, gaps_row = c(10, 20, 30, 40))
# Save to file with dimensions that keep both row and column names readable
pheatmap(mymat, cluster_cols = F, cluster_rows = F, annotation_row = mydf, gaps_row = c(10, 20, 30, 40), cellheight = 10, cellwidth = 20, file = "TEST.png")