Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plot violins in a specified order #14

Merged
merged 5 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions test_cases/testviolinplot.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

% TEST CASE 1
display('Test 1: Violin plot default options');
load carbig MPG Origin
Origin = cellstr(Origin);
figure
vs = violinplot(MPG, Origin);
ylabel('Fuel Economy in MPG');
xlim([0.5, 7.5]);

display('Test 1 passed ok');

% TEST CASE 2
display('Test 2: Test the plot ordering option');
grouporder={'USA','Sweden','Japan','Italy','Germany','France','England'};

figure;
vs2 = violinplot(MPG,Origin,'GroupOrder',grouporder);
display('Test 2 passed ok');

%other test cases could be added here
23 changes: 22 additions & 1 deletion violinplot.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,27 @@
% Defaults to false
% 'ShowMean' Whether to show mean indicator
% Defaults to false
% 'GroupOrder' Cell of category names in order to be plotted.
% Defaults to alphabetical ordering

% Copyright (c) 2016, Bastian Bechtold
% This code is released under the terms of the BSD 3-clause license

hascategories = exist('cats','var') && not(isempty(cats));

%parse the optional grouporder argument
%if it exists parse the categories order
% but also delete it from the arguments passed to Violin
grouporder = {};
idx=find(strcmp(varargin, 'GroupOrder'));
if ~isempty(idx) && numel(varargin)>idx
if iscell(varargin{idx+1})
grouporder = varargin{idx+1};
varargin(idx:idx+1)=[];
else
error('Second argument of ''GroupOrder'' optional arg must be a cell of category names')
end
end

% tabular data
if isa(data, 'dataset') || isstruct(data) || istable(data)
Expand All @@ -73,7 +89,12 @@

% 1D data, one category for each data point
elseif hascategories && numel(data) == numel(cats)
cats = categorical(cats);
if isempty(grouporder)
cats = categorical(cats);
else
cats = categorical(cats, grouporder);
end

catnames = categories(cats);
for n=1:length(catnames)
thisCat = catnames{n};
Expand Down