Skip to content

Commit

Permalink
Added 'alpha' option to geom_point(), geom_jitter() and geom_line()
Browse files Browse the repository at this point in the history
  • Loading branch information
piermorel committed Sep 29, 2016
1 parent ef2e44a commit cba705a
Show file tree
Hide file tree
Showing 45 changed files with 105 additions and 18 deletions.
4 changes: 3 additions & 1 deletion @gramm/draw.m
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,9 @@
tmp_N=100;

gradient_height=4;
imagesc([1 1.5],[obj.legend_y-legend_y_step*gradient_height obj.legend_y],linspace(min(min(obj.plot_lim.minc)),max(max(obj.plot_lim.maxc)),tmp_N)','Parent',obj.legend_axe_handle);
%imagesc coordinates correspond to centers of patches, hence the
%x=[1.5 1.5] to get [1 2] borders
imagesc([1.5 1.5],[obj.legend_y-legend_y_step*gradient_height obj.legend_y],linspace(min(min(obj.plot_lim.minc)),max(max(obj.plot_lim.maxc)),tmp_N)','Parent',obj.legend_axe_handle);

line([1.8 2;1.8 2;1.8 2 ; 1 1.2 ; 1 1.2 ; 1 1.2]',...
[obj.legend_y-legend_y_step*gradient_height/4 obj.legend_y-legend_y_step*gradient_height/4 ;...
Expand Down
5 changes: 4 additions & 1 deletion @gramm/geom_jitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

p=inputParser;
my_addParameter(p,'width',0.2);
my_addParameter(p,'height',0.2);
my_addParameter(p,'height',0);
my_addParameter(p,'dodge',0);
my_addParameter(p,'alpha',1);
parse(p,varargin{:});

obj.geom=vertcat(obj.geom,{@(dd)my_jitter(obj,dd,p.Results)});
Expand Down Expand Up @@ -44,5 +45,7 @@
%hndl=my_point(obj,draw_data);
hndl=line(draw_data.x,draw_data.y,'LineStyle','none','Marker',draw_data.marker,'MarkerEdgeColor','none','markerSize',draw_data.point_size,'MarkerFaceColor',draw_data.color);

set_alpha(hndl,1,params.alpha);

obj.results.geom_jitter_handle{obj.result_ind,1}=hndl;
end
5 changes: 5 additions & 0 deletions @gramm/geom_line.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

p=inputParser;
my_addParameter(p,'dodge',0);
my_addParameter(p,'alpha',1);
parse(p,varargin{:});

obj.geom=vertcat(obj.geom,{@(dd)my_line(obj,dd,p.Results)});
Expand Down Expand Up @@ -55,5 +56,9 @@
hndl=line(combnan(draw_data.x),combnan(draw_data.y),combnan(draw_data.z),'LineStyle',draw_data.line_style,'lineWidth',draw_data.line_size,'Color',draw_data.color);
end
end


set_alpha(hndl,params.alpha,1);

obj.results.geom_line_handle{obj.result_ind,1}=hndl;
end
3 changes: 3 additions & 0 deletions @gramm/geom_point.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

p=inputParser;
my_addParameter(p,'dodge',0);
my_addParameter(p,'alpha',1);
parse(p,varargin{:});


Expand Down Expand Up @@ -67,5 +68,7 @@
end
end

set_alpha(hndl,1,params.alpha);

obj.results.geom_point_handle{obj.result_ind,1}=hndl;
end
19 changes: 13 additions & 6 deletions @gramm/private/plotci.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,19 @@
xci=[tmp_xci1;tmp_xci2];
[x,y]=to_polar(obj,x,y);

%Use vertices and faces for patch object construction (has the
%advantage of properly handling NaN datapoints
vertices=nan(length(x)*2,2);
vertices(1:2:end,:)=[tmp_xci1' tmp_yci1'];
vertices(2:2:end,:)=[tmp_xci2' tmp_yci2'];
faces=[(1:length(x)*2-2)' (1:length(x)*2-2)'+1 (1:length(x)*2-2)'+2];

if 1 %any(isnan(tmp_yci1)) || any(isnan(tmp_yci2))
%Use vertices and faces for patch object construction with triangles (has the
%advantage of properly handling NaN datapoints
vertices=nan(length(x)*2,2);
vertices(1:2:end,:)=[tmp_xci1' tmp_yci1'];
vertices(2:2:end,:)=[tmp_xci2' tmp_yci2'];
faces=[(1:length(x)*2-2)' (1:length(x)*2-2)'+1 (1:length(x)*2-2)'+2];
else
%We create patches as one single polygon (looks much better when exporting with plot2svg)
vertices=horzcat([tmp_xci1' ; flipud(tmp_xci2')],[tmp_yci1' ; flipud(tmp_yci2')]);
faces=1:2*length(x);
end

%Line plot doesn't display anything if we have NaNs around, so find
%out which points are surrounded by NaNs and might not be
Expand Down
32 changes: 32 additions & 0 deletions @gramm/private/set_alpha.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function [ ] = set_alpha(input_handle,line_alpha,point_alpha)

persistent old_matlab
if isempty(old_matlab)
old_matlab=verLessThan('matlab','8.4.0');
end

if ~old_matlab
%Works for continuous color points made with scatter, but alpha goes
%back to 1 when CLimMode is set to manual in draw(), line 835
if point_alpha<1
drawnow; %Necessary for handle to be available
hMarkers = input_handle.MarkerHandle;
c=hMarkers.FaceColorData;
hMarkers.FaceColorType = 'truecoloralpha'; %Allows to set alpha
hMarkers.FaceColorData=[c(1:3,:); repmat(uint8(point_alpha*255),1,size(c,2))];
end

%Does not work for continuous color lines (which are a hack based on patch)
if line_alpha<1
drawnow; %Necessary for handle to be available
hEdge = input_handle.Edge;
c=hEdge.ColorData;
hEdge.ColorType = 'truecoloralpha'; %Allows to set alpha
hEdge.ColorData=[c(1:3,:); repmat(uint8(line_alpha*255),1,size(c,2))];
end
end



end

11 changes: 8 additions & 3 deletions examples.m
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@
g(1,1).geom_point();
%'patch_opts' can be used to provide more options to the patch() internal
%call
g(1,1).set_point_options('base_size',2);
g(1,1).stat_ellipse('type','95percentile','geom','area','patch_opts',{'FaceAlpha',0.1,'LineWidth',2});
g(1,1).set_title('stat_ellispe()');

Expand Down Expand Up @@ -308,12 +309,15 @@
g(2,1).set_names('column','grp','color','count');
g(2,1).set_title('stat_bin2d(''geom'',''image'')');

g(2,2)=gramm('x',x,'y',y,'color',test);
g(2,2).geom_point('alpha',0.05);
g(2,2).set_point_options('base_size',6);
g(2,2).set_title('geom_point(''alpha'',0.05)');

g.set_title('Visualization of 2D densities');

figure('Position',[100 100 800 600])
g.draw();
%We change the point size in the first graph a posteriori
set([g(1,1).results.geom_point_handle],'MarkerSize',2);

%% Methods for visualizing repeated trajectories
% gramm supports 2D inputs for X and Y data (as 2D array or cell of
Expand Down Expand Up @@ -777,9 +781,10 @@
'label',cars_table.ModelShort,'color',cars_table.Manufacturer,'subset',strcmp(cars_table.Origin_Region,'Japan'));
%geom_label() takes the same arguments as text().
%'BackgroundColor','EdgeColor' and 'Color' can be set to 'auto'
g.geom_label('VerticalAlignment','middle','HorizontalAlignment','center','BackgroundColor','auto','Color','w');
g.geom_label('VerticalAlignment','middle','HorizontalAlignment','center','BackgroundColor','auto','Color','k');
g.set_limit_extra([0.2 0.2],[0.1 0.1]);
g.set_names('color','Manufacturer','x','Horsepower','y','Acceleration');
g.set_color_options('map','brewer2')
g.draw();


Expand Down
Binary file modified gramm cheat sheet.pdf
Binary file not shown.
44 changes: 37 additions & 7 deletions html/examples.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<!--
This HTML was auto-generated from MATLAB code.
To make changes, update the MATLAB code and republish this document.
--><title>gramm examples</title><meta name="generator" content="MATLAB 9.0"><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/"><meta name="DC.date" content="2016-09-19"><meta name="DC.source" content="examples.m"><style type="text/css">
--><title>gramm examples</title><meta name="generator" content="MATLAB 9.0"><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/"><meta name="DC.date" content="2016-09-29"><meta name="DC.source" content="examples.m"><style type="text/css">
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}:focus{outine:0}ins{text-decoration:none}del{text-decoration:line-through}table{border-collapse:collapse;border-spacing:0}

html { min-height:100%; margin-bottom:1px; }
Expand Down Expand Up @@ -288,6 +288,7 @@
g(1,1).geom_point();
<span class="comment">%'patch_opts' can be used to provide more options to the patch() internal</span>
<span class="comment">%call</span>
g(1,1).set_point_options(<span class="string">'base_size'</span>,2)
g(1,1).stat_ellipse(<span class="string">'type'</span>,<span class="string">'95percentile'</span>,<span class="string">'geom'</span>,<span class="string">'area'</span>,<span class="string">'patch_opts'</span>,{<span class="string">'FaceAlpha'</span>,0.1,<span class="string">'LineWidth'</span>,2});
g(1,1).set_title(<span class="string">'stat_ellispe()'</span>);

Expand Down Expand Up @@ -315,12 +316,25 @@
g(2,1).set_names(<span class="string">'column'</span>,<span class="string">'grp'</span>,<span class="string">'color'</span>,<span class="string">'count'</span>);
g(2,1).set_title(<span class="string">'stat_bin2d(''geom'',''image'')'</span>);

g(2,2)=gramm(<span class="string">'x'</span>,x,<span class="string">'y'</span>,y,<span class="string">'color'</span>,test);
g(2,2).geom_point(<span class="string">'alpha'</span>,0.05);
g(2,2).set_point_options(<span class="string">'base_size'</span>,6);
g(2,2).set_title(<span class="string">'geom_point(''alpha'',0.05)'</span>);

g.set_title(<span class="string">'Visualization of 2D densities'</span>);

figure(<span class="string">'Position'</span>,[100 100 800 600])
g.draw();
<span class="comment">%We change the point size in the first graph a posteriori</span>
set([g(1,1).results.geom_point_handle],<span class="string">'MarkerSize'</span>,2);
</pre><pre class="codeoutput">
ans =

gramm with properties:

legend_axe_handle: []
title_axe_handle: []
facet_axes_handles: []
results: [1x1 struct]

</pre><img vspace="5" hspace="5" src="examples_07.png" alt=""> <h2>Methods for visualizing repeated trajectories<a name="15"></a></h2><p>gramm supports 2D inputs for X and Y data (as 2D array or cell of arrays), which is particularly useful for representing repeated trajectories. Here for example we generate 50 trajectories, each of length 40. The grouping data is then given per trajectory and not per data point. Here the color grouping variable is thus given as a 1x50 cellstr.</p><pre class="codeinput"><span class="comment">%We generate 50 trajectories of length 40, with 3 groups</span>
N=50;
nx=40;
Expand Down Expand Up @@ -688,9 +702,10 @@
<span class="string">'label'</span>,cars_table.ModelShort,<span class="string">'color'</span>,cars_table.Manufacturer,<span class="string">'subset'</span>,strcmp(cars_table.Origin_Region,<span class="string">'Japan'</span>));
<span class="comment">%geom_label() takes the same arguments as text().</span>
<span class="comment">%'BackgroundColor','EdgeColor' and 'Color' can be set to 'auto'</span>
g.geom_label(<span class="string">'VerticalAlignment'</span>,<span class="string">'middle'</span>,<span class="string">'HorizontalAlignment'</span>,<span class="string">'center'</span>,<span class="string">'BackgroundColor'</span>,<span class="string">'auto'</span>,<span class="string">'Color'</span>,<span class="string">'w'</span>);
g.geom_label(<span class="string">'VerticalAlignment'</span>,<span class="string">'middle'</span>,<span class="string">'HorizontalAlignment'</span>,<span class="string">'center'</span>,<span class="string">'BackgroundColor'</span>,<span class="string">'auto'</span>,<span class="string">'Color'</span>,<span class="string">'k'</span>);
g.set_limit_extra([0.2 0.2],[0.1 0.1]);
g.set_names(<span class="string">'color'</span>,<span class="string">'Manufacturer'</span>,<span class="string">'x'</span>,<span class="string">'Horsepower'</span>,<span class="string">'y'</span>,<span class="string">'Acceleration'</span>);
g.set_color_options(<span class="string">'map'</span>,<span class="string">'brewer2'</span>)
g.draw();


Expand All @@ -704,6 +719,16 @@
g.geom_label(<span class="string">'color'</span>,<span class="string">'k'</span>,<span class="string">'dodge'</span>,0.7,<span class="string">'VerticalAlignment'</span>,<span class="string">'bottom'</span>,<span class="string">'HorizontalAlignment'</span>,<span class="string">'center'</span>);
g.set_names(<span class="string">'color'</span>,<span class="string">'Origin'</span>,<span class="string">'x'</span>,<span class="string">'Year'</span>,<span class="string">'y'</span>,<span class="string">'Number of models'</span>);
g.draw();
</pre><pre class="codeoutput">
ans =

gramm with properties:

legend_axe_handle: []
title_axe_handle: []
facet_axes_handles: []
results: [1x1 struct]

</pre><img vspace="5" hspace="5" src="examples_18.png" alt=""> <img vspace="5" hspace="5" src="examples_19.png" alt=""> <h2>Superimposing gramm plots with update(): Using different groups for different stat_ and geom_ methods<a name="26"></a></h2><p>By using the method update() after a first draw() call of a gramm object, it is possible to add or remove grouping variables. Here in a first gramm plot we make a glm fit of cars Acceleration as a function of Horsepower, across all countries and number of cylinders, and change the color options so that the fit appears in grey</p><pre class="codeinput">clear <span class="string">g10</span>
figure(<span class="string">'Position'</span>,[100 100 600 450]);
g10=gramm(<span class="string">'x'</span>,cars.Horsepower,<span class="string">'y'</span>,cars.Acceleration,<span class="string">'subset'</span>,cars.Cylinders~=3 &amp; cars.Cylinders~=5);
Expand Down Expand Up @@ -1313,6 +1338,7 @@
g(1,1).geom_point();
%'patch_opts' can be used to provide more options to the patch() internal
%call
g(1,1).set_point_options('base_size',2)
g(1,1).stat_ellipse('type','95percentile','geom','area','patch_opts',{'FaceAlpha',0.1,'LineWidth',2});
g(1,1).set_title('stat_ellispe()');
Expand Down Expand Up @@ -1340,12 +1366,15 @@
g(2,1).set_names('column','grp','color','count');
g(2,1).set_title('stat_bin2d(''geom'',''image'')');
g(2,2)=gramm('x',x,'y',y,'color',test);
g(2,2).geom_point('alpha',0.05);
g(2,2).set_point_options('base_size',6);
g(2,2).set_title('geom_point(''alpha'',0.05)');
g.set_title('Visualization of 2D densities');
figure('Position',[100 100 800 600])
g.draw();
%We change the point size in the first graph a posteriori
set([g(1,1).results.geom_point_handle],'MarkerSize',2);
%% Methods for visualizing repeated trajectories
% gramm supports 2D inputs for X and Y data (as 2D array or cell of
Expand Down Expand Up @@ -1809,9 +1838,10 @@
'label',cars_table.ModelShort,'color',cars_table.Manufacturer,'subset',strcmp(cars_table.Origin_Region,'Japan'));
%geom_label() takes the same arguments as text().
%'BackgroundColor','EdgeColor' and 'Color' can be set to 'auto'
g.geom_label('VerticalAlignment','middle','HorizontalAlignment','center','BackgroundColor','auto','Color','w');
g.geom_label('VerticalAlignment','middle','HorizontalAlignment','center','BackgroundColor','auto','Color','k');
g.set_limit_extra([0.2 0.2],[0.1 0.1]);
g.set_names('color','Manufacturer','x','Horsepower','y','Acceleration');
g.set_color_options('map','brewer2')
g.draw();
Expand Down
Binary file modified html/examples.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_09.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
Binary file modified html/examples_16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_21.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_22.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_23.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/examples_25.png
Binary file modified html/examples_26.png
Binary file modified html/examples_27.png
Binary file modified html/examples_28.png
Binary file modified html/examples_29.png
Binary file modified html/examples_30.png
Binary file modified html/examples_31.png
Binary file modified html/examples_32.png
Binary file modified html/examples_33.png
Binary file modified html/examples_34.png
Binary file modified html/examples_35.png

0 comments on commit cba705a

Please sign in to comment.