The order of the lines in the heat map?

Take the following code:

heatmap(data.matrix(signals),col=colors,breaks=breaks,scale="none",Colv=NA,labRow=NA) 

How can I extract, pre-calculate or recalculate the row order in the resulting heatmap? Is there a way to input the output of hclust(dist(signals)) into a hclust(dist(signals)) function?

+5
r cluster-analysis heatmap
source share
5 answers

Thanks for the feedback, Jesse and Paolo. I wrote the following ordering function, which I hope will be useful to others:

 data = data.matrix(data) distance = dist(data) cluster = hclust(distance, method="ward") dendrogram = as.dendrogram(cluster) Rowv = rowMeans(data, na.rm = T) dendrogram = reorder(dendrogram, Rowv) ## Produce the heatmap from the calculated dendrogram. ## Don't allow it to re-order rows because we have already re-ordered them above. reorderfun = function(d,w) { d } png("heatmap.png", res=150, height=22,width=17,units="in") heatmap(data,col=colors,breaks=breaks,scale="none",Colv=NA,Rowv=dendrogram,labRow=NA, reorderfun=reorderfun) dev.off() ## Re-order the original data using the computed dendrogram rowInd = rev(order.dendrogram(dendrogram)) di = dim(data) nc = di[2L] nr = di[1L] colInd = 1L:nc data_ordered <- data[rowInd, colInd] write.table(data_ordered, "rows.txt",quote=F, sep="\t",row.names=T, col.names=T) 
+12
source share

There are many options. If you run ?heatmap , you will see various options that you can configure. Perhaps the easiest way is to set Rowv=NA , which should suppress the reordering of rows, and then pass to the matrix the rows that are already in the order you want. But you can also manually create a clustering function or dendrogram through Rowv and hclustfun , etc.

+4
source share

I agree with Jesse. For your problem, consider the arguments to Rowv , distfun and hclustfun function. For a larger selection of the heatmap.2 function in the gplots package, heatmap_plus in the Heatplus package and pheatmap in the pheatmap package can be useful.

+2
source share

I believe this post might be helpful:

How does R arrange order series by default?

Take, for example, the following matrix:

 set.seed(321) m = matrix(nrow=7, ncol = 7, rnorm(49)) > m [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 1.7049032 0.2331354 -1.1534395 -0.10706154 -1.1203274 0.11453945 0.2503958 [2,] -0.7120386 0.3391139 -0.8046717 0.98833540 -0.4746847 -2.22626331 0.2440872 [3,] -0.2779849 -0.5519147 0.4560691 -1.07223880 -1.5304122 1.63579034 0.7997382 [4,] -0.1196490 0.3477014 0.4203326 -0.75801528 0.4157148 -0.15932072 0.3414096 [5,] -0.1239606 1.4845918 0.5775845 0.09500072 0.6341979 0.02826746 0.2587177 [6,] 0.2681838 0.1883255 0.4463561 -2.33093117 1.2308474 -1.53665329 0.9538786 [7,] 0.7268415 2.4432598 0.9172555 0.41751598 -0.1545637 0.07815779 1.1364147 

You can override the order of rows and columns with the Rowv and Colv . You can override the order using this data as dendrograms. For example, you can calculate the order using the hclust function, and then pass this to heatmap as a dendrogram:

  rhcr <- hclust(dist(m)) chrc <- hclust(dist(t(m))) heatmap(m,Rowv = as.dendrogram(rhcr), Colv = as.dendrogram(rhcr)) > rhcr$order [1] 1 3 6 2 7 4 5 > chrc$order [1] 6 4 5 1 2 3 7 

gives:

Hclust heatmap

The heat map function by default uses one additional step, however, through the parameter reorderfun = function(d, w) reorder(d, w) , which reorganizes the dendrogram as much as possible, based on the value of the row / column. You can reproduce the default order with this additional step. Thus, to get the same order as the heatmap , you can do:

 rddr <- reorder(as.dendrogram(rhcr),rowMeans(m)) cddr <- reorder(as.dendrogram(chcr),colMeans(m)) > as.hclust(rddr)$order [1] 3 1 6 2 4 5 7 > as.hclust(cddr)$order [1] 6 4 5 1 2 3 7 

Which gives the same result as just heatmap(m) :

Default heatmap

In this example, the columns are not getting reordered, but the rows are executed. Finally, to just get an order, you can assign a heatmap to a variable and get the result.

 > p <- heatmap(m) > p$rowInd [1] 3 1 6 2 4 5 7 > p$colInd [1] 6 4 5 1 2 3 7 
+2
source share

pheatmap allows you to specify the method that it uses for clustering, taking the same arguments as hclust.

+1
source share

All Articles