setting distance matrix and clustering methods in heatmap.2
Asked Answered
C

1

18

heatmap.2 defaults to dist for calculating the distance matrix and hclust for clustering. Does anyone now how I can set dist to use the euclidean method and hclust to use the centroid method? I provided a compilable code sample bellow. I tried: distfun = dist(method = "euclidean"), but that doesn't work. Any ideas?

library("gplots")
library("RColorBrewer")

test <- matrix(c(79,38.6,30.2,10.8,22,
81,37.7,28.4,9.7,19.9,
82,36.2,26.8,9.8,20.9,
74,29.9,17.2,6.1,13.9,
81,37.4,20.5,6.7,14.6),ncol=5,byrow=TRUE)
colnames(test) <- c("18:0","18:1","18:2","18:3","20:0")
rownames(test) <- c("Sample 1","Sample 2","Sample 3", "Sample 4","Sample 5")
test <- as.table(test)
mat=data.matrix(test)

heatmap.2(mat,
dendrogram="row",
Rowv=TRUE,
Colv=NULL,
distfun = dist,
hclustfun = hclust,
xlab = "Lipid Species",
ylab = NULL,
colsep=c(1),
sepcolor="black",
key=TRUE,
keysize=1,
trace="none",
density.info=c("none"),
margins=c(8, 12),
col=bluered
)
Crissycrist answered 24/7, 2011 at 12:22 Comment(0)
B
33

Glancing at the code for heatmap.2 I'm fairly sure that the default is to use dist, and it's default is in turn to use euclidean distances.

The reason your attempt at passing distfun = dist(method = 'euclidean') didn't work is that distfun (and hclustfun) are supposed to simply be name of functions. So if you want to alter defaults and pass arguments you need to write a wrapper function like this:

heatmap.2(...,hclustfun = function(x) hclust(x,method = 'centroid'),...)

As I mentioned, I'm fairly certain that heatmap.2 is using euclidean distances by default, but a similar solution can be used to alter the distance function used:

heatmap.2(...,distfun = function(x) dist(x,method = 'euclidean'),...)
Buffington answered 24/7, 2011 at 14:21 Comment(2)
@Crissycrist - You're welcome, and welcome to SO! If this answer solved your problem, please click on the check mark next to it, so that others in the future who see this question will know that it solved your problem...Buffington
I couldn't get it to work. I created my function. heatmap2 <- function(...,hclustfun){ hclustfun= function(x) hclust(x,method = 'centroid') heatmap(...,hclustfun) } And now, I tried to pass it my usual arguments: heatmap2(as.matrix(mtcars), scale = "column", Colv = NA ) But I get the following error: Error in inherits(x, "dendrogram") : object of type 'closure' is not subsettableDecagon

© 2022 - 2024 — McMap. All rights reserved.