<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/data-driven-cheatsheets/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.r-statistics.com/2017/01/data-driven-cheatsheets/"> R – R-statistics blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Guest post by <a target="_blank" rel="nofollow" href="mailto:yonis@metrumrg.com">Jonathan Sidi</a></p>
<p>Cheatsheets are currently built and used exclusivley as a teaching tool. We want to try and change this and produce a cheat sheet that gives a roadmap to build a known product, but also is built as a function so users can input data into it to make the cheatsheet more personalized. This gives a versalility of a consistent format that people can share with each other, but has the added value of conveying a message through data driven visual changes.</p>
<div>
<h2>Example</h2>
<div>
<h3>ggplot2 themes</h3>
<p>The ggplot2 theme object is an amazing object you can specify <em>nearly</em> any part of the plot that is not conditonal on the data. What sets the theme object apart is that its structure is consistent, but the values in it change. In addition to change a theme it is a single function that too has a consistent call. The reoccuring challenge for users is to remember all the options that can be used in the theme call (there are approximately 220 unique options to calibrate at last count) or bookmark the <a target="_blank" rel="nofollow" href="http://docs.ggplot2.org/current/theme.html">help page</a> for the theme and remember how you deciphered it last time.</p>
<p align="center"><img alt="" src="https://i2.wp.com/p.fod4.com/p/media/5c597eb60b/ZcfqBkqCSoeGmTSftrgo_Confused%20Joey%20Friends.gif?w=840" data-recalc-dims="1"></p>
<p>This becomes a problem to pass all the information of the theme to someone who does not know what the values are set in your theme and attach instructions on it to let them recreate it without needing to open any web pages.</p>
<p>In writing the library <a target="_blank" rel="nofollow" href="https://github.com/metrumresearchgroup/ggedit">ggedit</a> we tried to make it easy to edit your theme so you don’t have to know too much about ggplots to make a large number of changes at once, for a quick clip see <a target="_blank" rel="nofollow" href="http://www.youtube.com/watch?v=813QxbsrvLM">here</a>. We had to make it easy to track those changes for people who are not versed in R, and plot.theme() was the outcome. In short think of the theme as a lot of small images that are combined to create a <a target="_blank" rel="nofollow" href="http://mosaically.com/embed/da1df7c7-7c9e-4c95-875b-294192cdeed9">singel portrait</a>.</p>
<p><span></span></p>
<p></p>
</div>
<div>
<h3>Converting a theme call to a single image</h3>
<pre><code>theme(themeType.element=elementClass(elementClassArgument=value))</code></pre>
<ul>
<li><strong>theme()</strong>: ggplot2 theme function</li>
<li><strong>themeType</strong>: general part of the theme
<ul>
<li>line,rect, text, axis,legend, panel, strip, plot</li>
</ul>
</li>
<li><strong>element</strong>: element if a <strong>themeType</strong>
<ul>
<li>ie <strong>themeType</strong>=axis and <strong>element</strong>=ticks is written as axis.ticks</li>
</ul>
</li>
<li><strong>elementClass</strong>: each element is classified to an element_class() which is function that controls characteristics for the class
<ul>
<li>eg element_line(size,linetype,lineend,colour,arrow)</li>
</ul>
</li>
<li><strong>elementClassArgument</strong>: characteristic of an element which are arguments of an element_class function</li>
<li><strong>value</strong>: the value given to an <strong>elementClassArgument</strong></li>
</ul>
<p>There are themeType.elements that are not classified in a specific class thus are given values directly, like legend.</p>
<pre><code>theme(themeType.element=value)</code></pre>
<p>To tie this all together we can create this single template that can be replicated for any element in the theme object. To add more information to the ouput the class of the value given to an elementClassArgument (or a themeType.element) and the index to uniquely identify the element.</p>
<pre><code>library(ggedit)</code></pre>
<p><img src="https://i1.wp.com/raw.githubusercontent.com/metrumresearchgroup/ggedit/master/BlogPosts/Data%20Driven%20Cheatsheets/DataDrivenCheatsheets_files/figure-html/unnamed-chunk-2-1.png?w=480&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<h3>Grouping templates</h3>
<pre><code>plot(theme_get(),themePart = 'legend',fnt=17,plotFrame = F)</code></pre>
<p><img src="https://i0.wp.com/raw.githubusercontent.com/metrumresearchgroup/ggedit/master/BlogPosts/Data%20Driven%20Cheatsheets/DataDrivenCheatsheets_files/figure-html/unnamed-chunk-3-1.png?w=480&ssl=1" data-recalc-dims="1"></p>
<p>We add some colour to distinguish which elements are set to NULL (grey) and which ones have values (red).</p>
<p>As an example this is how to read the output</p>
<pre><code>theme(legend.key=element_rect(fill='grey95',colour='white'),
      legend.text=element_text(size=rel(0.8)),
      legend.title=element_text(hjust=0),
      legend.box.spacing=unit(0.4,'cm'),
      legend.justification='center',
      legend.position='right',
      legend.spacing=unit(0.4,'cm'))</code></pre>
