Skip to content

Commit

Permalink
Added notch option to stat_boxplot()
Browse files Browse the repository at this point in the history
- Corrected bug in stat_ellipse() for 2014a and earlier versions
  • Loading branch information
piermorel committed Jun 29, 2016
1 parent 1dd0644 commit 65b8ed8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
45 changes: 34 additions & 11 deletions @gramm/stat_boxplot.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
% a distance to the box equal to 1.5 times the interquartile
% range (Tukey boxplot).
% Points outside the whiskers ranges are plotted.
% - 'notch', set to true to display notches at median ± 1.58*IQR/sqrt(N)
% - 'dodge' allows to set the spacing between boxes of
% different colors within an unique value of x.
% - 'width' allows to set the width of the individual boxes.
Expand All @@ -17,6 +18,7 @@
p=inputParser;
my_addParameter(p,'width',0.6);
my_addParameter(p,'dodge',0.7);
my_addParameter(p,'notch',false);
parse(p,varargin{:});

obj.geom=vertcat(obj.geom,{@(dd)my_boxplot(obj,dd,p.Results)});
Expand Down Expand Up @@ -46,7 +48,11 @@
uni_x(diff(uni_x)<1e-10)=[];

%Initialize arrays
p=zeros(length(uni_x),5);
if params.notch
p=zeros(length(uni_x),7);
else
p=zeros(length(uni_x),5);
end
outliersx=[];
outliersy=[];

Expand All @@ -60,11 +66,17 @@
%Quartiles
temp=prctile(ysel,[25 50 75]);

%Outlier limits at 1.5 Inter Quartile Range
p(ind_x,:)=[temp(1)-1.5*(temp(3)-temp(1)) , temp , temp(3)+1.5*(temp(3)-temp(1))];
IQR=temp(3)-temp(1);

if params.notch
p(ind_x,:)=[temp(1)-1.5*IQR , temp(1), temp(2)-1.58*IQR/sqrt(length(ysel)) , temp(2) , temp(2)+1.58*IQR/sqrt(length(ysel)) , temp(3) , temp(3)+1.5*IQR];
else
%Outlier limits at 1.5 Inter Quartile Range
p(ind_x,:)=[temp(1)-1.5*IQR , temp , temp(3)+1.5*IQR];
end

