1、一步一步学heatmap2函数一步一步学heatmap.2函数数据如下: library(gplots) data(mtcars) x - as.matrix(mtcars) rc - rainbow(nrow(x), start=0, end=.3) cc - rainbow(ncol(x), start=0, end=.3)X就是一个矩阵,里面是我们需要画热图的数据。Rc是一个调色板,有32个颜色,渐进的Cc也是一个调色板,有11个颜色,也是渐进的首先画一个默认的图:heatmap.2(x)然后可以把聚类数可以去掉:就是控制这个dendrogram参数heatmap.2(x, dendro
2、gram=none) 然后我们控制一下聚类树heatmap.2(x, dendrogram=row) 只显示行向量的聚类情况heatmap.2(x, dendrogram=col)只显示列向量的聚类情况下面还是在调控聚类树,但是我没看懂跟上面的参数有啥子区别! heatmap.2(x, keysize=2) # default - dendrogram plotted and reordering done. heatmap.2(x, Rowv=FALSE, dendrogram=both) # generate warning! heatmap.2(x, Rowv=NULL, dendrog
3、ram=both) # generate warning! heatmap.2(x, Colv=FALSE, dendrogram=both) # generate warning!接下来我们可以调控行列向量的label的字体大小方向首先我们调控列向量,也就是x轴的label heatmap.2(x, srtCol=NULL) heatmap.2(x, srtCol=0, adjCol = c(0.5,1) ) heatmap.2(x, srtCol=45, adjCol = c(1,1) ) heatmap.2(x, srtCol=135, adjCol = c(1,0) ) heatmap
4、.2(x, srtCol=180, adjCol = c(0.5,0) ) heatmap.2(x, srtCol=225, adjCol = c(0,0) ) # not very useful heatmap.2(x, srtCol=270, adjCol = c(0,0.5) ) heatmap.2(x, srtCol=315, adjCol = c(0,1) ) heatmap.2(x, srtCol=360, adjCol = c(0.5,1) )然后我们调控一下行向量,也就是y轴的label heatmap.2(x, srtRow=45, adjRow=c(0, 1) ) heat
5、map.2(x, srtRow=45, adjRow=c(0, 1), srtCol=45, adjCol=c(1,1) ) heatmap.2(x, srtRow=45, adjRow=c(0, 1), srtCol=270, adjCol=c(0,0.5) )设置 offsetRow/offsetCol 可以把label跟热图隔开! # Show effect of offsetRow/offsetCol (only works when srtRow/srtCol is # not also present) heatmap.2(x, offsetRow=0, offsetCol=0)
6、heatmap.2(x, offsetRow=1, offsetCol=1) heatmap.2(x, offsetRow=2, offsetCol=2) heatmap.2(x, offsetRow=-1, offsetCol=-1) heatmap.2(x, srtRow=0, srtCol=90, offsetRow=0, offsetCol=0) heatmap.2(x, srtRow=0, srtCol=90, offsetRow=1, offsetCol=1) heatmap.2(x, srtRow=0, srtCol=90, offsetRow=2, offsetCol=2) h
7、eatmap.2(x, srtRow=0, srtCol=90, offsetRow=-1, offsetCol=-1)# Show effect of z-score scaling within columns, blue-red color scale # hv names(hv)可以看到hv对象里面有很多子对象rowInd colInd call colMeans colSDs carpet rowDendrogram colDendrogram breaks col vline colorTable # Show the mapping of z-score values to co
8、lor bins hv$colorTable # Extract the range associated with white我们得到了热图的颜色的数值映射矩阵,接下来就可以进行一系列的操作! hv$colorTablehv$colorTable,color=#FFFFFF,首先得到了白色所对应的数值区间!然后还可以通过一下命令,直接求出属于白色区间的那些数值。whiteBin - unlist(hv$colorTablehv$colorTable,color=#FFFFFF,1:2) rbind(whiteBin1 * hv$colSDs + hv$colMeans, whiteBin2
9、* hv$colSDs + hv$colMeans )调整scale参数选择按照列还是行来进行数据的标准化heatmap.2(x, col=bluered, scale=column, tracecol=#303030)heatmap.2(x, col=bluered, scale=row, tracecol=#303030)如果选择了标准化,那么还可以手工调整标准化的参数:rowMeans, rowSDsmean and standard deviation of each row: only present if scale=rowcolMeans, colSDsmean and stan
10、dard deviation of each column: only present if scale=column通过hclustfun参数来调整聚类方法参考:Cluster_Method-c( ward, single, complete, average, mcquitty, median, centroid)#R语言里面自带的hclust函数共有7种聚类方法for (i in 1:length(Cluster_Method) #make a function to extract the cluster method myclust-function(x) hclust(x,meth
11、od=Cluster_Methodi) #make heatmap by jpeg jpeg(filename=paste(Cluster_Methodi,.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_Methodi, col=greenred(64) dev.off()这样就可以一下子把七种cluster的方法依次用到heatmap上面来。而且通过对cluster树的比较,我们可以从中挑选出最好、最稳定到cluster方法,为后续分析打好基础!对下面这个数据聚类: require(graphics) hc - hclust(dist(USArrests), ave) plot(hc)首先对一个数据框用dist函数处理得到一个dist对象!Dist对象比较特殊,专门为hclust函数来画聚类树的!
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2