Custom layouts
Open this example in the editor - All gramm examples - set_parent() in the cheat sheet
We start by loading the sample data (structure created from the carbig dataset)
Contents
load example_data;
Plots of different sizes
gramm plots can be drawn in tiles of a tiledlayout with set_parent(layout,'tile',tile,'span',span). tile and span work as in nexttile(): tile is the number of the first tile (counted row by row, or given as [row column]) and span is the number of [rows columns] covered by the plot.
figure('Position',[100 100 1100 660]); t=tiledlayout(2,3); %Large plot on the left, spanning 2 rows and 2 columns g_large=gramm('x',cars.Horsepower,'y',cars.MPG,'color',cars.Cylinders,'subset',cars.Cylinders~=3 & cars.Cylinders~=5); g_large.set_parent(t,'tile',1,'span',[2 2]); g_large.geom_point(); g_large.stat_glm(); g_large.set_names('x','Horsepower','y','MPG','color','# Cyl'); g_large.set_title('Fuel economy vs. horsepower'); g_large.draw(); %Two smaller plots on the right g_top=gramm('x',cars.Origin_Region,'y',cars.MPG,'color',cars.Origin_Region); g_top.set_parent(t,'tile',3); g_top.stat_boxplot(); g_top.set_names('x','','y','MPG'); g_top.set_layout_options('legend',false); g_top.set_title('Per region'); g_top.draw(); g_bottom=gramm('x',cars.Model_Year,'y',cars.MPG); g_bottom.set_parent(t,'tile',6); g_bottom.stat_summary('geom','area'); g_bottom.set_names('x','Year','y','MPG'); g_bottom.set_title('Over the years'); g_bottom.draw();
Combine gramm with regular Matlab axes
The other tiles of the tiledlayout can hold any Matlab plot, created with nexttile() as usual
figure('Position',[100 100 1100 450]); t=tiledlayout(1,2); g=gramm('x',cars.Weight,'y',cars.Acceleration,'color',cars.Origin_Region); g.set_parent(t,'tile',1); g.geom_point(); g.set_names('x','Weight','y','Acceleration','color','Origin'); g.set_title('gramm plot'); g.draw(); ax=nexttile(t,2); imagesc(ax,corr([cars.Horsepower cars.Weight cars.Acceleration cars.MPG],'rows','complete')); axis(ax,'image'); colorbar(ax); ax.XTick=1:4; ax.YTick=1:4; ax.XTickLabel={'Horsepower','Weight','Acceleration','MPG'}; ax.YTickLabel=ax.XTickLabel; title(ax,'Regular Matlab axes (correlations)');
Array of gramm plots in a tile
An array of gramm plots can itself be drawn in a tile: give the tile to the whole array with set_parent()
figure('Position',[100 100 1100 660]); t=tiledlayout(1,3); clear g g(1,1)=gramm('x',cars.Horsepower,'y',cars.MPG,'color',cars.Origin_Region); g(1,1).geom_point(); g(1,1).set_title('Points'); g(2,1)=gramm('x',cars.Horsepower,'y',cars.MPG,'color',cars.Origin_Region); g(2,1).stat_glm(); g(2,1).set_title('Fits'); g.set_names('x','Horsepower','y','MPG','color','Origin'); g.set_title('Array of gramm plots spanning two tiles'); g.set_parent(t,'tile',1,'span',[1 2]); g.draw(); g2=gramm('x',cars.MPG,'color',cars.Origin_Region); g2.set_parent(t,'tile',3); g2.stat_density(); g2.set_names('x','MPG','color','Origin'); g2.set_title('Single plot'); g2.draw();
Plots in a panel, next to other interface components
set_parent() also accepts a uipanel or a uitab, e.g. to place a gramm plot next to buttons of an interface. Copy this code in the command window to run it (the result doesn't show because uicontrol/uipanel figures do not get generated automatically):
f=figure('Position',[100 100 1100 450]); uicontrol(f,'Style','pushbutton','String','Button','Units','normalized',... 'Position',[0.8 0.45 0.15 0.1]); p=uipanel(f,'Position',[0.05 0.1 0.7 0.8]); g=gramm('x',cars.Horsepower,'y',cars.MPG,'color',cars.Origin_Region); g.geom_point(); g.set_names('x','Horsepower','y','MPG','color','Origin'); g.set_parent(p); g.draw();