<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/ggformula-another-option-for-teaching-graphics-in-r-to-beginners/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://feedproxy.google.com/~r/SASandR/~3/j2Miy37Y9vw/ggformula-another-option-for-teaching.html"> SAS and R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A previous entry (<a rel="nofollow" href="http://sas-and-r.blogspot.com/2017/07/options-for-teaching-r-to-beginners.html" target="_blank">http://sas-and-r.blogspot.com/2017/07/options-for-teaching-r-to-beginners.html</a>) <span open>describes an approach to teaching graphics in R that also “get[s] students doing powerful things quickly”, as David Robinson <a rel="nofollow" href="http://varianceexplained.org/r/teach-tidyverse/" target="_blank">suggested</a>. </span><br><span open><br></span><span open>In this guest blog entry, <a rel="nofollow" href="http://www.calvin.edu/~rpruim/" target="_blank">Randall Pruim</a> offers an alternative way based on a different formula interface. Here’s Randall: </span><br><span open><br></span><span open>For a number of years I and several of my colleagues have been teaching R to beginners using an approach that includes a combination of</span>
<ul open>
<li>the <code>lattice</code> package for graphics,</li>
<li>several functions from the <code>stats</code> package for modeling (e.g., <code>lm(), t.test()</code>), and</li>
<li>the <code>mosaic</code> package for numerical summaries and for smoothing over edge cases and inconsistencies in the other two components.</li>
</ul>
<p><span open>Important in this approach is the syntactic similarity that the following “formula template” brings to all of these operations.  </span>
<ul open><center><br>
<h2><strong><span>goal</span> ( <span>y</span> ~ <span>x</span> , data = <span>mydata</span>, … )</strong> </h2>
<p>
<div open><span open>Many data analysis operations can be executed by filling in four pieces of information (goal, y, x, and mydata) with the appropriate information for the desired task. This allows students to become fluent quickly with a powerful, coherent toolkit for data analysis.</span><br><span open><br></span><span open>Trouble in paradise</span><br><span open>As the earlier post noted, the use of</span><span open> </span><code>lattice</code><span open> </span><span open>has some drawbacks. While basic graphs like histograms, boxplots, scatterplots, and quantile-quantile plots are simple to make with</span><span open> </span><code>lattice</code><span open>, it is challenging to combine these simple plots into more complex plots or to plot data from multiple data sources. Splitting data into subgroups and either overlaying with multiple colors or separating into sub-plots (facets) is easy, but the labeling of such plots is not as convenient (and takes more space) than the equivalent plots made with</span><span open> </span><code>ggplot2</code><span open>. And in our experience, students generally find the look of</span><span open> </span><code>ggplot2</code><span open> </span><span open>graphics more appealing.</span>
<div open>
<div>On the other hand, introducing <code>ggplot2</code> into a first course is challenging. The syntax tends to be more verbose, so it takes up more of the limited space on projected images and course handouts. More importantly, the syntax is entirely unrelated to the syntax used for other aspects of the course. For those adopting a “Less Volume, More Creativity” approach, <code>ggplot2</code> is tough to justify.</div>
<div><span>ggformula: The third-and-a half way</span></div>
</div>
<div open>
<div>Danny Kaplan and I recently introduced <code><a rel="nofollow" href="https://cran.r-project.org/web/packages/ggformula/index.html" target="_blank">ggformula</a></code>, an R package that provides a formula interface to <code>ggplot2 </code>graphics. Our hope is that this provides the best aspects of <code>lattice</code> (the formula interface and lighter syntax) and <code>ggplot2</code> (modularity, layering, and better visual aesthetics).</div>
<div>For simple plots, the only thing that changes is the name of the plotting function. Each of these functions begins with <code>gf</code>. Here are two examples, either of which could replace the side-by-side boxplots made with <code>lattice</code> in the previous post.</div>
<div><a rel="nofollow" href="https://i2.wp.com/3.bp.blogspot.com/-2pMMPlQHZLQ/WcF1KVxYOgI/AAAAAAAACKY/MAhc99Cf6P8ixECtfk5HNgf4iT207teywCLcBGAs/s1600/Screen%2BShot%2B2017-09-19%2Bat%2B3.50.30%2BPM.png?ssl=1" target="_blank"><img border="0" data-original-height="446" src="https://i2.wp.com/3.bp.blogspot.com/-2pMMPlQHZLQ/WcF1KVxYOgI/AAAAAAAACKY/MAhc99Cf6P8ixECtfk5HNgf4iT207teywCLcBGAs/s320/Screen%2BShot%2B2017-09-19%2Bat%2B3.50.30%2BPM.png?resize=450%2C446&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div>We can even overlay these two types of plots to see how they compare. To do so, we simply place what I call the “then” operator (<code>%>%</code>, also commonly called a pipe) between the two layers and adjust the transparency so we can see both where they overlap.</div>
<div><a rel="nofollow" href="https://i2.wp.com/4.bp.blogspot.com/-KDRI-On39Yw/WcF1T5Fa5XI/AAAAAAAACKc/FySVrRkXRDEeCI24RAYecvgntfuu-D7YQCLcBGAs/s1600/Screen%2BShot%2B2017-09-19%2Bat%2B3.51.25%2BPM.png?ssl=1" target="_blank"><img border="0" data-original-height="438" src="https://i2.wp.com/4.bp.blogspot.com/-KDRI-On39Yw/WcF1T5Fa5XI/AAAAAAAACKc/FySVrRkXRDEeCI24RAYecvgntfuu-D7YQCLcBGAs/s320/Screen%2BShot%2B2017-09-19%2Bat%2B3.51.25%2BPM.png?resize=450%2C438&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div><span><br></span></div>
<div><span>Comparing groups</span></div>
<div>Groups can be compared either by overlaying multiple groups distinguishable by some attribute (e.g., color)</div>
<div><a rel="nofollow" href="https://i2.wp.com/1.bp.blogspot.com/-2ohXcCp05Ro/WcF1fDckAwI/AAAAAAAACKg/etzCsf8DhYIrfO_S9GdQe-i4cgtysHIyQCLcBGAs/s1600/Screen%2BShot%2B2017-09-19%2Bat%2B3.52.06%2BPM.png?ssl=1" target="_blank"><img border="0" data-original-height="441" src="https://i2.wp.com/1.bp.blogspot.com/-2ohXcCp05Ro/WcF1fDckAwI/AAAAAAAACKg/etzCsf8DhYIrfO_S9GdQe-i4cgtysHIyQCLcBGAs/s320/Screen%2BShot%2B2017-09-19%2Bat%2B3.52.06%2BPM.png?resize=450%2C441&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div>or by creating multiple plots arranged in a grid rather than overlaying subgroups in the same space. The <code>ggformula </code>package provides two ways to create these facets. The first uses <code>|</code> very much like <code>lattice</code> does. Notice that the <code>gf_lm()</code> layer inherits information from the the <code>gf_points()</code> layer in these plots, saving some typing when the information is the same in multiple layers.</div>
<div><a rel="nofollow" href="https://i0.wp.com/4.bp.blogspot.com/-w8h0SV59xWo/WcF1o6epB6I/AAAAAAAACKk/2cNV6BY4AzcM3d7cJTR8esfdemUp7GUPACLcBGAs/s1600/Screen%2BShot%2B2017-09-19%2Bat%2B3.52.48%2BPM.png?ssl=1" target="_blank"><img border="0" data-original-height="848" src="https://i2.wp.com/4.bp.blogspot.com/-w8h0SV59xWo/WcF1o6epB6I/AAAAAAAACKk/2cNV6BY4AzcM3d7cJTR8esfdemUp7GUPACLcBGAs/s320/Screen%2BShot%2B2017-09-19%2Bat%2B3.52.48%2BPM.png?resize=450%2C848&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
</div>
</div>
<div open>
<div></div>
<div>The second way adds facets with <code>gf_facet_wrap()</code> or <code>gf_facet_grid()</code> and can be more convenient for complex plots or when customization of facets is desired.</div>
<div><span>Fitting into the tidyverse work flow</span></div>
</div>
<div open>
<div><code>ggformala</code> also fits into a <a rel="nofollow" href="https://www.tidyverse.org/" target="_blank">tidyverse</a>-style workflow (arguably better than <code>ggplot2</code> itself does). Data can be piped into the initial call to a <code>ggformula</code> function and there is no need to switch between <code>%>%</code> and <code>+</code> when moving from data transformations to plot operations.</div>
<div><a rel="nofollow" href="https://i2.wp.com/3.bp.blogspot.com/-abiMaxXnflA/WcF1y-b6s0I/AAAAAAAACKo/XlkgYCkp4S4Mj4gEsSET6iLCvzLuv7ZgQCLcBGAs/s1600/Screen%2BShot%2B2017-09-19%2Bat%2B3.53.29%2BPM.png?ssl=1" target="_blank"><img border="0" data-original-height="436" src="https://i0.wp.com/3.bp.blogspot.com/-abiMaxXnflA/WcF1y-b6s0I/AAAAAAAACKo/XlkgYCkp4S4Mj4gEsSET6iLCvzLuv7ZgQCLcBGAs/s320/Screen%2BShot%2B2017-09-19%2Bat%2B3.53.29%2BPM.png?resize=441%2C436&ssl=1" data-recalc-dims="1" data-original-width="441"></a></div>
<div><span>Summary</span></div>
<div>The “Less Volume, More Creativity” approach is based on a common formula template that has served well for several years, but the arrival of <code>ggformula</code> strengthens this approach by bringing a richer graphical system into reach for beginners without introducing new syntactical structures. The full range of <code>ggplot2</code> features and customizations remains available, and the  <code>ggformula</code>  package vignettes and tutorials describe these in more detail.</div>
</div>
<p>— Randall Pruim
<div>
<a rel="nofollow" href="http://feeds.feedburner.com/~ff/SASandR?a=j2Miy37Y9vw:9903-flRFSg:yIl2AUoC8zA" target="_blank"><img border="0" src="http://feeds.feedburner.com/~ff/SASandR?d=yIl2AUoC8zA"></a> <a rel="nofollow" href="http://feeds.feedburner.com/~ff/SASandR?a=j2Miy37Y9vw:9903-flRFSg:J3aVl1i_38o" target="_blank"><img border="0" src="http://feeds.feedburner.com/~ff/SASandR?d=J3aVl1i_38o"></a> <a rel="nofollow" href="http://feeds.feedburner.com/~ff/SASandR?a=j2Miy37Y9vw:9903-flRFSg:qj6IDK7rITs" target="_blank"><img border="0" src="http://feeds.feedburner.com/~ff/SASandR?d=qj6IDK7rITs"></a> <a rel="nofollow" href="http://feeds.feedburner.com/~ff/SASandR?a=j2Miy37Y9vw:9903-flRFSg:63t7Ie-LG7Y" target="_blank"><img border="0" src="http://feeds.feedburner.com/~ff/SASandR?d=63t7Ie-LG7Y"></a> <a rel="nofollow" href="http://feeds.feedburner.com/~ff/SASandR?a=j2Miy37Y9vw:9903-flRFSg:F7zBnMyn0Lo" target="_blank"><img border="0" src="http://feeds.feedburner.com/~ff/SASandR?i=j2Miy37Y9vw:9903-flRFSg:F7zBnMyn0Lo"></a> <a rel="nofollow" href="http://feeds.feedburner.com/~ff/SASandR?a=j2Miy37Y9vw:9903-flRFSg:gIN9vFwOqvQ" target="_blank"><img border="0" src="http://feeds.feedburner.com/~ff/SASandR?i=j2Miy37Y9vw:9903-flRFSg:gIN9vFwOqvQ"></a>
</div>
<p><img alt="" width="1" src="http://feeds.feedburner.com/~r/SASandR/~4/j2Miy37Y9vw" height="1"></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://feedproxy.google.com/~r/SASandR/~3/j2Miy37Y9vw/ggformula-another-option-for-teaching.html"> SAS and R</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/RTLtWbd-k5k" height="1"></a></div></center></ul>