|
| 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