-
Notifications
You must be signed in to change notification settings - Fork 0
/
aboxplot.m
213 lines (187 loc) · 6.17 KB
/
aboxplot.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
%
% Copyright (C) 2011-2012 Alex Bikfalvi
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 3 of the License, or (at
% your option) any later version.
% This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
% General Public License for more details.
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
%
function aboxplot(X,varargin)
% Parameters
widthl = 0.7;
widths = 0.8;
widthe = 0.4;
outMarker = '.';
outMarkerSize = 3;
outMarkerEdgeColor = [0.6 0.6 0.6];
outMarkerFaceColor = [0.6 0.6 0.6];
alpha = 0.05;
cmap = [];
colorrev = 0;
colorgrd = 'blue_down';
% Get the number or data matrices
if iscell(X)
d = length(X);
else
% If data is a matrix extend to a 3D array
if 2 == ndims(X)
X = reshape(X, [1,size(X)]);
end
d = size(X,1);
end;
% Get the data size
if iscell(X)
n = size(X{1},2);
else
n = size(X,3);
end
% Set the labels
labels = cell(n,1);
for i=1:n
labels{i} = num2str(i);
end
% Optional arguments
optargin = size(varargin,2);
i = 1;
while i <= optargin
switch lower(varargin{i})
case 'labels'
labels = varargin{i+1};
case 'colormap'
cmap = varargin{i+1};
case 'colorgrad'
colorgrd = varargin{i+1};
case 'colorrev'
colorrev = varargin{i+1};
case 'outliermarker'
outMarker = varargin{i+1};
case 'outliermarkersize'
outMarkerSize = varargin{i+1};
case 'outliermarkeredgecolor'
outMarkerEdgeColor = varargin{i+1};
case 'outliermarkerfacecolor'
outMarkerFaceColor = varargin{i+1};
case 'widthl'
widthl = varargin{i+1};
case 'widths'
widths = varargin{i+1};
case 'widthe'
widthe = varargin{i+1};
end
i = i + 2;
end
% Colors
colors = cell(d,n);
if colorrev
% Set colormap
if isempty(cmap)
cmap = colorgrad(n,colorgrd);
end
if size(cmap,1) ~= n
error('The number of colors in the colormap must equal n.');
end
for j=1:d
for i=1:n
colors{j,i} = cmap(i,:);
end
end
else
% Set colormap
if isempty(cmap)
cmap = colorgrad(d,colorgrd);
end
if size(cmap,1) ~= d
error('The number of colors in the colormap must equal n.');
end
for j=1:d
for i=1:n
colors{j,i} = cmap(j,:);
end
end
end
xlim([0.5 n+0.5]);
hgg = zeros(d,1);
for j=1:d
% Get the j matrix
if iscell(X)
Y = X{j};
else
Y = squeeze(X(j,:,:));
end
% Create a hggroup for each data set
hgg(j) = hggroup();
set(get(get(hgg(j),'Annotation'),'LegendInformation'),'IconDisplayStyle','on');
legendinfo(hgg(j),'patch',...
'LineWidth',0.5,...
'EdgeColor','k',...
'FaceColor',colors{j,1},...
'LineStyle','-',...
'XData',[0 0 1 1 0],...
'YData',[0 1 1 0 0]);
for i=1:n
% Calculate the mean and confidence intervals
[q1 q2 q3 fu fl ou ol] = quartile(Y(:,i));
u = nanmean(Y(:,i));
% large interval [i - widthl/2 i + widthl/2] delta = widthl
% medium interval start: i - widthl/2 + (j-1) * widthl / d
% medium interval end: i - widthl/2 + j * widthl / d
% medium interval width: widthl / d
% medium interval middle: i-widthl/2+(2*j-1)*widthl/(2*d)
% small interval width: widths*widthl/d
% small interval start: i-widthl/2+(2*j-1-widths)*widthl/(2*d)
% Plot outliers
hold on;
plot((i-widthl/2+(2*j-1)*widthl/(2*d)).*ones(size(ou)),ou,...
'LineStyle','none',...
'Marker',outMarker,...
'MarkerSize',outMarkerSize,...
'MarkerEdgeColor',outMarkerEdgeColor,...
'MarkerFaceColor',outMarkerFaceColor,...
'HitTest','off',...
'Parent',hgg(j));
plot((i-widthl/2+(2*j-1)*widthl/(2*d)).*ones(size(ol)),ol,...
'LineStyle','none',...
'Marker',outMarker,...
'MarkerSize',outMarkerSize,...
'MarkerEdgeColor',outMarkerEdgeColor,...
'MarkerFaceColor',outMarkerFaceColor,...
'HitTest','off',...
'Parent',hgg(j));
hold off;
% Plot fence
line([i-widthl/2+(2*j-1)*widthl/(2*d) i-widthl/2+(2*j-1)*widthl/(2*d)],[fu fl],...
'Color','k','LineStyle',':','HitTest','off','Parent',hgg(j));
line([i-widthl/2+(2*j-1-widthe)*widthl/(2*d) i-widthl/2+(2*j-1+widthe)*widthl/(2*d)],[fu fu],...
'Color','k','HitTest','off','Parent',hgg(j));
line([i-widthl/2+(2*j-1-widthe)*widthl/(2*d) i-widthl/2+(2*j-1+widthe)*widthl/(2*d)],[fl fl],...
'Color','k','HitTest','off','Parent',hgg(j));
% Plot quantile
if q3 > q1
rectangle('Position',[i-widthl/2+(2*j-1-widths)*widthl/(2*d) q1 widths*widthl/d q3-q1],...
'EdgeColor','k','FaceColor',colors{j,i},'HitTest','off','Parent',hgg(j));
end
% Plot median
line([i-widthl/2+(2*j-1-widths)*widthl/(2*d) i-widthl/2+(2*j-1+widths)*widthl/(2*d)],[q2 q2],...
'Color','k','LineWidth',1,'HitTest','off','Parent',hgg(j));
% Plot mean
hold on;
plot(i-widthl/2+(2*j-1)*widthl/(2*d), u,...
'LineStyle','none',...
'Marker','o',...
'MarkerEdgeColor','k',...
'MarkerFaceColor',colors{j,i},...
'HitTest','off','Parent',hgg(j));
hold off;
end
end
box on;
set(gca,'XTick',1:n);
set(gca,'XTickLabel',labels);
end