-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscatterErrorBarPlot.m
More file actions
239 lines (185 loc) · 8.05 KB
/
scatterErrorBarPlot.m
File metadata and controls
239 lines (185 loc) · 8.05 KB
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
function scatterErrorBarPlot(A,errorType,cmap,marker_size,marker_order,cmap_edge)
% USAGE: scatterErrorBarPlot(A,errorType,cmap,marker_size,marker_order,cmap_edge)
%
% REQUIRED INPUT:
% A m x n matrix. Will plot n bars with m scattered data points
%
% OPTIONAL INPUTS:
% errorType Type of displayed error bars. Default is 95% confidence intervals.
% Choices: 'STD' (standard deviation),'SEM' (standard error
% of the mean), '95CI' (95% confidence intervals), and
% 'STDratio', 'SEMratio','95CIratio' (log corrected error bars).
% Important note: these are independent, not joint, 95%
% confidence intervals. Can also choose 'none' to skip bars.
% cmap b x 3 matrix of rgb colors (values between 0 and 1) for bars.
% Defaults to color blind friendly colormap. Colormap
% repeats if b < n.
% marker_size Size of plotted markers. Default 10.
% marker_order Symbols to use for markers.
% Default repeats the sequence {'o','s','^','v','d'}.
% cmap_edge b x 3 matrix of rgb colors for bar edges. Default = cmap;
%%%%%%%
% Change Log
% 2016/07/19 RML - created function
% 2016/07/20 RML - more flexibility with different sized matrices and pairs colors
% 2016/08/03 RML - choose color
% 2016/09/08 RML - use nanmean, nanstd
% 2017/01/13 RML - choose between paired colors or unique colors
% 2018/02/28 RML - edges can be different than bar center, thicker linewidth
% 2018/07/20 RML - show error bars even if not paired
% 2019/11/15 RML - can handle NaN columns now
% 2020/05/29 RML - no longer requires dependent functions
% 2020/07/02 RML - add flexibility in choosing error bar types, remove
% paired colors (now covered by colormap options)
% 2020/07/26 RML - convert from nanmean/nanstd to 'omitnan' to remove
% statistical toolbox dependency.
% 2020/08/01 RML - make ratio error bars work with NaNs
% 2020/08/19 RML - add option of 'none' for error bars
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%% Load default parameters %%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Data Points
jitter = 0.3; % Hard coded parameter for scatter in data points
% Marker Size
if ~exist('marker_size','var') || isempty(marker_size)
marker_size = 10;
end
% Marker Order
if ~exist('marker_order','var') || isempty(marker_order)
marker_order = repmat({'o','s','^','v','d'},[1 ceil(size(A,2)/5)]);
elseif length(marker_order) < size(A,2)
marker_order = repmat(marker_order,[1 ceil(size(A,2)/length(marker_order))]);
end
%%% Bar Plot Colormaps
% Main Colormap
if ~exist('cmap','var') || isempty(cmap)
% This color map is based on a Nature Methods paper:
% doi:10.1038/nmeth.1618 (See Figure 2)
cmap = [0.6 0.6 0.6
0.9020 0.6235 0
0.3373 0.7059 0.9137
0 0.6196 0.4510
0.9412 0.8941 0.2588
0 0.4471 0.6980
0.8353 0.3686 0
0.8000 0.4745 0.6549];
end
if length(cmap) < size(A,2)
warning('Color map will repeat to accomodate data')
repnum = ceil(size(A,2)/size(cmap,1));
cmap = repmat(cmap,[repnum, 1]);
end
% Bar Edges
if ~exist('cmap_edge','var') || isempty(cmap_edge)
cmap_edge = cmap;
end
if length(cmap_edge) < size(A,2)
warning('Edge color map will repeat to accomodate data')
repnum = ceil(size(A,2)/size(cmap_edge,1));
cmap_edge = repmat(cmap_edge,[repnum, 1]);
end
%%% Errorbars
if ~exist('errorType','var') || isempty(errorType)
errorType = '95CI';
end
typeCheck = strcmp(errorType,{'95CI','STD','SEM','95CIratio','STDratio','SEMratio','none'});
if sum(typeCheck) == 0
error('Please enter a valid error bar type: ''95CI'',''STD'',''SEM'',''95CIratio'',''STDratio'',''SEMratio'',''none''')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%% Start Plotting %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Make the bars
for kk = 1:size(A,2)
slice = A(:,kk);
bar(kk,mean(slice,'omitnan'),'FaceColor',cmap(kk,:),'EdgeColor',cmap_edge(kk,:),'linewidth',2)
if kk == 1
hold on
end
end
%%%%%% Plot the data points
for kk = 1:size(A,2)
xslice = repmat(kk,[size(A,1) 1]);
yslice = A(:,kk);
J = (rand(size(xslice))-0.5)*jitter;
plot(xslice+J,yslice,'k',...
'Marker',marker_order{kk},'LineStyle','none',...
'MarkerFaceColor','k',...
'MarkerSize',marker_size)
end
%%%%%% Add the errorbars
if strcmp(errorType,'STD') % Standard Deviation Error Bars
for kk = 1:size(A,2)
slice = A(:,kk);
barVal = std(slice,'omitnan');
errorbar([kk -8],[mean(slice,'omitnan') 0],[barVal 0],'.k','LineWidth',2,'Marker','none') % The value at -8 is a silly way to get wider error bars
end
elseif strcmp(errorType,'SEM') % Standard Error of the Mean Error Bars
for kk = 1:size(A,2)
slice = A(:,kk);
N = numel(find(~isnan(slice)));
barVal = std(slice,'omitnan')/sqrt(N);
errorbar([kk -8],[mean(slice,'omitnan') 0],[barVal 0],'.k','LineWidth',2,'Marker','none') % The value at -8 is a silly way to get wider error bars
end
elseif strcmp(errorType,'95CI') % 95% CI Error Bars
for kk = 1:size(A,2)
slice = A(:,kk);
N = numel(find(~isnan(slice)));
t_star = tinv(1-0.05/2,N-1);
barVal = t_star*std(slice,'omitnan')/sqrt(N);
errorbar([kk -8],[mean(slice,'omitnan') 0],[barVal 0],'.k','LineWidth',2,'Marker','none') % The value at -8 is a silly way to get wider error bars
end
elseif strcmp(errorType,'STDratio') % STD Error Bars for Ratio Values
for kk = 1:size(A,2)
slice = A(:,kk);
slice(isnan(slice)) = [];
if sum(slice==0)
warning('log(0) = -Inf. Error bars will not display')
end
sliceLog = log(slice);
stdLog = std(sliceLog);
meanLog = mean(sliceLog);
upLog = meanLog + stdLog;
lowLog = meanLog - stdLog;
upperB = exp(upLog) - mean(slice,'omitnan');
lowerB = exp(lowLog) - mean(slice,'omitnan');
errorbar([kk -8],[mean(slice,'omitnan') 0],[lowerB 0],[upperB 0],'.k','LineWidth',2,'Marker','none') % The value at -8 is a silly way to get wider error bars
end
elseif strcmp(errorType,'SEMratio') % SEM Error Bars for Ratio Values
for kk = 1:size(A,2)
slice = A(:,kk);
slice(isnan(slice)) = [];
if sum(slice==0)
warning('log(0) = -Inf. Error bars will not display')
end
sliceLog = log(slice);
N = numel(find(~isnan(slice)));
stdLog = std(sliceLog);
meanLog = mean(sliceLog);
upLog = meanLog + stdLog/sqrt(N);
lowLog = meanLog - stdLog/sqrt(N);
upperB = exp(upLog) - mean(slice,'omitnan');
lowerB = exp(lowLog) - mean(slice,'omitnan');
errorbar([kk -8],[mean(slice,'omitnan') 0],[lowerB 0],[upperB 0],'.k','LineWidth',2,'Marker','none') % The value at -8 is a silly way to get wider error bars
end
elseif strcmp(errorType,'95CIratio') % 95% CI Error Bars for Ratio Values
for kk = 1:size(A,2)
slice = A(:,kk);
slice(isnan(slice)) = [];
if sum(slice==0)
warning('log(0) = -Inf. Error bars will not display')
end
sliceLog = log(slice);
N = numel(find(~isnan(slice)));
t_star = tinv(1-0.05/2,N-1);
stdLog = std(sliceLog);
meanLog = mean(sliceLog);
upLog = meanLog + stdLog/sqrt(N)*t_star;
lowLog = meanLog - stdLog/sqrt(N)*t_star;
upperB = exp(upLog) - mean(slice,'omitnan');
lowerB = exp(lowLog) - mean(slice,'omitnan');
errorbar([kk -8],[mean(slice,'omitnan') 0],[lowerB 0],[upperB 0],'.k','LineWidth',2,'Marker','none') % The value at -8 is a silly way to get wider error bars
end
end
hold off
%%%%%% Set the x-axis limits
a = size(A,2);
xlim([0 a]+.5)
box off