Hey Ann,
I went thourgh Sam's code on file exchange and was able to make the labels work. I made only little changes to its code which are as follows:
- Function Signature: Added labels as an input parameter.
- Label Handling: Modified the loop that relabels leaves to use the provided labels array.
This is the modified code:
function [h,T,perm] = polardendrogram(Z, labels, varargin)
%POLARDENDROGRAM plots a polar dendrogram plot, taking same options as
%dendrogram and giving same outputs.
% Example:
% X= rand(100,2);
% Y= pdist(X,'cityblock');
% Z= linkage(Y,'average');
% [H,T] = polardendrogram(Z,'colorthreshold','default');
% Plot a normal dendrogram
[h,T,perm] = dendrogram(Z, varargin{:});
% Get x and y ranges
xlim = get(gca,'XLim');
ylim = get(gca,'YLim');
minx = xlim(1);
maxx = xlim(2);
miny = ylim(1);
maxy = ylim(2);
xrange = maxx - minx;
yrange = maxy - miny;
% Reshape into a polar plot
for i = 1:size(h)
xdata = get(h(i),'XData');
ydata = get(h(i),'YData');
% Rescale xdata to go from pi/12 to 2pi - pi/12
xdata = (((xdata - minx) / xrange) * (pi * 11 / 6)) + (pi / 12);
% Rescale ydata to go from 1 to 0, cutting off lines
% which drop below the axis limit
ydata = max(ydata, miny);
ydata = 1 - ((ydata - miny) / yrange);
% To make horizontal lines look more circular,
% insert ten points into the middle of the line before
% polar transform
newxdata = [xdata(1), linspace(xdata(2), xdata(3), 10), xdata(4)];
newydata = [ydata(1), repmat(ydata(2), 1, 10), ydata(4)];
% Transform to polar coordinates
[xdata, ydata] = pol2cart(newxdata, newydata);
% Reset line positions to new polar positions
set(h(i), 'XData', xdata);
set(h(i), 'YData', ydata);
end
% Relabel leaves
for i = 1:length(labels)
[x, y] = pol2cart((((i - minx) / xrange) * (pi * 11 / 6)) + (pi * 1 / 12), 1.1);
text(x, y, labels{i}, 'HorizontalAlignment', 'center');
end
% Add and label gridlines
hold on
lineh(1) = polar([0,0], [0,1], '-');
lineh(2) = polar(linspace(0, 2 * pi, 50), ones(1, 50), ':');
lineh(3) = polar(linspace(0, 2 * pi, 50), ones(1, 50) * 0.75, ':');
lineh(4) = polar(linspace(0, 2 * pi, 50), ones(1, 50) * 0.5, ':');
lineh(5) = polar(linspace(0, 2 * pi, 50), ones(1, 50) * 0.25, ':');
set(lineh, 'Color', [0.5, 0.5, 0.5]);
for i = 1:4
[x, y] = pol2cart(0, i / 4);
str = sprintf('%2.1f', ((maxy - miny) * (4 - i) / 4) + miny);
text(x, y, str, 'VerticalAlignment', 'bottom');
end
% Prettier
set(gca, 'XLim', [-1.5, 1.5], 'YLim', [-1.5, 1.5], 'Visible', 'off');
view(3)
axis fill
daspect([1, 1, 100]);
zoom(2.8);
end
And this is how you may utilize the above fucntion:
X = rand(5, 20);
Y = pdist(X, 'cityblock');
Z = linkage(Y, 'average');
labels = {'a', 'b', 'c', 'd', 'e'}; % Define your labels here
clf;
polardendrogram(Z, labels, 'colorthreshold', 'default');
zoom(0.8);
view(2);
Hope this helps!