Plot Dendrogram Clusters in Different Colors

10 views (last 30 days)
William
William on 25 Oct 2014
Edited: micholeodon on 14 Apr 2021
Hi all,
I'm working with 3D points that specify the XYZ locations of cells in a brain. I'm trying to represent how these cells are clustered. Specifically, I'd like to be able to have a dendrogram that is colored such that each color represents a different cluster (colors chosen are arbitrary.
My question is very similar to this previous one, and I want my final dendrogram to look something very similar to this, except that the solution to the question relies on setting the 'colorthreshold' property such that all nodes under a certain distance are colored. In my dendrogram, I want my clusters to be determined by a specific inconsistency coefficient as opposed to cutting off at a specific distance. If you imagine my data as having two distinct 'types' of clusters (e.g. a very small, tight cluster and a very large cluster), identifying a specific threshold distance won't work, as the threshold distance assumes (to some degree) that all clusters of interest will be of the same size, which might not be the case. This is why I'm using inconsistency coefficient in order to determine what's a cluster, and this is why I'd like to color my dendrogram clusters by inconsistency coefficient.
In summary, I want to be able to plot specific dendrogram clusters (defined by an inconsistency coefficient) in a certain color. Of course, I can use the cluster function and a specific inconsistency coefficient to get a list of which cluster a datapoint is in. However, the problem is plotting these dendrogram clusters in specific colors.
Any ideas? Many thanks in advance.
W
  1 Comment
micholeodon
micholeodon on 14 Apr 2021
Edited: micholeodon on 14 Apr 2021
Here is how you get colors from your dendrogram and link it with the observations.
By changing 'Color' property in lines stored in h variable below you can change colors in your dendrogram.
clear; close all; clc;
%% Generate example data
rng('default') % For reproducibility
N = 10; % number of observations
X = rand(N,3);
%% Get linkage
tree = linkage(X, 'average');
%% Get desired number of clusters
nClusters = 2;
cutoff = median([tree(end-nClusters+1,3) tree(end-nClusters+2, 3)]);
%% plot tree
figure
h = dendrogram(tree, 'ColorThreshold', cutoff); % h contains Line objects
%% get colors
linesColor = cell2mat(get(h,'Color')); % get lines color;
colorList = unique(linesColor, 'rows');
% NOTE that each row is single line corresponding to the same row in tree
% variable. I use that property below.
X_color = zeros(N,3);
X_cluster = zeros(N,1);
for iLeaf = 1:N
[iRow, ~] = find(tree==iLeaf);
color = linesColor(iRow,:); % !
% assign color to each observation
X_color(iLeaf,:) = color;
% assign cluster number to each observation
X_cluster(iLeaf,:) = find(ismember(colorList, color, 'rows'));
end
%% changing lines color
h(5).Color = [0 1 0]

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!