How to cluster data in a boxplot?

12 Ansichten (letzte 30 Tage)
Marc Elmeua
Marc Elmeua am 5 Jun. 2023
Kommentiert: Marc Elmeua am 6 Jun. 2023
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')
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;
% Create boxplot for the current combination
title(['Boxplot - Gait: ', gait, ', Direction: ', direction, ', Group: ', group]);
% Retrieve column names
columnNames = filteredData.Properties.VariableNames(col:col+3);
% Set x-axis tick labels as column names
% Save the figure in the dedicated folder
saveas(gcf, fullfile(folderName, ['figure_', num2str(figureCount), '.png']));
figureCount = figureCount + 1;
% Close any remaining open figures
close all;

Akzeptierte Antwort

Ive J
Ive J am 5 Jun. 2023
Bearbeitet: Ive J am 6 Jun. 2023
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.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
  1 Kommentar
Marc Elmeua
Marc Elmeua am 6 Jun. 2023
This is very helpful, thanks a lot.
I'll try to understand everything in the code, but it works :')

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)




Community Treasure Hunt

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

Start Hunting!

Translated by