怎样在heatmap中使用多种cluster方法

来源:Forever_YCC评论5,542

生物信息学中经常使用R 来画图,而R画heatmap的功能是非常强大的。通常,我的习惯是使用gplots包中的heatmap.2函数来进行画图。不过这个函数中不能对聚类分析(clustering)到方法进行调整,于是,小小写一段代码即能使用不同的聚类分析方法来对heatmap进行聚类整合。

# There are 7 methods to make cluster in the function hclust in R
Cluster_Method<-c( "ward", "single", "complete", "average", "mcquitty", "median", "centroid")

for (i in 1:length(Cluster_Method)){
  #make a function to extract the cluster method
  myclust<-function(x){
    hclust(x,method=Cluster_Method[i])
  }
  #make heatmap by jpeg
  jpeg(filename=paste(Cluster_Method[i],'.jpg'),width=1024,height=728)
  heatmap.2(as.matrix(Data_Top1k_Var),
            trace='none',
            hclustfun=myclust,labRow=NA,
            ColSideColors=c('black',grey(0.4),'lightgrey')[as.factor(CellLine_Anno$Type)],
            xlab='CellLines',
            ylab='Probes',
            main=Cluster_Method[i],
            col=greenred(64))
  dev.off()
}

这样就可以一下子把七种cluster的方法依次用到heatmap上面来。而且通过对cluster树的比较,我们可以从中挑选出最好、最稳定到cluster方法,为后续分析打好基础!

发表评论

匿名网友