title: R: How do I display clustered matrix heatmap (similar color patterns are grouped)
body: <p>I searched a lot of questions about heatmap throughout the site and packages, but I still have a problem.
  <br>
  I have clustered data (kmeans/EM/DBscan..), and I want to create a heatmap by grouping the same cluster. I want the similar color patterns to be grouped in the heatmap, so generally, it looks like a block-diagonal. <br>
  I tried to order the data by the cluster number and display it,<br>
  <pre><code>k = kmeans(data, 3)
  d = data.frame(data)
  d = data.frame(d, k$cluster)
  d = d[order(d$k.cluster),]
  heatmap(as.matrix(d))
  </code></pre> but it is still not sorted and looks like this link:<img src="https://i.stack.imgur.com/ca8sL.png" alt="enter image description here"> <br>But, I want it to be sorted by its cluster number and looked like this:<img src="https://i.stack.imgur.com/MNbM4.png" alt="enter image description here"><br>
  Can I do this in R?<br>
  I searched lots of packages and tried many ways, but I still have a problem.<br>
  Thanks a lot.</p>