gpt4 book ai didi

r - 如何仅对 R heatmap.2 中的列进行聚类?

转载 作者:行者123 更新时间:2023-12-04 07:53:58 28 4
gpt4 key购买 nike

我有一个数据,我想用树状图聚类为列绘制热图。
我怎样才能做到这一点?

数据只有一行多列。
请注意,我确实想要列上的集群,而不是将其转换为行集群。

这是我的代码,它不起作用。

library(gplots)
library(RColorBrewer)
dat.all <- structure(list(Probes = structure(1L, .Label = "1419598_at", class = "factor"),
XXX_LV_06.ip = 0.985, XXX_SP_06.ip = 0.932, XXX_LN_06.id = 2.115,
XXX_LV_06.id = 1.753, XXX_SP_06.id = 2.668, ZZZ_KD_06.ip = 10.079,
ZZZ_LG_06.ip = 2.323, ZZZ_LV_06.ip = 2.119, ZZZ_SP_06.ip = 4.157,
ZZZ_LN_06.id = 1.371, ZZZ_LV_06.id = 1.825, ZZZ_SP_06.id = 1.457,
ZZZ_KD_24.ip = 0L, ZZZ_LG_24.ip = 1.049, ZZZ_LV_24.ip = 1.372,
ZZZ_SP_24.ip = 1.83, AAA_LN_06.id = 1.991, AAA_LV_06.ip = 2.555,
AAA_SP_06.ip = 4.209, AAA_LV_06.id = 1.375, AAA_SP_06.id = 0.75,
GGG_LV_06.ip = 5.938, GGG_SP_06.ip = 8.326, GGG_LN_06.id = 1.982,
GGG_LV_06.id = 0.779, GGG_SP_06.id = 1.383, KKK_LN_06.id = 2.006,
KKK_LV_06.ip = 1.253, KKK_SP_06.ip = 1.774, X333_LV_06.id = 1.792,
X333_SP_06.id = 1.408, EEE_LV_06.in = 0.881, EEE_SP_06.in = 1.374,
DDD_LN_06.id = 2.052, DDD_LV_06.id = 1.363, DDD_SP_06.id = 1.678), .Names = c("Probes",
"XXX_LV_06.ip", "XXX_SP_06.ip", "XXX_LN_06.id", "XXX_LV_06.id",
"XXX_SP_06.id", "ZZZ_KD_06.ip", "ZZZ_LG_06.ip", "ZZZ_LV_06.ip",
"ZZZ_SP_06.ip", "ZZZ_LN_06.id", "ZZZ_LV_06.id", "ZZZ_SP_06.id",
"ZZZ_KD_24.ip", "ZZZ_LG_24.ip", "ZZZ_LV_24.ip", "ZZZ_SP_24.ip",
"AAA_LN_06.id", "AAA_LV_06.ip", "AAA_SP_06.ip", "AAA_LV_06.id",
"AAA_SP_06.id", "GGG_LV_06.ip", "GGG_SP_06.ip", "GGG_LN_06.id",
"GGG_LV_06.id", "GGG_SP_06.id", "KKK_LN_06.id", "KKK_LV_06.ip",
"KKK_SP_06.ip", "X333_LV_06.id", "X333_SP_06.id", "EEE_LV_06.in",
"EEE_SP_06.in", "DDD_LN_06.id", "DDD_LV_06.id", "DDD_SP_06.id"
), row.names = 1L, class = "data.frame")



# Clustering and distance function
hclustfunc <- function(x) hclust(x, method="complete")
distfunc <- function(x) dist(x,method="maximum")


height <- 3;

outdir <- "./";

# Define output file name
heatout <-paste(outdir,base,"myplot.pdf",sep="");

# require(RColorBrewer)
col1 <- colorRampPalette(brewer.pal(12, "Set3"));
col2 <- colorRampPalette(brewer.pal(9, "Set1"));


cl.col <- hclustfunc(distfunc(t(dat.all)))


# extract cluster assignments; i.e. k=8 (rows) k=5 (columns)
gr.col <- cutree(cl.col, h=3)
gr.col.nofclust <- length(unique(as.vector(gr.col)));
clust.col.height <- col2(gr.col.nofclust);
hmcols <- rev(redgreen(2750));

pdf(file=heatout,width=50,height=25);
heatmap.2(as.matrix(dat.all),
scale='row',
trace='none',
Rowv=FALSE,
col=hmcols,
symbreak=T,
hclustfun=hclustfunc,
distfun=distfunc,
keysize=0.1,
margins=c(10,200),
lwid=c(1,4), lhei=c(0.7,3),
ColSideColors=clust.col.height[gr.col])
dev.off();

图像将如下所示:
enter image description here

最佳答案

您是否明确需要使用 heatmap.2()功能?如果没有,那么我建议你考虑函数pheatmap()来自包 pheatmap,因为它允许您以相当少的体操完成您所追求的壮举。

首先,我会去掉数据集中的第一列。但是,为了保留信息,我将 Affymetrix ID 作为行名称放在数据框中:

rownames(dat.all)<-dat.all[,1]
dat.all<-dat.all[,-1]

之后,您可以运行其余代码,直到实际绘制热图。在那个阶段你诉诸 pheatmap() .它的工作原理与 heatmap.2() 非常相似。 ,但参数的名称不同。以下命令应该可以让您完成剩下的工作或接近它:
require(pheatmap)
pheatmap(dat.all, cluster_rows=FALSE, color=hmcols, scale="row",
annotation.colors=clust.col.height[gr.col], annotation=t(dat.all),
clustering_distance_cols=distfunc(t(dat.all)))

名称中带有注释的参数会添加列边颜色。如果您想使用自己的距离函数,可以将其输出指定为 pheatmap() 的输入。使用参数 clustering_distance_cols。请查阅 pheatmap 包的帮助以获取更多详细信息。另外,请参见下面的示例图。

pheatmap plot

关于r - 如何仅对 R heatmap.2 中的列进行聚类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22523386/

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