From: Cagri on
Hi,
I'm applying agglomerative hierarchical clustering with various linkage values, and then I obtain the dendogram of the clustering, as follows:

-------------------------------------
Y = pdist(Ap);
Z = linkage(Y, 'average');
dendrogram(Z,0);
clusters = cluster(Z,'maxclust',k);
-------------------------------------

So far so good, I get all leaf nodes displayed. Now, I want to cut the dendrogram with a - not necessarily visible - dashed line, so that this cut will create k clusters, where k is given. I want to color the k subtrees under the cutoff depth with different colors, so that k clusters are visible and distinguishable by color.

I was wondering if anybody has an idea how to do it.
From: Tom Lane on
> So far so good, I get all leaf nodes displayed. Now, I want to cut the
> dendrogram with a - not necessarily visible - dashed line, so that this
> cut will create k clusters, where k is given. I want to color the k
> subtrees under the cutoff depth with different colors, so that k clusters
> are visible and distinguishable by color.

If you type "help dendrogram" you should see:

[...] = DENDROGRAM(...,'COLORTHRESHOLD',T)

That seems like what you want.

-- Tom


From: Cagri on
"Tom Lane" <tlane(a)mathworks.com> wrote in message <hpbdsh$rvf$1(a)fred.mathworks.com>...
> > So far so good, I get all leaf nodes displayed. Now, I want to cut the
> > dendrogram with a - not necessarily visible - dashed line, so that this
> > cut will create k clusters, where k is given. I want to color the k
> > subtrees under the cutoff depth with different colors, so that k clusters
> > are visible and distinguishable by color.
>
> If you type "help dendrogram" you should see:
>
> [...] = DENDROGRAM(...,'COLORTHRESHOLD',T)
>
> That seems like what you want.
>
> -- Tom
>

colorsthreshold is the way, but the key was to find T. To find T, I used Z. The following code worked fine:

--------------------------------------------------------------------------------------
Y = pdist(Ap);
Z = linkage(Y, 'average');
clusters = cluster(Z,'maxclust',k);
% Find the distance threshold
t = sort(Z(:,3));
th = t(size(Z,1)+2-k);
dendrogram(Z,0,'colorthreshold', th);
---------------------------------------------------------------------------------------

Works perfectly well.