How to cluster data in a boxplot?

조회 수: 10 (최근 30일)
Marc Elmeua
Marc Elmeua 2023년 6월 5일
댓글: Marc Elmeua 2023년 6월 6일
I am trying to cluster boxplots with no success.
My data is sorted as follows: columns 1 to 8 are subject properties; columns 9 to end are 4 repeated measures of different variables, each column represents a single measure.
I would like to split the file based on the unique combinations of 2 properties (Gait, direction) and cluster the boxplots based on another property (Group). Therefore I would get 4 repeated measures on the x-Axis, its corresponding value on the y-axis and both groups clustered in the plot.
So far I have achieved to plot separately the unique combination of gait, direction and group. I now want to remove group from the combination and use it as a clustering factor.
Can anyone help with this?? thanks in advance!
Data is attached. My code:
numColumns = size(data, 2); % Total number of columns in the data
% Extract unique gait and direction values
uniqueGaits = unique(data.Gait);
uniqueDirections = unique(data.Direction);
uniqueGroups = unique(data.Group);
% Set the maximum number of open figures
maxOpenFigures = 10;
figureCount = 0; % Counter for the number of open figures
% Create a dedicated folder for saving figures
folderName = 'Figures';
if ~exist(folderName, 'dir')
mkdir(folderName);
end
for i = 1:numel(uniqueGaits)
gait = uniqueGaits{i};
for j = 1:numel(uniqueDirections)
direction = uniqueDirections{j};
for k = 1:numel(uniqueGroups)
group = uniqueGroups{k};
% Filter the data based on the unique combination
filteredData = data(strcmp(data.Gait, gait) & strcmp(data.Direction, direction) & strcmp(data.Group, group), :);
% Iterate over the desired range of column indices
for col = 9:4:numColumns
% Extract columns for the current combination
variableData = table2array(filteredData(:, col:col+3));
% Close figures exceeding the maximum limit
if figureCount >= maxOpenFigures
close all;
figureCount = 0;
end
% Create boxplot for the current combination
figure;
boxplot(variableData);
title(['Boxplot - Gait: ', gait, ', Direction: ', direction, ', Group: ', group]);
xlabel('Variables');
ylabel('Value');
% Retrieve column names
columnNames = filteredData.Properties.VariableNames(col:col+3);
% Set x-axis tick labels as column names
xticklabels(columnNames);
% Save the figure in the dedicated folder
saveas(gcf, fullfile(folderName, ['figure_', num2str(figureCount), '.png']));
figureCount = figureCount + 1;
end
end
end
end
% Close any remaining open figures
close all;

채택된 답변

Ive J
Ive J 2023년 6월 5일
편집: Ive J 2023년 6월 6일
what about this?
data = load("data.mat").data;
% just a personal preference: strings are easier to work with
cellCols = varfun(@(x)isa(x, "cell"), data);
data = convertvars(data, table2array(cellCols), @string);
% group based on combinations of "Direction" and "Gait"
gtab = groupsummary(data, ["Direction", "Gait"], @(x){x}, [7, 9:width(data)]);
gtab.Properties.VariableNames = regexprep(gtab.Properties.VariableNames, "^fun1_", ""); % clean variable names
% find variables for plotting
cols = string(gtab.Properties.VariableNames);
idx = endsWith(cols, "_" + digitsPattern(1) + textBoundary("end")); % _1, _2, etc
viz_cols = cols(idx);
viz_patt = unique(viz_cols.erase("_" + digitsPattern(1) + textBoundary("end"))); % unique pattern of variables for plotting
% loop over each combination pairs
for k1 = 1 % 1:height(gtab) %# DEBUG
% loop over plotting variables
for k2 = 1 % 1:numel(viz_patt) %# DEBUG
close gcf force
% create a visualization table
idx = startsWith(cols, viz_patt(k2));
tmp_col = repmat(gtab.Group(k1), sum(idx), 1);
tmp_value = gtab{k1, cols(idx)}';
tmp_group = repelem(1:numel(tmp_value), cellfun(@numel, tmp_value));
viz_tab = table(categorical(vertcat(tmp_col{:})),...
vertcat(tmp_value{:}), categorical(tmp_group'), ...
VariableNames=["cluster", "value", "group"]);
% colour by 'Group' column and group by repeated measures
ax = boxchart(viz_tab.group, viz_tab.value, GroupByColor=viz_tab.cluster);
legend(ax); % change aesthetic of 'ax' if needed
xlabel(ax(1).Parent, viz_patt(k2).replace("_", "-"))
title(ax(1).Parent, sprintf("Direction:%s, Gait:%s", gtab.Direction(k1), gtab.Gait(k1)))
% exportgraphics(gcf, viz_patt(k2) + "." + gtab.Direction(k1) + "."
% + gtab.Gait(k1) + ".png") %# DEBUG
end
end
  댓글 수: 1
Marc Elmeua
Marc Elmeua 2023년 6월 6일
This is very helpful, thanks a lot.
I'll try to understand everything in the code, but it works :')

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by