Gramm is a powerful plotting toolbox which allows to quickly create complex, publication-quality figures in Matlab, and is inspired by R's ggplot2 library by Hadley Wickham. As a reference to this inspiration, gramm stands for GRAMmar of graphics for Matlab.
Gramm is a data visualization toolbox for Matlab that allows to produce publication-quality plots from grouped data easily and flexibly. Matlab can be used for complex data analysis using a high-level interface: it supports mixed-type tabular data via tables, provides statistical functions that accept these tables as arguments, and allows users to adopt a split-apply-combine approach (Wickham 2011) with rowfun()
. However, the standard plotting functionality in Matlab is mostly low-level, allowing to create axes in figure windows and draw geometric primitives (lines, points, patches) or simple statistical visualizations (histograms, boxplots) from numerical array data. Producing complex plots from grouped data thus requires iterating over the various groups in order to make successive statistical computations and low-level draw calls, all the while handling axis and color generation in order to visually separate data by groups. The corresponding code is often long, not easily reusable, and makes exploring alternative plot designs tedious.
Inspired by ggplot2 (Wickham 2009), the R implementation of "grammar of graphics" principles (Wilkinson 1999), gramm improves Matlab's plotting functionality, allowing to generate complex figures using high-level object-oriented code.Gramm has been used in several publications in the field of neuroscience, from human psychophysics (Morel et al. 2017), to electrophysiology (Morel et al. 2016; Ferrea et al. 2017), human functional imaging (Wan et al. 2017) and animal training (Berger et al. 2017).
Gramm has been published in the Journal of Open Source Software. If you use gramm plots in a publication you can thus cite it using the following:
Morel, (2018). Gramm: grammar of graphics plotting in Matlab. Journal of Open Source Software, 3(23), 568, https://doi.org/10.21105/joss.00568
Tested under Matlab 2014b+ versions. With pre-2014b versions, gramm forces 'painters'
, renderer to avoid some graphic bugs, which deactivates transparencies (use non-transparent geoms, for example stat_summary('geom','lines')
). The statistics toolbox is required for some methods: stat_glm()
, some stat_summary()
methods, stat_density()
. The curve fitting toolbox is required for stat_fit()
.
Download the gramm toolbox from GitHub ("Clone or download" button>download ZIP) or clone it, and add the folder containing the @gramm class folder to your Matlab path (using the GUI or addpath()
)
examples.m
, exported for preview in html/examples.htmldoc gramm
to find links to the documentation of each method.The typical workflow to generate a figure with gramm is the following:
For example, with gramm, 7 lines of code are enough to create the figure below from the carbig
dataset. Here the figure represents the evolution of fuel economy of new cars in time, with number of cylinders indicated by color, and regions of origin separated across subplot columns:
load carbig.mat %Load example dataset about cars
origin_region=num2cell(org,2); %Convert origin data to a cellstr
% Create a gramm object, provide x (year of production) and y (fuel economy) data,
% color grouping data (number of cylinders) and select a subset of the data
g=gramm('x',Model_Year,'y',MPG,'color',Cylinders,'subset',Cylinders~=3 & Cylinders~=5)
% Subdivide the data in subplots horizontally by region of origin
g.facet_grid([],origin_region)
% Plot raw data as points
g.geom_point()
% Plot linear fits of the data with associated confidence intervals
g.stat_glm()
% Set appropriate names for legends
g.set_names('column','Origin','x','Year of production','y','Fuel economy (MPG)','color','# Cylinders')
%Set figure title
g.set_title('Fuel economy of new cars between 1970 and 1982')
% Do the actual drawing
g.draw()
To export figures in a vector-based format, use the SVG or PDF option rather than EPS. SVG can be read by all vector editing softwares and causes less problems than EPS both for export and import (transparency support, text without cuts, etc.). gramm has a convenient export()
method that can be called after draw()
and maintains correct dimensions/aspect ratio. The 'alpha'
option for geom_line()
and geom_point()
is not supported by Matlab for exports.
Accepts X Y and Z data as arrays, matrices or cells of arrays
Accepts grouping data as arrays or cellstr.
Multiple ways of separating groups of data:
'color'
, 'lightness'
, 'marker'
, 'linestyle'
, 'size'
)facet_grid()
and facet_wrap()
). Multiple options for consistent axis limits across facets, rows, columns, etc. (using 'scale'
and 'space'
)fig()
)Multiple ways of directly plotting the data:
geom_point()
) and jittered scatter plot (geom_jitter()
)geom_line()
)geom_interval()
)geom_bar()
)geom_raster()
)geom_label()
)point_count()
)Multiple ways of plotting statistics on the data:
stat_summary()
)stat_bin()
and stat_density()
)stat_boxplot()
)stat_violin()
)stat_qq()
) of x data distribution against theoretical distribution or y data distribution.stat_smooth()
)stat_bin2d()
)stat_glm()
, requires statistics toolbox)stat_fit()
)stat_ellipse()
)When Z data is provided in the call to gramm()
, geom_point()
and geom_line()
generate 3D plots
Subplots are created without too much empty space in between (and resize properly !)
Polar coordinates (set_polar()
)
Color data can also be displayed as a continous variable, not as a grouping factor (set_continuous_color()
)
X and Y axes can be flipped to get horizontal statistics visualizations (coord_flip()
)
Color generation can be customized in the LCH color space, or can use alternative colormaps (Matlab's default, colorbrewer2), or provide a custom colormap (set_color_options()
)
Marker shapes and sizes can be customized with set_point_options()
Line styles and width can be customized with set_line_options()
Text elements aspect can be customized with set_text_options()
Parameters of stat_
functions (alpha level, N bootstraps) can be modified with set_stat_options()
The ordering of grouping variables can be changed between native, sorted, or custom (set_order_options
)
Confidence intervals as shaded areas, error bars or thin lines
Set the width and dodging of graphical elements in geom_
functions, stat_bin()
, stat_summary()
, and stat_boxplot()
, with 'width'
and 'dodge'
arguments
The member structure results
contains the results of computations from stat_
plots as well as graphic handles for all plotted elements
Figure title (set_title()
)
Multiple gramm plots can be combined in the same figure by creating a matrix of gramm objects and calling the draw()
method on the whole matrix. An overarching title can be added by calling set_title()
on the whole matrix.
Different groupings can be used for different stat_
and geom_
layers with the update()
method
Matlabs axes properties are acessible through the method axe_property()
Custom legend labels with set_names()
Plot reference line on the plots with geom_abline()
, geom_vline()
,geom_hline()
Plot reference polygons on the plots with geom_polygon()
Date ticks with set_datetick()
Gramm works best with table-like data: separate variables / structure fields / table columns for the variables of interest, with each variable having as many elements as observations.
The code for the following figures and many others is in examples.m
.
All the mappings presented below can be combined.
Note that we by using Origin as a faceting variable, we visualize exactly the same quantities as in the figure above.
Here the variable given as Y is a Nx1 cell of 1D arrays containing the individual trajectories. Color is given as a Nx1 cellstr.
This example highlights the potential use of gramm for neuroscientific data. Here X is a Nx1 cell containing spike trains collected over N trials. Color is given as a Nx1 cellstr.Using stat_bin()
it is possible to construct peristimulus time histograms.
With set_color_options()
With set_order_options()
By making calling the update() method after a first draw, the same axes can be reused for another gramm plot.Here this allows to plot the whole dataset in the background of each facet.
gramm was inspired and/or used code from:
Φ p value of the P - Ⅲ distribution calculated byMATLAB p rogramm ing language is high, and the divergence zone ofΦ p numerical val2 MATLAB (M...... T he p rogramm ing m ethod based on M A TLAB w as i
我正在使用Grammar::Tracer进行以下设置: p、 p6 MyGrammar.pm6: 但是没有启用跟踪。我猜是因为语法不在语句的词汇范围内?
XML-Grammar-Fortune 可通过 XML 语法来收集各种引用 (or Unix-like fortune cookies) ,通过大量丰富的元数据生成 XHTML 和纯文本信息。
Grammatica是一个C#和Java的语法剖析器生成器(Parser Generator或叫作编译器的编译器:Compiler Complier) 。它相对于其它一些类似的工具如yacc和ANTLR有了更好的改进。这是因为Grammatica: 1.创建了更好的注释和易读的源代码. 2.拥有错误自动恢复并能够详述错误信息. 3.支持语法/词法测试与调试.
一个语言的grammar说明,可以用于编译器创造抽象语法树(AST): Python 有自己的grammar specification, 但是我在JavaScript 官方文档,没搜索到: https://udn.realityripple.com/search?q=Grammar+specification 但是一般语言都应该有自己的grammar说明,请问js的在哪里可以找到呢? === 比