gpt4 book ai didi

R heatmap.2 手动分组行和列

转载 作者:行者123 更新时间:2023-12-04 12:48:59 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How to create pre-annotated rowside column in heatmap.2

(2 个回答)


4年前关闭。




我有以下 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()

产生这个:

test1

我想将行中的 CATEGORIES 组合在一起(如果可能的话,也可以将列中的处理组合在一起),所以它看起来像下面这样:

TEST2

或者,也许更好,使用左侧的 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()

其中产生:

TEST3

请帮我看一下图例,假设情况下我需要 12 种以上的颜色。谢谢!

最佳答案

我会使用 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)

row annotations
# Add gaps
pheatmap(mymat, cluster_cols = F, cluster_rows = F, annotation_row = mydf, gaps_row = c(10, 20, 30, 40))

with gaps
# 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")

final picture

关于R heatmap.2 手动分组行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41608294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com