From abf0cc7ef3df154b45da22f566f39452a6734de7 Mon Sep 17 00:00:00 2001 From: seamusholden Date: Wed, 10 Jul 2019 09:55:21 +0100 Subject: [PATCH 1/5] Added the ability to plot violins in a specified order --- violinplot.m | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/violinplot.m b/violinplot.m index 6b4fba3..0ab4869 100644 --- a/violinplot.m +++ b/violinplot.m @@ -44,11 +44,26 @@ % 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 +%modified 190606 to allow category ordering hascategories = exist('cats','var') && not(isempty(cats)); + grouporder = {}; + ii=1; + while ii<=numel(varargin) + if strcmp(varargin{ii},'GroupOrder') + grouporder = varargin{ii+1}; + varargin(ii:ii+1)=[]; + ii=ii+2; + else + ii=ii+1; + end + end + % tabular data if isa(data, 'dataset') || isstruct(data) || istable(data) @@ -73,7 +88,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}; From c2f3a0524536edd60b4aeead36cf04d97a56db98 Mon Sep 17 00:00:00 2001 From: seamusholden Date: Wed, 10 Jul 2019 11:50:13 +0100 Subject: [PATCH 2/5] removed the inline editing info --- violinplot.m | 1 - 1 file changed, 1 deletion(-) diff --git a/violinplot.m b/violinplot.m index 0ab4869..4007b58 100644 --- a/violinplot.m +++ b/violinplot.m @@ -49,7 +49,6 @@ % Copyright (c) 2016, Bastian Bechtold % This code is released under the terms of the BSD 3-clause license -%modified 190606 to allow category ordering hascategories = exist('cats','var') && not(isempty(cats)); grouporder = {}; From 03f0ed249a432f64bbe8ee72137da2fb04dee913 Mon Sep 17 00:00:00 2001 From: seamusholden Date: Wed, 17 Jul 2019 15:08:33 +0100 Subject: [PATCH 3/5] Added test case script Added a test script to test the default violinplot and the new plot ordering option before I make further changes --- test_cases/testviolinplot.m | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 test_cases/testviolinplot.m diff --git a/test_cases/testviolinplot.m b/test_cases/testviolinplot.m new file mode 100644 index 0000000..6047002 --- /dev/null +++ b/test_cases/testviolinplot.m @@ -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 From 338429ead15232b04437a6177b015045b0015f92 Mon Sep 17 00:00:00 2001 From: seamusholden Date: Wed, 17 Jul 2019 15:08:50 +0100 Subject: [PATCH 4/5] Update violinplot.m Resolve the spacing/ formatting request --- violinplot.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/violinplot.m b/violinplot.m index 4007b58..8b75c8c 100644 --- a/violinplot.m +++ b/violinplot.m @@ -90,7 +90,7 @@ if isempty(grouporder) cats = categorical(cats); else - cats = categorical(cats,grouporder); + cats = categorical(cats, grouporder); end catnames = categories(cats); From 53c76d0ed8efe84c36e2ee3c049615acd0c88231 Mon Sep 17 00:00:00 2001 From: seamusholden Date: Wed, 17 Jul 2019 15:20:14 +0100 Subject: [PATCH 5/5] Update violinplot.m Updated the vararg parsing. Not sure it's ended upsimpler but it is a bit more robust --- violinplot.m | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/violinplot.m b/violinplot.m index 8b75c8c..c151312 100644 --- a/violinplot.m +++ b/violinplot.m @@ -51,19 +51,21 @@ % 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 = {}; - ii=1; - while ii<=numel(varargin) - if strcmp(varargin{ii},'GroupOrder') - grouporder = varargin{ii+1}; - varargin(ii:ii+1)=[]; - ii=ii+2; + 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 - ii=ii+1; + 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) if isa(data, 'dataset')