piermorel / gramm

Gramm is a complete data visualization toolbox for Matlab. It provides an easy to use and high-level interface to produce publication-quality plots of complex data with varied statistical visualizations. Gramm is inspired by R's ggplot2 library.
MIT License
784 stars 222 forks source link

Vertical alignment of facet grids #94

Closed PierreP92 closed 4 years ago

PierreP92 commented 4 years ago

Hi Pierre,

thanks a lot for developing Gramm - it is a really useful set of functions and I recommend it to colleagues a lot!

I am encountering an issue with vertically aligning 2 facet grids. As you can see on the image attached, when the two variables have a different range of values, the width occupied by each subplot changes so the two rows are not properly aligned.

Screen Shot 2020-03-17 at 21 46 05

The core of my code looks something like this:

for i = 1:2
   g(i,1) = gramm('x',x,'y',y(:,i));
   g(i,1).facet_grid([],col,'column_labels',false,'space','fixed');
   g(i,1).stat_summary('type','sem','geom',{'area','point','errorbar'},'width',0);
end

Could you please advice on how to fix this?

Thanks a lot,

Pierre

piermorel commented 4 years ago

Hi @PierreP92 ,

By default Gramm aggressively resizes subplots so that they stay close together, even when you resize the window (this is a dynamic process). Using rows / columns of Gramm objects this dynamic resizing is independent between the Gramm objects and explains what you see.

The solution is thus to override the dynamic resizing and set subplot spacing values to satisfying ones. This is all done with set_layout_options() (see doc and examples.m). Set 'redraw' to falseand adjust margin_height, margin_width and gap. You will want to adjust these values for the final size of the figure you want, resizing won't be as nice as usual.