Skip to content

Commit

Permalink
Added geom_label() to plot text
Browse files Browse the repository at this point in the history
Added additional examples
Modified continuous color legend
  • Loading branch information
piermorel committed Sep 19, 2016
1 parent 985e9cb commit 397bbc1
Show file tree
Hide file tree
Showing 49 changed files with 422 additions and 61 deletions.
33 changes: 22 additions & 11 deletions @gramm/draw.m
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@

%If we have too many numerical values for the color we
%switch to continuous color
if length(uni_color)>15 && ~iscellstr(uni_color)
if length(uni_color)>15 && ~iscellstr(uni_color) && ~obj.continuous_color
set_continuous_color(obj);
end
end
Expand Down Expand Up @@ -222,7 +222,7 @@
n_groups=length(uni_row)*length(uni_column)*length(uni_marker)...
*length(uni_size)*length(uni_linestyle)*length(uni_color)*length(unique(temp_aes.group));
aes_names_fieldnames=fieldnames(obj.aes_names);
for fn=4:length(aes_names_fieldnames) %Starting from 4 we ignore x y and z
for fn=5:length(aes_names_fieldnames) %Starting from 4 we ignore x y z and label
obj.results.(aes_names_fieldnames{fn})=cell(n_groups,1);
obj.results.(['ind_' aes_names_fieldnames{fn}])=cell(n_groups,1);
end
Expand Down Expand Up @@ -441,6 +441,11 @@
else
draw_data.z=[];
end
if ~isempty(temp_aes.label)
draw_data.label=temp_aes.label(sel);
else
draw_data.label=[];
end
draw_data.continuous_color=temp_aes.color(sel);
draw_data.color=cmap((ind_color-1)*length(uni_lightness)+ind_lightness,:);
draw_data.marker=obj.point_options.markers{1+mod(ind_marker-1,length(obj.point_options.markers))};
Expand Down Expand Up @@ -652,14 +657,20 @@

obj.legend_y=obj.legend_y-legend_y_step; %HACK here, we have to multiply by 2 ??

% image(ones(1,length(obj.continuous_color_colormap))+0.5,...
% linspace(obj.legend_y-2,obj.legend_y,length(obj.continuous_color_colormap)),...
% reshape(obj.continuous_color_colormap,length(obj.continuous_color_colormap),1,3));

tmp_N=100;
imagesc([1 1.5],[obj.legend_y-legend_y_step*2 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.2 ; 1.8 2.2 ;1.8 2.2]',[obj.legend_y obj.legend_y;obj.legend_y-legend_y_step obj.legend_y-legend_y_step ;obj.legend_y-legend_y_step*2 obj.legend_y-legend_y_step*2 ]','Color','k','Parent',obj.legend_axe_handle)
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);

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 ;...
obj.legend_y-legend_y_step*gradient_height/2 obj.legend_y-legend_y_step*gradient_height/2 ;...
obj.legend_y-legend_y_step*3*gradient_height/4 obj.legend_y-legend_y_step*3*gradient_height/4;...
obj.legend_y-legend_y_step*gradient_height/4 obj.legend_y-legend_y_step*gradient_height/4 ;...
obj.legend_y-legend_y_step*gradient_height/2 obj.legend_y-legend_y_step*gradient_height/2 ;...
obj.legend_y-legend_y_step*3*gradient_height/4 obj.legend_y-legend_y_step*3*gradient_height/4]',...
'Color','w','Parent',obj.legend_axe_handle)


colormap(obj.continuous_color_colormap)
caxis([min(min(obj.plot_lim.minc)) max(max(obj.plot_lim.maxc))]);
Expand All @@ -671,18 +682,18 @@
'Parent',obj.legend_axe_handle)];

