# Cluster heat map in Julia

**URL:** <https://discourse.julialang.org/t/cluster-heat-map-in-julia/5502>\
**Category:** General Usage\
**Created:** [August 22, 2017, 12:57pm UTC](https://discourse.julialang.org/t/cluster-heat-map-in-julia/5502 "2017-08-22T12:57:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [August 22, 2017, 12:57pm UTC](https://discourse.julialang.org/t/cluster-heat-map-in-julia/5502/1 "2017-08-22T12:57:25Z")

</div>

A cluster heat map is a way of plotting a matrix. Here is an example: [File:Heatmap.png - Wikipedia](https://en.wikipedia.org/wiki/File:Heatmap.png) (I don’t need the trees).

The interesting part is that rows and columns are reordered so that similar values are closer together.

Is there a package in Julia providing this functionality?

---

<div class="post-metadata">

**Author:** ![ecx2093](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ecx2093/32/5623_2.png) [@ecx2093](https://discourse.julialang.org/u/ecx2093)\
**Post date:** [August 22, 2017, 1:38pm UTC](https://discourse.julialang.org/t/cluster-heat-map-in-julia/5502/2 "2017-08-22T13:38:34Z")

</div>

Does this work?

```julia
using GR
x = rand(20,20);
heatmap(x)

```

However, I am not sure which function in Plots.jl is used to access the GR heatmap.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 22, 2017, 2:01pm UTC](https://discourse.julialang.org/t/cluster-heat-map-in-julia/5502/3 "2017-08-22T14:01:15Z")

</div>

> [@ecx2093](#):
>
> However, I am not sure which function in Plots.jl is used to access the GR heatmap.

Just `heatmap`.

[https://juliaplots.github.io/examples/gr/#heatmap-categorical-axes-and-aspect\_ratio](https://juliaplots.github.io/examples/gr/#heatmap-categorical-axes-and-aspect_ratio)

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [August 22, 2017, 2:19pm UTC](https://discourse.julialang.org/t/cluster-heat-map-in-julia/5502/4 "2017-08-22T14:19:22Z")

</div>

Note though that this does not address the issue of clustering. I’m pretty sure that you’d need to cluster the matrix first, then plot the heatmap. The process of clustering a matrix by rows and columns is called biclustering, and is “soon to be implemented” in MLMetrics.jl.

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [August 22, 2017, 2:51pm UTC](https://discourse.julialang.org/t/cluster-heat-map-in-julia/5502/5 "2017-08-22T14:51:26Z")

</div>

For the clustering you can use the Clustering package:

```julia
using Clustering, Distributions

# build a matrix with 4 classes
μ = rand(4)*20
μ = StatsBase.sample(μ,100)
M = hcat([rand(Normal(μ,1),200) for μ in μ]...)

# cluster it
c = kmeans(M,4)
idx = sortperm(assignments(c))
M = M[:,idx]

# use you favorite heatmap plot
image(M)

```

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [August 22, 2017, 3:16pm UTC](https://discourse.julialang.org/t/cluster-heat-map-in-julia/5502/6 "2017-08-22T15:16:05Z")

</div>

That only works to sort the columns, though.
