Anna
Anna

Reputation: 187

R: Is there a way to generate a heatmap of the dissimilarity matrix only and sort it by cluster?

I am using hclust to find clusters among 266 observations. All the variables are categorical. I am trying to create a heatmap of the dissimilarity matrix created from the daisy function, to visualise the within cluster similarity. I am using the heatmap function:

heatmap(x, Rowv = NULL, Colv = if(symm)"Rowv" else NULL,
        distfun = dist, hclustfun = hclust,
        reorderfun = function(d, w) reorder(d, w),
        add.expr, symm = FALSE, revC = identical(Colv, "Rowv"),
        scale = c("row", "column", "none"), na.rm = TRUE,
        margins = c(5, 5), 
        cexRow = 0.2 + 1/log10(nr), cexCol = 0.2 + 1/log10(nc),
        labRow = NULL, labCol = NULL, main = NULL,
        xlab = NULL, ylab = NULL,
        keep.dendro = FALSE, verbose = getOption("verbose"))

However, this function requires x to be a numeric matrix.I tried to enter my dissimilarity matrix instead using data.matrix(data, metric="gower") but when I do it considers the matrix as the dataset. The function heatmap.2 (which may have some additional settings) is not available in the R version I am using.

The second part of my question is if there is a way to reorder the heatmap in way that each cluster is clearly underlined on it. I was trying :

x <- cutree(hclust.object, 4)
#cluster number=4
cluster.membership <- factor(assignCluster(data, data, x))

And then set the following in the heatmap function:

reorderfun = function(hclust.object, cluster.membership) reorder(hclust.object, cluster.membership)

The results seem to be ordered as expected. However, I still can not find a way to visualise the clustering results on the heatmap (e.g. by using legends).

Upvotes: 0

Views: 890

Answers (1)

phago29
phago29

Reputation: 152

By using pheatmap function in pheatmap package, you can generate heatmap with clusters based on your dissimilarity matrix.

Assuming data is your dataset and hclust.object is hclust object produced based on dissimilarity matrix;

library(pheatmap)
pheatmap(data, cluster_rows = hclust.object, cutree_rows = 4)

should give you heatmap, which is clustered based on hclust object, which is generated by your dissimilarity matrix. You can also use other parameters in pheatmap function as you like.

For further information, use ?pheatmap.

Upvotes: 1

Related Questions