user2783615
user2783615

Reputation: 839

plot heatmap of data after clustering in R

I am trying to create a heatmap of a matrix after clustering.

heatmap(r.matrix, 
        distfun = dist(r.matrix, method="euclidean"), 
        hclustfun = hclust(dist(r.matrix, method="euclidean"), method ="ward"))

I used the above command following help message of heatmap, but came back with the following error message:

Error in heatmap(r.matrix, distfun = dist(r.matrix, method = "euclidean"),  :
                 could not find function "hclustfun"

How can I do a clustering, and draw a heatmap of the clustered data, also keep the dendrogram? I might have not understood the functions in the argument list well.

Upvotes: 3

Views: 6580

Answers (1)

Peyton
Peyton

Reputation: 7396

The distfun and hclustfun arguments are supposed to be functions. You're passing the results of those functions, and since the results aren't themselves functions, the error is thrown. You know how apply, for example, wants a function as its third argument? Then it calls that function itself? heatmap is the same way.

Try this:

heatmap(r.matrix, distfun=dist, hclustfun=function(d) hclust(d, method="ward"))

Actually, since dist is the default argument (see ?heatmap), you can omit distfun from the function call. The only reason you have to create an anonymous function for hclust is because the default method is not "ward".

heatmap(r.matrix, hclustfun=function(d) hclust(d, method="ward"))

Upvotes: 4

Related Questions