Skip to content

Commit

Permalink
Added possiblity to omit 'x' or 'y' values in geom_polygon() in order…
Browse files Browse the repository at this point in the history
… to generate shaded horizontal or vertical areas.
  • Loading branch information
piermorel committed Mar 14, 2017
1 parent 7378bf2 commit 86ce6e9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 35 deletions.
5 changes: 0 additions & 5 deletions @gramm/draw.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
% call for the redraw() function, i.e. deactivates the fancy
% axis placement.

% Edit 2017-Mar-07
% Added support for geom_polygon
% author: Nicholas J. Schaub, Ph.D.
% email: [email protected]

%We set redraw() as resize callback by default
if nargin<2
do_redraw=true;
Expand Down
62 changes: 35 additions & 27 deletions @gramm/geom_polygon.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,11 @@
% point in a polygon.
%
% Optional inputs (defaults):
% map (obj.color_options.map) - Use this to override main color mapping
% alpha (0.2) - set fill alpha
% color ([]) - line color indexing, empty is black
% fill ([]) - fill color indexing, empty is gray
% style ([]) - line type, empty is no line. Options are identical to
% set_line_type('styles')
% color ([0 0 0]) - fill color of the polygon
% line_color ([0 0 0]) - line color of the polygon
% line_style ({'none'}) - line style
%
% Example Usage:
% figure;
% g=gramm('x',Model_Year,'y',MPG,'color',Cylinders,'subset',Cylinders~=3 & Cylinders~=5);
% g.facet_grid([],origin_region);
% g.geom_point();
% g.stat_glm();
% cmap = [1 0.5 0.5; % red (bad gas mileage)
% 1 1 0.5; % yellow (reasonable gas mileage)
% 0.5 1 0.5]; % green (good gas mileage)
% g.geom_polygon('x',{[50;90;90;50] [50;90;90;50] [50;90;90;50]},'y',{[5;5;20;20] [20;20;30;30] [30;30;50;50]},'alpha',.2,'fill',[1 2 3],'map',cmap);
% g.set_names('column','Origin','x','Year of production','y','Fuel economy (MPG)','color','# Cylinders');
% g.set_title('Fuel economy of new cars between 1970 and 1982');
% g.draw();
%
% created: 2017-Mar-03
% author: Nicholas J. Schaub, Ph.D.
Expand All @@ -47,40 +32,63 @@

my_addParameter(p,'x',{});
my_addParameter(p,'y',{});
my_addParameter(p,'alpha',0.1);
my_addParameter(p,'alpha',0.2);
my_addParameter(p,'color',[0 0 0]);
my_addParameter(p,'line_color',[0 0 0]);
my_addParameter(p,'line_style',{'none'});
my_addParameter(p,'extent',2);
parse(p,varargin{:});

temp_results=p.Results;


%% Check inputs
if isempty(p.Results.x) || isempty(p.Results.y)
warning('Either x or y is not provided. Will not draw polygons.')
if isempty(temp_results.x) && isempty(temp_results.y)
warning('Both x and y are not provided. Will not draw polygons.')
return
end

if ~iscell(p.Results.x) || ~iscell(p.Results.y)
if ~iscell(temp_results.x) || ~iscell(temp_results.y)
warning('Either x or y is not a cell. Will not draw polygons.')
return
end

if length(p.Results.x) ~= length(p.Results.y)
%If one of the xy input is omitted, we fill it with an cell full of empty
%arrays
if isempty(temp_results.x)
N=length(temp_results.y);
temp_results.x=repmat({[]},N,1);
elseif isempty(temp_results.y)
N=length(temp_results.x);
temp_results.y=repmat({[]},N,1);
else
N=length(temp_results.x);
end


if length(temp_results.x) ~= length(temp_results.y)
warning('The number of elements in x does not match y. Will not draw polygons.')
return
end

if ~isequal(cellfun(@length,p.Results.x),cellfun(@length,p.Results.y))
%Check number of vertices
nvx=cellfun(@length,temp_results.x);
nvy=cellfun(@length,temp_results.y);

%Which cases are allowed for omitted inputs
to_complete = (nvx==2 & nvy==0) | (nvx==0 & nvy==2);

if ~isequal(nvx(~to_complete),nvy(~to_complete))
warning('The number of x-coords does not match the number of y-coords for each polygon. Will not draw polygons.')
return
end

temp_results=p.Results;

N=length(temp_results.x);


