Skip to content

Commit e916d47

Browse files
committed
Initial commit
0 parents  commit e916d47

13 files changed

+2882
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
gramm_test.m

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Pierre Morel
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

README.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# gramm
2+
## Introduction
3+
Gramm allows to easily plot grouped data in Matlab, and is inspired by R's [ggplot2](http://ggplot2.org) library by [Hadley Wickham](http://had.co.nz)
4+
5+
With gramm, 6 lines of code are enough to create such a figure:
6+
<img src="/img/carbig_example.png" alt="gramm example" width="800">
7+
```matlab
8+
load carbig.mat %Load example dataset about cars
9+
origin_region=num2cell(org,2); %Convert origin data to a cellstr
10+
%Create a gramm object, provide x (year) and y (mpg) data
11+
%color data (region of origin) and select a subset of the data
12+
g=gramm('x',Model_Year,'y',MPG,'color',origin_region,'subset',Cylinders~=3 & Cylinders~=5,'size',5)
13+
%Set appropriate names for legends
14+
g.set_names('color','Origin','x','Year of production','y','MPG','column','# Cylinders')
15+
%Subdivide the data in subplots horizontally by number of cylinders
16+
g.facet_grid([],Cylinders)
17+
%Plot raw data points
18+
g.geom_point()
19+
%Plot summarized data: 5 bins over x are created and for each
20+
%bin the mean and confidence interval is displayed as a shaded area
21+
g.stat_summary('geom','area','type','bootci','bin_in',5)
22+
g.draw() %Draw method
23+
```
24+
25+
## Features
26+
- Accepts x and y data as arrays, matrices or cells of arrays
27+
- Accepts grouping data as arrays or cellstr
28+
- Multiple ways of separating data by groups:
29+
- Color, point size and line type
30+
- Subplots by row and/or columns, or wrapping columns (<code>facet_grid()</code> and <code>facet_wrap()</code>)
31+
- Multiple ways of directly plotting the data:
32+
- scatter plots (<code>geom_point()</code>) and jittered scatter plot (<code>geom_jitter()</code>)
33+
- lines (<code>geom_line()</code>)
34+
- bars plots (<code>geom_bar()</code>)
35+
- raster plots (<code>geom_raster()</code>)
36+
- Multiple ways of plotting transformed data:
37+
- y data summarized by unique x values with confidence intervals (<code>stat_summary()</code>)
38+
- spline-smoothed y data with optional confidence interval (<code>stat_smooth()</code>)
39+
- histograms and density plots of x values (<code>stat_bin()</code> and <code>stat_density()</code>)
40+
- 2D binning (<code>stat_bin2d()</code>)
41+
- GLM fits (<code>stat_glm()</code>, requires statistics toolbox)
42+
- Subplots are created without too much empty space in between (and resize properly !)
43+
- Polar plots (<code>set_polar()</code>)
44+
- Confidence intervals as shaded areas, error bars or thin lines
45+
- Multiple gramm plots can be combined in the same figure by creatin a matrix of gramm objects and calling the <code>draw()</code> method on the whole matrix.
46+
- Matlabs axes properties are acessible through the method <code>axe_property()</code>
47+
- Custom legend labels with <code>set_names()</code>
48+
49+
## Examples
50+
51+
### Multiple gramm objects in a single figure
52+
Also shows histograms, categorical x values
53+
<img src="/img/multiple_gramm_example.png" alt="Multiple gramm" width="800">
54+
55+
### Fits (carbig data) ###
56+
<img src="/img/carbig_glm_example.png" alt="Multiple gramm" width="500">
57+
58+
### Continuous colors
59+
<img src="/img/continuous_colot_example.png" alt="Multiple gramm" width="500">
60+
61+
## Acknowledgements
62+
gramm was inspired and/or used code from:
63+
- [ggplot2](http://ggplot2.org)
64+
- [Panda](http://www.neural-code.com/index.php/panda) for color conversion
65+
- [subtightplot](http://www.mathworks.com/matlabcentral/fileexchange/39664-subtightplot) for subplot creation

examples.m

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
%% Generate fake data
2+
3+
N=400
4+
5+
%Create a x and y data
6+
x=linspace(0,100,N);
7+
y=sin(x/10)+randn(1,N)*0.5;
8+
9+
%Create groups
10+
twoalt=repmat([1 2],1,N/2);
11+
twoaltb=repmat([1 1 2 2],1,N/4);
12+
twoaltc=repmat({'A' 'B'},1,N/2);
13+
twoaltcb=repmat({'one' 'one' 'two' 'two'},1,N/4);
14+
twoaltcc=repmat({'1' '1' '2' '1'},1,N/4);
15+
fouraltc=repmat({'alpha' 'beta' 'gamma' 'epsilon'},1,N/4);
16+
eightaltc=repmat({'I' 'II' 'III' 'IV' 'V' 'VI' 'VII' 'VIII'},1,N/8)
17+
18+
%Change data between groups
19+
y(twoalt==1)=y(twoalt==1)+3
20+
x(twoaltb==1)=x(twoaltb==1)+50;
21+
22+
%% Example use
23+
24+
figure
25+
g=gramm('x',x,'y',y,'color',fouraltc,'linestyle',twoaltcb)
26+
g.facet_grid(twoaltcb,twoaltc,'scales','fixed')
27+
g.geom_point()
28+
g.stat_smooth('lambda',1000,'geom','area')
29+
%It's possible to set native axis properties
30+
g.axe_property('XGrid','on')
31+
g.axe_property('YGrid','on')
32+
g.draw()
33+
34+
%% Plot multiple gramm objects in single window
35+
36+
%Just create an array of gramm objects, each graph being a different
37+
%element (they stay independent), rows will be rows, columns will be
38+
%columns, starting from top left
39+
40+
clear g
41+
42+
g(1,1)=gramm('x',x,'y',y,'color',fouraltc)
43+
g(1,1).facet_grid(twoaltc,twoaltcb) %,'scales','independent'
44+
g(1,1).stat_smooth('lambda',1000,'geom','area')
45+
g(1,1).geom_point()
46+
47+
g(1,2)=gramm('x',y,'y',x,'color',twoaltc)
48+
g(1,2).geom_point()
49+
50+
% X data can be a cellstr, data will be treated as being categorical
51+
g(2,1)=gramm('x',fouraltc,'y',y,'color',twoaltcb,'size',4)
52+
g(2,1).facet_grid(twoaltc,[],'scales','fixed')
53+
g(2,1).geom_jitter('width',0.2,'height',0) %We can jitter the points in the scatter plot to make the density more apparent
54+
55+
g(2,2)=gramm('x',y,'color',twoaltc)
56+
g(2,2).stat_bin('geom','bar') %Using stat_bin we can create histograms
57+
58+
%And call the draw function on the whole array !
59+
g.draw()
60+
61+
62+
%% Different scaling options for faceting
63+
clear g
64+
65+
g(1,1)=gramm('x',x,'y',y,'color',twoaltcb)
66+
g(1,1).facet_grid(twoaltc,twoaltcb,'scales','fixed') %Same x and y scale for all facets
67+
g(1,1).stat_smooth('lambda',1000,'geom','area')
68+
g(1,1).geom_point()
69+
70+
g(1,2)=gramm('x',x,'y',y,'color',twoaltcb)
71+
g(1,2).facet_grid(twoaltc,twoaltcb,'scales','free_x') %Facets on the same columns have the same x scale
72+
g(1,2).stat_smooth('lambda',1000,'geom','area')
73+
g(1,2).geom_point()
74+
75+
g(2,1)=gramm('x',x,'y',y,'color',twoaltcb)
76+
g(2,1).facet_grid(twoaltc,twoaltcb,'scales','free_y') %Facets on the same rows have the same y scale
77+
g(2,1).stat_smooth('lambda',1000,'geom','area')
78+
g(2,1).geom_point()
79+
80+
g(2,2)=gramm('x',x,'y',y,'color',twoaltcb)
81+
g(2,2).facet_grid(twoaltc,twoaltcb,'scales','independent') %Scales are independent on each facet
82+
g(2,2).stat_smooth('lambda',1000,'geom','area')
83+
g(2,2).geom_point()
84+
85+
g.draw()
86+
87+
%% Example from the readme
88+
89+
clear g
90+
load carbig.mat %Load example dataset about cars
91+
origin_region=num2cell(org,2); %Convert origin data to a cellstr
92+
%Create a gramm object, provide x (year) and y (mpg) data
93+
%color data (region of origin) and select a subset of the data
94+
g=gramm('x',Model_Year,'y',MPG,'color',origin_region,'subset',Cylinders~=3 & Cylinders~=5,'size',5)
95+
%Set appropriate names for legends
96+
g.set_names('color','Origin','x','Year of production','y','MPG','column','# Cylinders')
97+
%Subdivide the data in subplots horizontally by number of cylinders
98+
g.facet_grid([],Cylinders)
99+
%Plot raw data points
100+
g.geom_point()
101+
%Plot summarized data: 5 bins over x are created and for each
102+
%bin the mean and confidence interval is displayed as a shaded area
103+
g.stat_summary('geom','area','type','bootci','bin_in',5)
104+
g.draw() %Draw method
105+
106+
%% Example of glm fit
107+
108+
load carbig.mat %Load example dataset about cars
109+
110+
g=gramm('x',Horsepower,'y',Acceleration,'color',Cylinders,'subset',Cylinders~=3 & Cylinders~=5)
111+
g.set_names('color','# Cylinders','x','Horsepower','y','Acceleration')
112+
g.stat_glm('geom','area') %Linear fit (default for stat_glm
113+
g.geom_point()
114+
g.draw()
115+
116+
117+
%% When there are too many colors, we switch to a continuous scale
118+
119+
load spectra.mat
120+
121+
%Here we create x as a 1xN array (see Description), and use a MxN matrix
122+
%for y. Color applies to the M rows of y.
123+
g=gramm('x',900:2:1700,'y',NIR,'color',octane);
124+
g.set_names('x','Wavelength (nm)','y','NIR','color','octane')
125+
g.geom_line;
126+
g.draw;
127+

0 commit comments

Comments
 (0)