%Outliers
sel_outlier=ysel<p(ind_x,1) | ysel>p(ind_x,5);
sel_outlier=ysel<p(ind_x,1) | ysel>p(ind_x,end);
if sum(sel_outlier)>0
outliersy=[outliersy ysel(sel_outlier)'];
outliersx=[outliersx repmat(ind_x,1,sum(sel_outlier))];
Expand All @@ -75,7 +87,7 @@
sel_non_outlier=~sel_outlier;
if sum(sel_non_outlier)>0
p(ind_x,1)=min(ysel(sel_non_outlier));
p(ind_x,5)=max(ysel(sel_non_outlier));
p(ind_x,end)=max(ysel(sel_non_outlier));
end

end
Expand All @@ -94,9 +106,17 @@
boxleft=boxmid-0.5*boxw;
boxright=boxmid+0.5*boxw;

notchleft=boxmid-0.25*boxw;
notchright=boxmid+0.25*boxw;

if params.notch
xpatch=[boxleft' ; boxright' ; boxright' ; notchright'; boxright' ; boxright' ; boxleft' ; boxleft' ; notchleft' ; boxleft'];
ypatch=[p(:,2)' ; p(:,2)' ; p(:,3)' ; p(:,4)' ; p(:,5)' ; p(:,6)' ; p(:,6)' ; p(:,5)' ; p(:,4)' ; p(:,3)'];
else
xpatch=[boxleft' ; boxright' ; boxright' ; boxleft'];
ypatch=[p(:,2)' ; p(:,2)' ; p(:,4)' ; p(:,4)'];

xpatch=[boxleft' ; boxright' ; boxright' ; boxleft'];
ypatch=[p(:,2)' ; p(:,2)' ; p(:,4)' ; p(:,4)'];
end

%Draw boxes
hndl=patch(xpatch,...
Expand All @@ -106,12 +126,15 @@
obj.results.stat_boxplot{obj.result_ind,1}.box_handle=hndl;

%Draw medians
obj.results.stat_boxplot{obj.result_ind,1}.median_handle=line([boxleft' ; boxright'],[p(:,3)' ; p(:,3)'],'Color','k');
if params.notch
obj.results.stat_boxplot{obj.result_ind,1}.median_handle=line([notchleft' ; notchright'],[p(:,4)' ; p(:,4)'],'Color','k');
else
obj.results.stat_boxplot{obj.result_ind,1}.median_handle=line([boxleft' ; boxright'],[p(:,3)' ; p(:,3)'],'Color','k');
end

%Draw whiskers

obj.results.stat_boxplot{obj.result_ind,1}.lower_whisker_handle=line([boxmid' ; boxmid'],[p(:,1)' ; p(:,2)'],'Color','k');
obj.results.stat_boxplot{obj.result_ind,1}.upper_whisker_handle=line([boxmid' ; boxmid'],[p(:,4)' ; p(:,5)'],'Color','k');
obj.results.stat_boxplot{obj.result_ind,1}.upper_whisker_handle=line([boxmid' ; boxmid'],[p(:,end-1)' ; p(:,end)'],'Color','k');

%Draw outliers
obj.results.stat_boxplot{obj.result_ind,1}.outliers_handle=plot(boxmid(outliersx),outliersy,'o','MarkerEdgeColor','none','MarkerFaceColor',draw_data.color);
Expand All @@ -120,7 +143,7 @@
obj.plot_lim.maxx(obj.current_row,obj.current_column)=max(max(boxright),obj.plot_lim.maxx(obj.current_row,obj.current_column));
obj.plot_lim.minx(obj.current_row,obj.current_column)=min(min(boxleft),obj.plot_lim.minx(obj.current_row,obj.current_column));

obj.plot_lim.maxy(obj.current_row,obj.current_column)=max(max(p(:,5)),obj.plot_lim.maxy(obj.current_row,obj.current_column));
obj.plot_lim.maxy(obj.current_row,obj.current_column)=max(max(p(:,end)),obj.plot_lim.maxy(obj.current_row,obj.current_column));
obj.plot_lim.miny(obj.current_row,obj.current_column)=min(min(p(:,1)),obj.plot_lim.miny(obj.current_row,obj.current_column));


Expand Down
5 changes: 4 additions & 1 deletion @gramm/stat_ellipse.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@
case 'line'
hndl=patch(conf_elpoints(1,:)+m(1),conf_elpoints(2,:)+m(2),draw_data.color,'FaceColor','none','EdgeColor',draw_data.color,'LineWidth',2);
end
tmp = set(hndl,params.patch_opts{:}); %displays a lot of stuff if we don't have an output value !
set(hndl,params.patch_opts{:});
%One matlab version displayed stuff if no output value was set (but
%crashes 2014a and earlier versions)
%tmp = set(hndl,params.patch_opts{:});



Expand Down
4 changes: 2 additions & 2 deletions examples.m
Original file line number Diff line number Diff line change
Expand Up @@ -678,8 +678,8 @@
g(4,1).set_title('''width'',0.6,''dodge'',0.4');

g(5,1).facet_grid([],c);
g(5,1).stat_boxplot('width',0.5,'dodge',0);
g(5,1).set_title('''width'',0.5,''dodge'',0');
g(5,1).stat_boxplot('width',0.5,'dodge',0,'notch',true);
g(5,1).set_title('''width'',0.5,''dodge'',0,''notch'',true');

g.set_title('Dodge and spacing options for stat_boxplot()');

Expand Down
Binary file modified gramm cheat sheet.pdf
Binary file not shown.
Binary file modified html/examples_15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 65b8ed8

Please sign in to comment.