Plot boxplot/boxchart outliers in different colors by goups

34 次查看(过去 30 天)
Hello community,
I´m running out of ideas on how to handle this problem. I have a table with 91 columns. The first 88 columns are features and and the last 3 are labels for this features. One of the three label columns tells, if the sample is considered sick or not. So I want to have a boxchart/boxplot which shows me the boxplot for each of this 88 features and color the outliers by their label, if they are sick or not.
All I get is this boxchart atm.
and my tbale looks like this
Here is a subset of the table. Only with path_1 and path_2 as columns and the labels column
  5 个评论
dpb
dpb 2022-9-23
编辑:dpb 2022-9-23
I was just agreeing/acknowledging...
A very quick perusal of boxplot wasn't much promising...I'm guessing you'll have to add the additional info onto the basic boxplot -- and how friendly it is to such shenanigans I don't know; I've not really tried much in that vein.

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2022-9-24
编辑:dpb 2022-9-25
fn=websave('sub_table.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1134300/sub_table.mat');
load(fn)
isO=isoutlier(sub_table{:,1:end-1},"quartiles"); % outlier logical address array
[~,ic]=ismember(sub_table.label,categories(sub_table.label)); % lookup index for outlier
C=[1 0 0; 0 1 0; 0 0 1]; % r,g,b color each type label; update as desired
hBX=boxchart(sub_table{:,1:end-1}); % draw base box chart
hold on % get ready to add on top of box chart
drawnow % needed to refresh visible result; not needed if at command line
hBX.NodeChildren(1).Visible='off'; % the hidden handle hides marker object (outliers)
hAx=gca; % get current axes handle
% iterate over each patient in table -- assumes only one label last column in index limits
for i=1:numel(sub_table.Properties.VariableNames(1:end-1))
x=repmat(hAx.XTick(i),sum(isO(:,i)),1); % need a vector of categorical type
y=sub_table{isO(:,i),i}; % and the outliers for the patient
clr=C(ic(isO(:,i)),:); % the color by label lookup function
hSC(i)=scatter(x,y,[],clr); % draw the colored outliers
end
% dummy up a legend by color code -- scatter is just one object each subject
hL=plot(nan(size(C)),'o'); % as many lines as labels
set(hL,{'Color'},mat2cell(C,[1 1 1],3)) % match colors
hLG=legend(hL,categories(sub_table.label)); % label names
box on
"Salt to suit" on color mapping, symbols, size -- I could not find the handles to the markers to be able to delete them; there is only one color property for them however, so something like the above appears to be about the only choice.
It would be nice to use the 'jitter' option to not have the outliers all in the one vertical line...this is incorporated in the boxchart but not so easy to do with the added scatter plot since the x-axis is categorical and so only allows the discrete categorical values. One would have to then put the scatter plot on a second axes or revert to a numeric x-axis on the original data instead.

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by