/
spider_plot.m
337 lines (297 loc) · 9.63 KB
/
spider_plot.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
function spider_plot(P, P_labels, axes_interval, axes_precision, legend_str, varargin)
dbstop if error
% Create a spider web or radar plot with an axes specified for each column
%
% spider_plot(P, P_labels, axes_interval, axes_precision) creates a spider
% web plot using the points specified in the array P. The column of P
% contains the data points and the rows of P contain the multiple sets of
% data points. Each point must be accompanied by a label specified in the
% cell P_labels. The number of intervals that separate the axes is
% specified by axes_interval. The number of decimal precision points is
% specified by axes_precision.
%
% P - [vector | matrix]
% P_labels - [cell of string]
% axes_interval - [integer]
% axes_precision - [integer]
%
% spider_plot(P, P_labels, axes_interval, axes_precision, line_spec) works
% the same as the function above. Additional line properties can be added
% in the same format as the default "plot" function in MATLAB.
%
% line_spec - [character vector]
%
% %%%%%%%%%%%%%%%%%%% Example of a Generic Spider Plot %%%%%%%%%%%%%%%%%%%
% % Clear workspace
% close all;
% clearvars;
% clc;
%
% % Point properties
% num_of_points = 6;
% row_of_points = 4;
%
% % Random data
% P = rand(row_of_points, num_of_points);
%
% % Scale points by a factor
% P(:, 2) = P(:, 2) * 2;
% P(:, 3) = P(:, 3) * 3;
% P(:, 4) = P(:, 4) * 4;
% P(:, 5) = P(:, 5) * 5;
%
% % Make random values negative
% P(1:3, 3) = P(1:3, 3) * -1;
% P(:, 5) = P(:, 5) * -1;
%
% % Create generic labels
% P_labels = cell(num_of_points, 1);
%
% for ii = 1:num_of_points
% P_labels{ii} = sprintf('Label %i', ii);
% end
%
% % Figure properties
% figure('units', 'normalized', 'outerposition', [0 0.05 1 0.95]);
%
% % Axes properties
% axes_interval = 2;
% axes_precision = 1;
%
% % Spider plot
% spider_plot(P, P_labels, axes_interval, axes_precision,...
% 'Marker', 'o',...
% 'LineStyle', '-',...
% 'LineWidth', 2,...
% 'MarkerSize', 5);
%
% % Title properties
% title('Sample Spider Plot',...
% 'Fontweight', 'bold',...
% 'FontSize', 12);
%
% % Legend properties
% legend('show', 'Location', 'southoutside');
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Point Properties %%%
% Number of points
[row_of_points, num_of_points] = size(P);
%%% Error Check %%%
% Check if axes properties are an integer
if floor(axes_interval) ~= axes_interval || floor(axes_precision) ~= axes_precision
error('Error: Please enter in an integer for the axes properties.');
end
% Check if axes properties are positive
if axes_interval < 1 || axes_precision < 1
error('Error: Please enter value greater than one for the axes properties.');
end
% Check if the labels are the same number as the number of points
if length(P_labels) ~= num_of_points
error('Error: Please make sure the number of labels is the same as the number of points.');
end
% Pre-allocation
max_values = zeros(1, num_of_points);
min_values = zeros(1, num_of_points);
axis_increment = zeros(1, num_of_points);
% Normalized axis increment
normalized_axis_increment = 1/axes_interval;
% Iterate through number of points
for ii = 1:num_of_points
% Group of points
group_points = P(:, ii);
% Max and min value of each group
% max_values(ii) = max(group_points);
% min_values(ii) = min(group_points);
% range = max_values(ii) - min_values(ii);
%CH - made this so that instead the axis is always 0-1
max_values(ii) = 1;
min_values(ii) = 0;
range = 1;
% Axis increment
axis_increment(ii) = range/axes_interval;
% Normalize points to range from [0, 1] - CH removed this
%P(:, ii) = (P(:, ii)-min(group_points))/range;
% Shift points by one axis increment
P(:, ii) = P(:, ii) + normalized_axis_increment;
end
%%% Polar Axes %%%
% Polar increments
polar_increments = 2*pi/num_of_points;
% Normalized max limit of axes
axes_limit = 1;
% Shift axes limit by one axis increment
axes_limit = axes_limit + normalized_axis_increment;
% Polar points
radius = [0; axes_limit];
theta = 0:polar_increments:2*pi;
% Convert polar to cartesian coordinates
[x_axes, y_axes] = pol2cart(theta, radius);
% Plot polar axes
grey = [1, 1, 1] * 0.5;
h = line(x_axes, y_axes,...
'LineWidth', 1,...
'Color', grey);
% Iterate through all the line handles
for ii = 1:length(h)
% Remove polar axes from legend
h(ii).Annotation.LegendInformation.IconDisplayStyle = 'off';
end
%%% Polar Isocurves %%%
% Shifted axes interval
shifted_axes_interval = axes_interval+1;
% Incremental radius
radius = (0:axes_limit/shifted_axes_interval:axes_limit)';
% Convert polar to cartesian coordinates
[x_isocurves, y_isocurves] = pol2cart(theta, radius);
% Plot polar isocurves
hold on;
h = plot(x_isocurves', y_isocurves',...
'LineWidth', 1,...
'Color', grey);
% Iterate through all the plot handles
for ii = 1:length(h)
% Remove polar isocurves from legend
h(ii).Annotation.LegendInformation.IconDisplayStyle = 'off';
end
%%% Figure Properties %%%
colors = [0, 0.4470, 0.7410;...
0.8500, 0.3250, 0.0980;...
0.9290, 0.6940, 0.1250;...
0.4940, 0.1840, 0.5560;...
0.4660, 0.6740, 0.1880;...
0.3010, 0.7450, 0.9330;...
0.6350, 0.0780, 0.1840];
% Repeat colors is necessary
repeat_colors = fix(row_of_points/size(colors, 1))+1;
colors = repmat(colors, repeat_colors, 1);
%%% Data Points %%%
% Iterate through all the rows
for ii = 1:row_of_points
% Convert polar to cartesian coordinates
[x_points, y_points] = pol2cart(theta(1:end-1), P(ii, :));
% Make points circular
x_circular = [x_points, x_points(1)];
y_circular = [y_points, y_points(1)];
% Plot data points
subset(ii) = plot(x_circular, y_circular,...
'Color', colors(ii, :),...
'MarkerFaceColor', colors(ii, :),...
varargin{:});
f = fill(x_circular, y_circular, colors(ii,:));
set(f,'facealpha', 0.5)
end
%%% Axis Properties %%%
% Figure background
fig = gcf;
fig.Color = 'white';
% Iterate through all the number of points
for hh = 1:num_of_points
% Shifted min value
shifted_min_value = min_values(hh)-axis_increment(hh);
% Axis label for each row
row_axis_labels = (shifted_min_value:axis_increment(hh):max_values(hh))';
% Iterate through all the isocurve radius - CH removed
% for ii = 2:length(radius)
% % Display axis text for each isocurve
% text(x_isocurves(ii, hh), y_isocurves(ii, hh), sprintf(sprintf('%%.%if', axes_precision), row_axis_labels(ii)),...
% 'Units', 'Data',...
% 'Color', 'k',...
% 'FontSize', 8,...
% 'FontName', 'Arial',...
% 'HorizontalAlignment', 'center',...
% 'VerticalAlignment', 'middle');
% end
end
% Label points
x_label = x_isocurves(end, :);
y_label = y_isocurves(end, :);
% Shift axis label
shift_pos = 0.07;
% Iterate through each label
for ii = 1:num_of_points
% Angle of point in radians
theta_point = theta(ii);
% Find out which quadrant the point is in
if theta_point == 0
quadrant = 0;
elseif theta_point == pi/2
quadrant = 1.5;
elseif theta_point == pi
quadrant = 2.5;
elseif theta_point == 3*pi/2
quadrant = 3.5;
elseif theta_point == 2*pi
quadrant = 0;
elseif theta_point > 0 && theta_point < pi/2
quadrant = 1;
elseif theta_point > pi/2 && theta_point < pi
quadrant = 2;
elseif theta_point > pi && theta_point < 3*pi/2
quadrant = 3;
elseif theta_point > 3*pi/2 && theta_point < 2*pi
quadrant = 4;
end
% Adjust text alignment information depending on quadrant
switch quadrant
case 0
horz_align = 'left';
vert_align = 'middle';
x_pos = shift_pos;
y_pos = 0;
case 1
horz_align = 'left';
vert_align = 'bottom';
x_pos = shift_pos;
y_pos = shift_pos;
case 1.5
horz_align = 'center';
vert_align = 'bottom';
x_pos = 0;
y_pos = shift_pos;
case 2
horz_align = 'right';
vert_align = 'bottom';
x_pos = -shift_pos;
y_pos = shift_pos;
case 2.5
horz_align = 'right';
vert_align = 'middle';
x_pos = -shift_pos;
y_pos = 0;
case 3
horz_align = 'right';
vert_align = 'top';
x_pos = -shift_pos;
y_pos = -shift_pos;
case 3.5
horz_align = 'center';
vert_align = 'top';
x_pos = 0;
y_pos = -shift_pos;
case 4
horz_align = 'left';
vert_align = 'top';
x_pos = shift_pos;
y_pos = -shift_pos;
end
% Display text label
text(x_label(ii)+x_pos, y_label(ii)+y_pos, P_labels{ii},...
'Units', 'Data',...
'HorizontalAlignment', horz_align,...
'VerticalAlignment', vert_align,...
'EdgeColor', 'none',...
'FontSize', 32,...
'FontName', 'Arial',...
'FontWeight', 'bold',...
'BackgroundColor', 'w');
end
% Axis limits
axis square;
axis([-axes_limit, axes_limit, -axes_limit, axes_limit]);
axis off;
ax = gca;
ax.FontName = 'Arial';
% ax.FontWeight = 'bold';
ax.FontSize = 16;
legend(subset, legend_str, 'Location', 'northeastout')
end