</div>
<div>
<h3>Visualize the whole theme</h3>
<pre><code>plot(theme_get(),plotFrame = F)</code></pre>
<p><img src="https://i2.wp.com/raw.githubusercontent.com/metrumresearchgroup/ggedit/master/BlogPosts/Data%20Driven%20Cheatsheets/DataDrivenCheatsheets_files/figure-html/unnamed-chunk-4-1.png?w=840&ssl=1" data-recalc-dims="1"></p>
<p>Make this plot interactive by applying ggplotly from the <a target="_blank" rel="nofollow" href="https://github.com/ropensci/plotly">plotly package</a>.</p>
<pre><code>plot(theme_get(),as.plotly = T)</code></pre>
<p><iframe height="1000" width="100%" src="https://cdn.rawgit.com/metrumresearchgroup/ggedit/be20b77a/BlogPosts/Data%20Driven%20Cheatsheets/plotlyOutput.html"><br>
</iframe></p>
</div>
<div>
<h3>Create the cheatsheet</h3>
<p>Toggling the <code>plotFrame</code> argument to the plot call will nest the plots into a generic cheatsheet layout that does a better job of finding the best width for each box and supplies instructions on the border of how to read the output with a caption on the bottom which theme was used.</p>
<pre><code>plot(theme_get(),plotFrame = T,fnt = 10)</code></pre>
<p><img src="https://i1.wp.com/raw.githubusercontent.com/metrumresearchgroup/ggedit/master/BlogPosts/Data%20Driven%20Cheatsheets/DataDrivenCheatsheets_files/figure-html/unnamed-chunk-6-1.png?w=840&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<h3>Compare themes</h3>
<p>Finally there is an option to compare themes. The same layout will be given but the color coding will change, where a blue color will indicate an update from the benchmark theme.</p>
<pre><code>library(ggthemes)
plot(obj=theme_economist(),obj2 = theme_bw(),fnt = 10)</code></pre>
<p><img src="https://i0.wp.com/raw.githubusercontent.com/metrumresearchgroup/ggedit/master/BlogPosts/Data%20Driven%20Cheatsheets/DataDrivenCheatsheets_files/figure-html/unnamed-chunk-7-1.png?w=840&ssl=1" data-recalc-dims="1"></p>
<p>When collaborating with many people and large changes are made to the theme this lets you have a single language everyone can understand for quick referencing and hopefully problem solving.</p>
<hr>
<p><em> Jonathan Sidi joined Metrum Researcg Group in 2016 after working for several years on problems in applied statistics, financial stress testing and economic forecasting in both industrial and academic settings.</em></p>
<p>To learn more about additional open-source software packages developed by Metrum Research Group please visit the Metrum <a target="_blank" rel="nofollow" href="http://metrumrg.com/opensourcetools.html">website</a>.</p>
<p>Contact: For questions and comments, feel free to email me at: <a target="_blank" rel="nofollow" href="mailto:yonis@metrumrg.com">yonis@metrumrg.com</a> or open an issue in <a target="_blank" rel="nofollow" href="https://github.com/metrumresearchgroup/ggedit/issues">github</a>.</p>
</div>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.r-statistics.com/2017/01/data-driven-cheatsheets/"> R – R-statistics blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/QDGVz8YB5ns"></a></div>