rheatmapeuclidean-distancelinkage

Changing Distance and Linkage of a heat map


I am trying to change the distance and linkage of my heat map from the standard Euclidean Distance and Complete linkage to any other setting for a question in my homework.

We are using this data set:

library("flexclust")
data(milk)
help(milk)

this is my current code:

hm3<-heatmap(milk_matrix, scale="column", main="Heat map of the Composition of Mammals' Milk", xlab= "Nutrient", ylab = "Animal", cexRow = 0.6, cexCol = 1.1, col=heat.colors(10))
legend(x= "bottomright", legend=c("min", "med", "max"),  cex = 0.6, fill=heat.colors(3))

R help for heat map says you can change the distance/ linkage by:

distfun= and hclustfun=

but I have had no luck typing something such as hclustfun= "single".

Any help would be greatly appreciated ! I can specify any distance or linkage as long as it is not Euclidean Distance and Complete linkage.

Kind Regards, Rosie :)

heat map changing distance and linkage


Solution

  • For the distfun and hclustfun arguments you have to use options already available in the stats::dist() and stats::hclust() (check their help). So for example for distfun based on "maximum" distance measure instead of the default "euclidean" you can use the following:

    # dist method must be one of "euclidean", "maximum", "manhattan", "canberra", "binary" or "minkowski"
    
    heatmap(as.matrix(milk), 
            scale="column", 
            main="Heat map of the Composition of Mammals' Milk", 
            xlab= "Nutrient", 
            ylab = "Animal", 
            cexRow = 0.6, 
            cexCol = 1.1, 
            col=heat.colors(10),
            distfun = function(x) dist(x, method = "maximum"))
    legend(x= "bottomright", legend=c("min", "med", "max"),  cex = 0.6, fill=heat.colors(3))