<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/more-tidy-evaluation-with-ggplot2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.johnmackintosh.com/2018-07-04-more-tidy-evaluation-with-ggplot2/"> HighlandR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>	Following up the follow up – </p>
<p>After my previous post, my good friend <a rel="nofollow" href="https://twitter.com/daibhaidhhen" target="_blank">David Henderson</a> asked a good question on Twitter ( it happens :))</p>
<p><img alt="2018-07-04-tweetshot.png" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-07-04-tweetshot.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>To be honest, I hadn’t even noticed there was no tilde (the ‘~’ ) in my call to facet_wrap.</p>
<p>So I did a little experiment :</p>
<div>
<div>
<pre><code><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tidyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tibble</span><span>)</span><span>
</span><span>data</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>fdeaths</span><span>,</span><span>mdeaths</span><span>,</span><span>ldeaths</span><span>)</span><span> </span><span>#time series data- needs prep</span><span>
</span><span>names</span><span>(</span><span>data</span><span>)[</span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;fdeaths&quot;</span><span>,</span><span>&quot;mdeaths&quot;</span><span>,</span><span>&quot;ldeaths&quot;</span><span>)</span><span>
</span><span>data</span><span> </span><span>&lt;-</span><span> </span><span>as_tibble</span><span>(</span><span>data</span><span>)</span><span>
</span><span>startdate</span><span> </span><span>&lt;-</span><span> </span><span>as.Date</span><span>(</span><span>'1974-1-1'</span><span>)</span><span>
</span><span>data</span><span>$</span><span>date</span><span> </span><span>&lt;-</span><span> </span><span>seq.Date</span><span>(</span><span>startdate</span><span>,</span><span>by</span><span> </span><span>=</span><span> </span><span>'month'</span><span>,</span><span>length.out</span><span> </span><span>=</span><span> </span><span>72</span><span>)</span><span>
</span><span>newdata</span><span> </span><span>&lt;-</span><span> </span><span>tidyr</span><span>::</span><span>gather</span><span>(</span><span>data</span><span>,</span><span> </span><span>key</span><span> </span><span>=</span><span> </span><span>key</span><span>,</span><span> </span><span>value</span><span> </span><span>=</span><span> </span><span>value</span><span>,</span><span>-</span><span>date</span><span>)</span><span>
</span><span>newdata</span><span>$</span><span>value</span><span> </span><span>&lt;-</span><span> </span><span>as.numeric</span><span>(</span><span>newdata</span><span>$</span><span>value</span><span>)</span><span>
</span><span># adding in an additional colum to facet by</span><span>
</span><span>newdata</span><span>$</span><span>random</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_sub</span><span>(</span><span>newdata</span><span>$</span><span>key</span><span>,</span><span>1</span><span>,</span><span>1</span><span>)</span><span>
</span><span>gtest</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>df</span><span>,</span><span>x</span><span>,</span><span>y</span><span>,</span><span>...</span><span>)</span><span> </span><span>{</span><span>
</span><span># check out the ... in the function parameters</span><span>
</span><span>x_quo</span><span> </span><span>&lt;-</span><span> </span><span>enquo</span><span>(</span><span>x</span><span>)</span><span>
</span><span>y_quo</span><span> </span><span>&lt;-</span><span> </span><span>enquo</span><span>(</span><span>y</span><span>)</span><span>
</span><span>groupvars</span><span> </span><span>&lt;-</span><span> </span><span>quos</span><span>(</span><span>...</span><span>)</span><span> </span><span>## this is a new bit too</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>df</span><span>,</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>!!</span><span>x_quo</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>!!</span><span>y_quo</span><span>))</span><span> </span><span>+</span><span> </span><span>#bangin' </span><span>
</span><span>geom_line</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;blue&quot;</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span> </span><span>geom_point</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;blue&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>groupvars</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span> </span><span>#look Ma, no tilde. Also, 2 cols, not 3</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span> </span><span>+</span><span> </span><span>ggtitle</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>&quot;Easy Tidy Eval in ggplot 3.0.0&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;ggplot with tidy evaluation &amp; facetting with no strings&quot;</span><span>)</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span> </span><span>+</span><span> </span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>NULL</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>NULL</span><span>,</span><span> </span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_bw</span><span>()</span><span>
</span><span>p</span><span>
</span><span>}</span><span>
</span><span>gtest</span><span>(</span><span>newdata</span><span>,</span><span>date</span><span>,</span><span>value</span><span>,</span><span>random</span><span>,</span><span>key</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="2018-07-04-tidy-string-free-ggplot2.png" src="https://i0.wp.com/www.johnmackintosh.com/img/2018-07-04-tidy-string-free-ggplot2.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Pretty cool huh?</p>
<p>What’s different here?</p>
<p>Well, I added in ‘…’ to the definition, and assigned groupvars using quos(…)<br>
Prior to that I’d taken the first letter of the key column as a new additional grouping column.<br>
So now we have 2 columns to facet_wrap.</p>
<p>Normally we’d use</p>
<div>
<div>
<pre><code><span>
</span><span>facet_wrap</span><span>(</span><span>random</span><span>~</span><span>key</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>Or vice versa.<br>
But, I didn’t need to do that, I just specified ‘groupvars’ as the faceting variable, and changed the number of columns to prove it actually does what I want.</p>
<div>
<div>
<pre><code><span>facet_wrap</span><span>(</span><span>groupvars</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>Cleverer people than me will be able to explain this magic, but in the meantime, if you need to create a faceted ggplot2 function, I hope this post and the previous one help you get off the ground.</p>
<p>If you like this, please consider liking it (wherever is appropriate) and / or sharing it.<br>
Tag me on Twitter if you do ! <a rel="nofollow" href="https://twitter.com/_johnmackintosh" target="_blank">@_johnmackintosh</a></p>
<p>Also clever peeps are welcome to explain the magic in the comments down below.</p>
<p>Cheers <img alt="🙂" src="https://s.w.org/images/core/emoji/2.4/72x72/1f642.png" class="wp-smiley"></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="https://www.johnmackintosh.com/2018-07-04-more-tidy-evaluation-with-ggplot2/"> HighlandR</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 width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/f167ENYpDmg" height="1"></a></div>