How to cluster data in a boxplot?
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Marc Elmeua
el 5 de Jun. de 2023
Comentada: Marc Elmeua
el 6 de Jun. de 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')
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;
0 comentarios
Respuesta aceptada
Ive J
el 5 de Jun. de 2023
Editada: Ive J
el 6 de Jun. de 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.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
Más respuestas (0)
Ver también
Categorías
Más información sobre Annotations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!