Thanks for the feedback, Jesse and Paolo. I wrote the following ordering function which will hopefully 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="",row.names=T, col.names=T)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…