{"id":6671,"date":"2016-04-22T09:01:51","date_gmt":"2016-04-22T13:01:51","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6671"},"modified":"2016-05-03T10:58:19","modified_gmt":"2016-05-03T14:58:19","slug":"gramm","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/04\/22\/gramm\/","title":{"rendered":"GRAMM"},"content":{"rendered":"<div class=\"content\">\r\n\r\n<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/54465-gramm--complete-data-visualization-toolbox--ggplot2-r-like-\">gramm<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/902225\">Pierre Morel<\/a>.\r\n\r\n&nbsp;\r\n<h3>Contents<\/h3>\r\n<div>\r\n<ul>\r\n\t<li><a href=\"#1\">Grouped Plotting<\/a><\/li>\r\n\t<li><a href=\"#2\">Simple Example<\/a><\/li>\r\n\t<li><a href=\"#5\">Suggestions<\/a><\/li>\r\n\t<li><a href=\"#6\">Comments<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n<h3>Grouped Plotting<a name=\"1\"><\/a><\/h3>\r\nHave you ever wanted to plot things based on a group or a category? If so, Pierre's gramm should likely be your friend.\r\nGramm stands for \"Grammar of Graphics for MATLAB\" which is inspired from <a href=\"http:\/\/www.springer.com\/us\/book\/9780387245447\">this book<\/a>. This tool allows for plotting and statistics based on grouping data.\r\n<h3>Simple Example<a name=\"2\"><\/a><\/h3>\r\nHere is a simple example using fuel economy data that I have stored in a table called <i>CarData<\/i>. We're going to fit fuel economy to rated horsepower based on whether it is a car or a truck operating in the city or on\r\nthe highway.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">g = gramm(<span style=\"color: #a020f0;\">'x'<\/span>,CarData.RatedHP',<span style=\"color: #a020f0;\">'y'<\/span>,CarData.MPG,<span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'Color'<\/span>,CarData.City_Highway,<span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'Marker'<\/span>,CarData.Car_Truck);\r\nfacet_grid(g,CarData.City_Highway,CarData.Car_Truck,<span style=\"color: #a020f0;\">'scale'<\/span>,<span style=\"color: #a020f0;\">'fixed'<\/span>);\r\ngeom_point(g);\r\nstat_fit(g,<span style=\"color: #a020f0;\">'fun'<\/span>,@(a,b,c,x)a.*x.^b+c,<span style=\"color: #a020f0;\">'disp_fit'<\/span>,true,<span style=\"color: #a020f0;\">'StartPoint'<\/span>,[1 1 21]);\r\nset_names(g,<span style=\"color: #a020f0;\">'x'<\/span>,<span style=\"color: #a020f0;\">'Horsepower'<\/span>,<span style=\"color: #a020f0;\">'y'<\/span>,<span style=\"color: #a020f0;\">'MPG'<\/span>,<span style=\"color: #a020f0;\">'column'<\/span>,<span style=\"color: #a020f0;\">'Vehicle class'<\/span>,<span style=\"color: #a020f0;\">'color'<\/span>,<span style=\"color: #a020f0;\">'Driving condition'<\/span>,<span style=\"color: #a020f0;\">'column'<\/span>,<span style=\"color: #a020f0;\">''<\/span>,<span style=\"color: #a020f0;\">'row'<\/span>,<span style=\"color: #a020f0;\">''<\/span>);\r\ndraw(g);<\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/maingramm\/maingramm_01.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\nWe can now look at the model results of the separate fits:\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">g.results.stat_fit.model<\/pre>\r\n<pre style=\"font-style: oblique;\">ans = \r\n     General model:\r\n     ans(x) = a.*x.^b+c\r\n     Coefficients (with 95% confidence bounds):\r\n       a =      -331.9  (-2840, 2177)\r\n       b =     0.03341  (-0.1808, 0.2476)\r\n       c =       419.4  (-2125, 2964)\r\nans = \r\n     General model:\r\n     ans(x) = a.*x.^b+c\r\n     Coefficients (with 95% confidence bounds):\r\n       a =      -82.91  (-684.2, 518.4)\r\n       b =     0.08624  (-0.3401, 0.5125)\r\n       c =       150.1  (-503.6, 803.8)\r\nans = \r\n     General model:\r\n     ans(x) = a.*x.^b+c\r\n     Coefficients (with 95% confidence bounds):\r\n       a =      -219.4  (-1350, 911.2)\r\n       b =     0.05282  (-0.1593, 0.265)\r\n       c =       326.3  (-842.9, 1496)\r\nans = \r\n     General model:\r\n     ans(x) = a.*x.^b+c\r\n     Coefficients (with 95% confidence bounds):\r\n       a =      -65.33  (-1142, 1011)\r\n       b =     0.08805  (-0.8959, 1.072)\r\n       c =       131.9  (-1041, 1305)\r\n<\/pre>\r\nRather than trying to come up with a bunch of examples on my own, I went ahead and opened <a href=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/maingramm\/grammexamples.html\">Pierre's example script<\/a> as a <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016a\/matlab\/live-scripts.html (link no longer works)\">Live Script<\/a> and exported it to html. His example script provides a lot of different examples for the comprehensive range of capabilities.\r\n\r\nIn additon to the examples, there is also a PDF \"cheat sheet\" to learn the common parameters you can add. While working with\r\n<tt>gramm<\/tt>, I kept it open the entire time.\r\n<h3>Suggestions<a name=\"5\"><\/a><\/h3>\r\n<em><b>Author Update<\/b> Pierre released a new version of <tt>gramm<\/tt> at the beginning of May 2016 that has implemented the suggestions below!\u00a0 For the third suggestion, you can now parent it to a <tt>figure<\/tt>, <tt>uitab<\/tt> or <tt>uipanel<\/tt>.<\/em>\r\n\r\nA few suggestions for Pierre:\r\n<div>\r\n<ul>\r\n\t<li><tt>gramm<\/tt> should work with the categorical data type so that you do not need to cast back to cell strings in order to group.<\/li>\r\n\t<li>In R2014b or newer, the axes handles returned by <tt>g.facet_axes_handles<\/tt> could return the handle, not the numeric version of it like in older releases. This allows for tab complete and viewing\r\nall properties.<\/li>\r\n\t<li>It would be nice to be able to specify an axes or collection of existing axes as a <i>'Parent'<\/i> so that you can use <tt>gramm<\/tt> within a user interface. I don't see any simple way to do this right now other than using <tt>copyobj<\/tt> a bunch of times on an invisible figure <tt>gramm<\/tt> creates.<\/li>\r\n<\/ul>\r\n<\/div>\r\n<h3>Comments<a name=\"6\"><\/a><\/h3>\r\nGive it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=6671#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/54465-gramm--complete-data-visualization-toolbox--ggplot2-r-like-#comments\">comment<\/a> for Pierre.\r\n\r\n<script>\/\/ <![CDATA[\r\nfunction grabCode_094bc4b16e2d48669ac6f3cc799cddf2() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='094bc4b16e2d48669ac6f3cc799cddf2 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 094bc4b16e2d48669ac6f3cc799cddf2';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Sean de Wolski';\r\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }\r\n\/\/ ]]><\/script>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\"><a><span style=\"font-size: x-small; font-style: italic;\">Get\r\nthe MATLAB code\r\n<noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\r\nPublished with MATLAB\u00ae R2016a\r\n\r\n<\/div>\r\n<!--\r\n094bc4b16e2d48669ac6f3cc799cddf2 ##### SOURCE BEGIN #####\r\n%% GRAMM\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s\r\n% pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/54465-gramm--complete-data-visualization-toolbox--ggplot2-r-like- gramm> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/902225 Pierre % Morel>.\r\n%\r\n\r\n%% Grouped Plotting\r\n% Have you ever wanted to plot things based on a group or a category?  If\r\n% so, Pierre's gramm should likely be your friend.  Gramm stands for\r\n% \"Grammar of Graphics for MATLAB\" which is inspired from\r\n% <http:\/\/www.springer.com\/us\/book\/9780387245447 this book>.  This tool\r\n% allows for plotting and statistics based on grouping data.\r\n\r\n%% Simple Example\r\n% Here is a simple example using fuel economy data that I have stored in a\r\n% table called _CarData_.  We're going to fit fuel economy to rated\r\n% horsepower based on whether it is a car or a truck operating in the city\r\n% or on the highway.\r\n\r\ng = gramm('x',CarData.RatedHP','y',CarData.MPG,...\r\n'Color',CarData.City_Highway,...\r\n'Marker',CarData.Car_Truck);\r\nfacet_grid(g,CarData.City_Highway,CarData.Car_Truck,'scale','fixed');\r\ngeom_point(g);\r\nstat_fit(g,'fun',@(a,b,c,x)a.*x.^b+c,'disp_fit',true,'StartPoint',[1 1 21]);\r\nset_names(g,'x','Horsepower','y','MPG','column','Vehicle class','color','Driving condition','column','','row','');\r\ndraw(g);\r\n\r\n%%\r\n% We can now look at the model results of the separate fits:\r\n\r\ng.results.stat_fit.model\r\n\r\n%%\r\n% Rather than trying to come up with a bunch of examples on my own, I went\r\n% ahead and opened\r\n% <https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/maingramm\/grammexamples.html % example script> as a\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2016a\/matlab\/live-scripts.html % Live Script> and exported it to html.  His example script provides a lot\r\n% of different examples for the comprehensive range of capabilities.\r\n%\r\n% In additon to the examples, there is also a PDF \"cheat sheet\" to learn\r\n% the common parameters you can add.  While working with |gramm|, I kept it\r\n% open the entire time.\r\n\r\n%% Suggestions\r\n%\r\n% _*Author Update* Pierre released a new version of |gramm| at the end of\r\n% April 2016 that has implemented the first two suggestions below!\r\n% Unfortunately, the third suggestion is really difficult to implement\r\n% because gramm likes full control of the figure._\r\n%\r\n% A few suggestions for Pierre:\r\n%\r\n% * |gramm| should work with the categorical data type so that you do not\r\n% need to cast back to cell strings in order to group.\r\n% * In R2014b or newer, the axes handles returned by |g.facet_axes_handles|\r\n% could return the handle, not the numeric version of it like in older\r\n% releases.  This allows for tab complete and viewing all properties.\r\n% * It would be nice to be able to specify an axes or collection of\r\n% existing axes as a _'Parent'_ so that you can use |gramm| within a user\r\n% interface.  I don't see any simple way to do this right now other than\r\n% using |copyobj| a bunch of times on an invisible figure |gramm| creates.\r\n\r\n%% Comments\r\n%\r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=6671#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/54465-gramm--complete-data-visualization-toolbox--ggplot2-r-like-#comments % comment> for Pierre.\r\n\r\n##### SOURCE END ##### 094bc4b16e2d48669ac6f3cc799cddf2\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/maingramm\/maingramm_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n\r\nSean's pick this week is gramm by Pierre Morel.\r\n\r\n&nbsp;\r\nContents\r\n\r\n\r\n\tGrouped Plotting\r\n\tSimple Example\r\n\tSuggestions\r\n\tComments\r\n\r\n\r\nGrouped Plotting\r\nHave you ever wanted to plot things... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/04\/22\/gramm\/\">read more >><\/a><\/p>","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6671"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=6671"}],"version-history":[{"count":7,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6671\/revisions"}],"predecessor-version":[{"id":6889,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6671\/revisions\/6889"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}