%Expand the inputs for which single entries were given to the number of
%polygons
to_adjust={'alpha','color','line_color','line_style'};
to_adjust={'alpha','color','line_color','line_style','extent'};
for k=1:length(to_adjust)
if size(temp_results.(to_adjust{k}),1)==1
temp_results.(to_adjust{k}) = repmat(temp_results.(to_adjust{k}),N,1);
Expand Down
3 changes: 2 additions & 1 deletion @gramm/gramm.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
'line_style',{[]},...
'color',[],...
'line_color',[],...
'alpha',[]);
'alpha',[],...
'extent',[]);

datetick_params={} %cell containng datetick parameters
current_row %What is the currently drawn row of the subplot
Expand Down
21 changes: 20 additions & 1 deletion @gramm/private/draw_polygons.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,26 @@ function draw_polygons(obj)

for poly_ind = 1:length(obj.polygon.x)

p = patch(obj.polygon.x{poly_ind},obj.polygon.y{poly_ind},obj.polygon.color(poly_ind,:),...
tmp_x=obj.polygon.x{poly_ind};
tmp_y=obj.polygon.y{poly_ind};

%Handle cases of omitted x or y values
if isempty(tmp_x)
tmp_xl=[obj.var_lim.minx obj.var_lim.maxx];
tmp_extent=(tmp_xl(2)-tmp_xl(1))*obj.polygon.extent(poly_ind)/2;
xl=[mean(tmp_xl)-tmp_extent mean(tmp_xl)+tmp_extent];
tmp_y=[tmp_y(1) tmp_y(2) tmp_y(2) tmp_y(1)];
tmp_x=[xl(1) xl(1) xl(2) xl(2)];
end
if isempty(tmp_y)
tmp_yl=[obj.var_lim.miny obj.var_lim.maxy];
tmp_extent=(tmp_yl(2)-tmp_yl(1))*obj.polygon.extent(poly_ind)/2;
yl=[mean(tmp_yl)-tmp_extent mean(tmp_yl)+tmp_extent];
tmp_x=[tmp_x(1) tmp_x(2) tmp_x(2) tmp_x(1)];
tmp_y=[yl(1) yl(1) yl(2) yl(2)];
end

p = patch(tmp_x,tmp_y,obj.polygon.color(poly_ind,:),...
'Parent',obj.facet_axes_handles(obj.current_row,obj.current_column),...
'FaceColor',obj.polygon.color(poly_ind,:),...
'FaceAlpha',obj.polygon.alpha(poly_ind),...
Expand Down
12 changes: 11 additions & 1 deletion examples.m
Original file line number Diff line number Diff line change
Expand Up @@ -674,19 +674,27 @@
g.set_title('Fuel economy of new cars between 1970 and 1982');

g(1,2)=copy(g(1));
g(1,3)=copy(g(1));
g(2,1)=copy(g(1));
g(2,2)=copy(g(1));



%X and Y must be column cells
%X, Y and style must be cells

%Easier defaults, just grey polygons
g(1,1).geom_polygon('x',{[50;90;90;50] ; [50;90;90;50]},'y',{[5;5;20;20] ; [30;30;50;50]});
g(1,1).geom_polygon('x',{[50 90 90 50] ; [50 90 90 50]},'y',{[5 5 20 20] ; [30 30 50 50]});

%Possibility to manually set fill, color, style and alpha.
g(1,2).geom_polygon('x',{[50;90;90;50] ; [50;90;90;50] ; [50;90;90;50]},'y',{[5;5;20;20]; [20;20;30;30]; [30;30;50;50]},'color',cmap,'alpha',0.3);


%Possibility to generate polygons more easily by omitting 'x' or 'y' values
%and giving only two coordinates for the non-omitted ones
g(1,3).geom_polygon('y',{[5 20]; [20 30]; [30 50]},'color',cmap);
g(1,3).geom_polygon('x',{[80 85]},'alpha',0.1);

%Possibility to set color and fill by indices (using a column vector of
%integers. Colormap generated between 1 and max(vector))
g(2,1).geom_polygon('x',{[50;90;90;50] ; [50;90;90;50] ; [50;90;90;50]},'y',{[5;5;20;20]; [20;20;30;30]; [30;30;50;50]},'color',[1 ; 2; 3]);
Expand All @@ -697,6 +705,8 @@
g(2,2).geom_polygon('x',{[50;90;90;50] },'y',{ [20;20;30;30]});




g.draw();


Expand Down

0 comments on commit 86ce6e9

Please sign in to comment.