R heatmap.2行和列的手动分组 [英] R heatmap.2 manual grouping of rows and columns

查看:658
本文介绍了R heatmap.2行和列的手动分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下MWE,其中在不进行任何聚类和显示任何树状图的情况下制作热图.我想以一种比现在更好的方式将行(基因)归为一类.

I have the following MWE in which I make a heatmap without performing any clustering and showing any dendrogram. I want to group my rows (genes) together in categories, in a better looking way than how it is now.

这是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()

哪个产生这个:

我想将行中的类别归为一类(如果可能的话,还要对列进行分组),因此它看起来像以下内容:

I would like to group the CATEGORIES in the rows together (and, if possible, the treatments in the columns as well), so it looks something like the following:

或者,甚至更好的是,使用左侧的类别,与执行聚类和显示树状图的方式相同;但是更容易,更清楚...

Or, maybe even better, with the CATEGORIES on the left, the same way as when clustering is performed and dendrograms are shown; however is easier and clearer...

有什么办法吗?谢谢!

编辑!

我在注释中意识到了RowSideColors,并在下面进行了MWE.但是,我似乎无法在输出png中打印图例,而且图例中的颜色也不正确,而且我也无法正确定位位置.因此,请帮助我了解下面MWE中的图例.

另一方面,我使用调色板"Set3",它包含12种颜色,但是如果我需要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种以上的颜色.谢谢!

推荐答案

我会使用pheatmap包.您的示例如下所示:

I would use pheatmap package. Your example would look something like that:

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

这篇关于R heatmap.2行和列的手动分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