如何只聚集R heatmap.2中的列?

时间:2014-03-20 04:16:15

标签: r plot heatmap

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

数据只包含一行但包含多列。 请注意,我确实希望列上的集群而不是将其转换为行集群。

这是我的代码,但没有用。

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

2 个答案:

答案 0 :(得分:5)

您明确需要使用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包的帮助。另外,请参阅下面的示例图。

pheatmap plot

答案 1 :(得分:1)

要克服'每个维度必须是两个或更多'约束,你可以rbind单行到它自己,

heatmap.2(rbind(as.numeric(dat.all[,-1]),as.numeric(dat.all[,-1])),...

虽然您可能需要手动调整标签。我把第一列从dat.all上取下来(使用[,-1],因为当我复制它时,affymetrix id会妨碍你 - 你可能不需要在真正的版本中这样做吗?