obj.legend_text_handles=[obj.legend_text_handles...
text(2.5,obj.legend_y-legend_y_step,num2str((max(max(obj.plot_lim.maxc))+min(min(obj.plot_lim.minc)))/2),...
text(2.5,obj.legend_y-legend_y_step*gradient_height/2,num2str((max(max(obj.plot_lim.maxc))+min(min(obj.plot_lim.minc)))/2),...
'FontName',obj.text_options.font,...
'FontSize',obj.text_options.base_size*obj.text_options.legend_scaling,...
'Parent',obj.legend_axe_handle)];

obj.legend_text_handles=[obj.legend_text_handles...
text(2.5,obj.legend_y-legend_y_step*2,num2str(min(min(obj.plot_lim.minc))),...
text(2.5,obj.legend_y-legend_y_step*gradient_height,num2str(min(min(obj.plot_lim.minc))),...
'FontName',obj.text_options.font,...
'FontSize',obj.text_options.base_size*obj.text_options.legend_scaling,...
'Parent',obj.legend_axe_handle)];

obj.legend_y=obj.legend_y-legend_y_step*3;
obj.legend_y=obj.legend_y-legend_y_step*(gradient_height+1);
end

%marker legend
Expand Down
1 change: 1 addition & 0 deletions @gramm/geom_count.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

if obj.continuous_color
disp('geom_count() unsupported with continuous color')
hndl=[];
else
[x,y]=to_polar(obj,comb(draw_data.x),comb(draw_data.y));

Expand Down
75 changes: 75 additions & 0 deletions @gramm/geom_label.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
function obj = geom_label(obj,varargin)
% geom_label Display data as labels
%
% Example syntax: gramm_object.geom_jitter('Color','auto','dodge',0.6)
% Geom label displays text (provided as 'label' in the constructor call
% gramm()). At the X and Y locations provided in the constructor call.
% Appearance of the text can be customized with any text property given as
% 'name',value pair in the arguments. The color-related arguments
% ('Color','EdgeColor','BackgroundColor') can also be set to 'auto' in
% order for the corresponding element to be colored according the the
% colro groups provided in the constructor. geom_label() also accepts a
% 'dodge' argument.



%Look in varargin
if mod(numel(varargin),2)~=0
error('Improper number of ''name'',value argument pairs')
end


obj.geom=vertcat(obj.geom,{@(dd)my_label(obj,dd,varargin)});
obj.results.geom_label_handle={};

end

function hndl=my_label(obj,draw_data,params)

if isnumeric(draw_data.label)
draw_data.label=cellfun(@(c)num2str(c),num2cell(draw_data.label),'UniformOutput',false);
end

if iscell(draw_data.x)
disp('Unsupported cell X/Y for geom_text')
hndl=[];
else


if isempty(params)
[x,y]=to_polar(obj,draw_data.x,draw_data.y);

hndl=text(x,y,draw_data.label,'Color',draw_data.color);
else

%If one of the colors is 'auto' we replace with the data-driven
%color
for k=1:2:numel(params)
if any(strcmp(params{k},{'Color','EdgeColor','BackgroundColor'})) && strcmp(params{k+1},'auto')
params{k+1}=draw_data.color;
end
end

%Find dodge option
dodge_ind=find(strcmp(params,'dodge'));
if ~isempty(dodge_ind)
dodge=params{dodge_ind+1};
params(dodge_ind:dodge_ind+1)=[];
else
dodge=0;
end
x=dodger(draw_data.x,draw_data,dodge);
[x,y]=to_polar(obj,x,draw_data.y);



hndl=text(x,y,draw_data.label,'Color',draw_data.color,params{:});
end

end

obj.results.geom_label_handle{obj.result_ind,1}={hndl};

end


4 changes: 4 additions & 0 deletions @gramm/gramm.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
aes_names=struct('x','x',...
'y','y',...
'z','z',...
'label','label',...
'color','Color',...
'marker','Marker',...
'linestyle','Line Style',...
Expand Down Expand Up @@ -141,6 +142,7 @@
% - 'x' for the data to plot as abcissa, or the data that
% will be used to construct histograms/density estimates
% - 'y' for the data to plot as ordinate
% - 'label' for the label text
% - 'color' for the data that determines color (hue)
% - 'lightness' for the data that determines lightness
% - 'linestyle' for the data that determines line style
Expand All @@ -167,6 +169,7 @@
% array of the same size (N*M). It can also be a 1D numerical
% array of size M, in which case the same abcissa will be
% used for every row of y.
% - label should be a 1D cell array of strings of length N.

obj.aes=parse_aes(varargin{:});
obj.handle_graphics=~verLessThan('matlab','8.4.0');
Expand Down Expand Up @@ -217,6 +220,7 @@
obj=geom_raster(obj,varargin)
obj=geom_bar(obj,varargin)
obj=geom_interval(obj,varargin)
obj=geom_label(obj,varargin)

% stat methods
obj=stat_smooth(obj,varargin)
Expand Down
1 change: 1 addition & 0 deletions @gramm/private/parse_aes.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
my_addParameter(p,'ymin',[]);
my_addParameter(p,'ymax',[]);
my_addParameter(p,'z',[]);
my_addParameter(p,'label',[]);

% Other aesthetics are string-value pairs
my_addParameter(p,'color',[]);
Expand Down
2 changes: 1 addition & 1 deletion @gramm/private/validate_aes.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

%Missing fields are replaced with arrays of ones
for k=1:length(fields)
if isempty(aes.(fields{k})) && sum(strcmp(fields{k},{'z' 'ymin' 'ymax'}))==0 %If z, ymin, ymax are empty we leave them empty
if isempty(aes.(fields{k})) && sum(strcmp(fields{k},{'z' 'ymin' 'ymax' 'label'}))==0 %If z, ymin, ymax are empty we leave them empty

out.(fields{k})=ones(aes_length,1);
if strcmp(fields{k},'subset') %Or array of true for 'subset'
Expand Down
6 changes: 3 additions & 3 deletions @gramm/redraw.m
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
end
end

if ~isempty(legend_text_pos) && obj.with_legend
if ~isempty(legend_text_pos) % && obj.with_legend
%Here we correct by the width to get the coordinates in
%normalized values
[max_text_x,max_ind]=max(cellfun(@(p)legend_pos(1)+legend_pos(3)*(p(1)+p(3))/legend_axis_width,legend_text_pos));
Expand Down Expand Up @@ -252,8 +252,8 @@
max_facet_y_text=max(max_facet_y_text,max_facet_y);

%If we don't have legends on the right then we use the multi parameters
temp_available_x=obj.multi.orig(2)+obj.multi.size(2);
if ~isempty(legend_text_pos) && obj.with_legend %Place relative to legend axis if we have one
temp_available_x=obj.multi.orig(2)+obj.multi.size(2)-spacing_w;
if ~isempty(legend_text_pos)% && obj.with_legend %Place relative to legend axis if we have one
tmp=get(obj.legend_axe_handle,'Position');
temp_available_x=tmp(1);
end
Expand Down
1 change: 1 addition & 0 deletions @gramm/stat_glm.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@

else
warning('Not enough points for linear fit')
obj.results.stat_glm{obj.result_ind,1}=[];
end

end
1 change: 1 addition & 0 deletions @gramm/update.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

obj.updater.updated=true;
obj.updater.facet_updated=false;
obj.with_legend=true;

%Remove ablines
obj.abline=[];
Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ Type <code>doc gramm</code> to find links to the documentation of each method.
- confidence intervals (<code>geom_interval()</code>)
- bars plots (<code>geom_bar()</code>)
- raster plots (<code>geom_raster()</code>)
- labels (<code>geom_label()</code>)
- point counts (<code>point_count()</code>)


- Multiple ways of plotting statistics on the data:
- y data summarized by x values (uniques or binned) with confidence intervals (<code>stat_summary()</code>)
- histograms and density plots of x values (<code>stat_bin()</code> and <code>stat_density()</code>)
Expand Down Expand Up @@ -155,26 +157,30 @@ Using <code>stat_bin()</code> it is possible to construct peristimulus time hist

<img src="/html/examples_10.png" alt="facet_grid() options" width="800">

### Text labels with geom_label() ###

<img src="/html/examples_19.png" alt="geom_label()" width="800">

### Colormap customization ###
With <code>set_color_options()</code>

<img src="/html/examples_20.png" alt="Colormaps example" width="800">
<img src="/html/examples_24.png" alt="Colormaps example" width="800">

### Continuous colors

<img src="/html/examples_21.png" alt="Continuous colors" width="800">
<img src="/html/examples_25.png" alt="Continuous colors" width="800">

###Reordering of categorical variables
With <code>set_order_options()</code>

<img src="/html/examples_22.png" alt="Reordering" width="800">
<img src="/html/examples_26.png" alt="Reordering" width="800">


### Superimposition of gramm objects on the same axes
By making calling the update() method after a first draw, the same axes can be reused for another gramm plot.
Here this allows to use different groupings for the points and for the glm fit.
Here this allows to use plot the whole dataset in the background of each facet.

<img src="/html/examples_19.png" alt="gramm superimposition" width="600">
<img src="/html/examples_22.png" alt="gramm superimposition" width="800">

## Acknowledgements
gramm was inspired and/or used code from:
Expand Down
Loading

0 comments on commit 397bbc1

Please sign in to comment.