Cluster heat map in Julia

A cluster heat map is a way of plotting a matrix. Here is an example: File:Heatmap.png - Wikipedia (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?

Does this work?

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.

Just heatmap.

https://juliaplots.github.io/examples/gr/#heatmap-categorical-axes-and-aspect_ratio

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.

2 Likes

For the clustering you can use the Clustering package:

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)
2 Likes

That only works to sort the columns, though.