我有一个数据,我想仅针对该列绘制具有树状图聚类的热图。
我该如何实现?
数据仅包含一行,但包含多列。
请注意,我确实希望在列上使用群集,而不要将其转换为行群集。
这是我的代码,没有用。
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();
该图像将如下所示:
最佳答案
您是否明确需要使用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)))
名称中带有注释的自变量会添加列的侧面颜色。如果要使用自己的距离函数,可以使用参数clustering_distance_cols将其输出指定为
pheatmap()
的输入。请咨询有关Pheatmap软件包的帮助以获取更多详细信息。另外,请参见下面的示例图。