More tidy evaluation with ggplot2

[This article was first published on HighlandR, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Following up the follow up –

After my previous post, my good friend David Henderson asked a good question on Twitter ( it happens :))

2018-07-04-tweetshot.png

To be honest, I hadn’t even noticed there was no tilde (the ‘~’ ) in my call to facet_wrap.

So I did a little experiment :

<span class="n">library</span><span class="p">(</span><span class="n">dplyr</span><span class="p">)</span><span class="w">

</span><span class="n">library</span><span class="p">(</span><span class="n">ggplot2</span><span class="p">)</span><span class="w">

</span><span class="n">library</span><span class="p">(</span><span class="n">tidyr</span><span class="p">)</span><span class="w">

</span><span class="n">library</span><span class="p">(</span><span class="n">tibble</span><span class="p">)</span><span class="w">


</span><span class="n">data</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">list</span><span class="p">(</span><span class="n">fdeaths</span><span class="p">,</span><span class="n">mdeaths</span><span class="p">,</span><span class="n">ldeaths</span><span class="p">)</span><span class="w"> </span><span class="c1">#time series data- needs prep</span><span class="w">

</span><span class="nf">names</span><span class="p">(</span><span class="n">data</span><span class="p">)[</span><span class="m">1</span><span class="o">:</span><span class="m">3</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"fdeaths"</span><span class="p">,</span><span class="s2">"mdeaths"</span><span class="p">,</span><span class="s2">"ldeaths"</span><span class="p">)</span><span class="w">

</span><span class="n">data</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">as_tibble</span><span class="p">(</span><span class="n">data</span><span class="p">)</span><span class="w">

</span><span class="n">startdate</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">as.Date</span><span class="p">(</span><span class="s1">'1974-1-1'</span><span class="p">)</span><span class="w">

</span><span class="n">data</span><span class="o">$</span><span class="n">date</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">seq.Date</span><span class="p">(</span><span class="n">startdate</span><span class="p">,</span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'month'</span><span class="p">,</span><span class="n">length.out</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">72</span><span class="p">)</span><span class="w">


</span><span class="n">newdata</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tidyr</span><span class="o">::</span><span class="n">gather</span><span class="p">(</span><span class="n">data</span><span class="p">,</span><span class="w"> </span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">value</span><span class="p">,</span><span class="o">-</span><span class="n">date</span><span class="p">)</span><span class="w">

</span><span class="n">newdata</span><span class="o">$</span><span class="n">value</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(</span><span class="n">newdata</span><span class="o">$</span><span class="n">value</span><span class="p">)</span><span class="w">
</span><span class="c1"># adding in an additional colum to facet by</span><span class="w">
</span><span class="n">newdata</span><span class="o">$</span><span class="n">random</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_sub</span><span class="p">(</span><span class="n">newdata</span><span class="o">$</span><span class="n">key</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">1</span><span class="p">)</span><span class="w">


</span><span class="n">gtest</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">df</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">...</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  
  </span><span class="c1"># check out the ... in the function parameters</span><span class="w">
  
  </span><span class="n">x_quo</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">enquo</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w">
  
  </span><span class="n">y_quo</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">enquo</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="w">
  
  </span><span class="n">groupvars</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">quos</span><span class="p">(</span><span class="n">...</span><span class="p">)</span><span class="w"> </span><span class="c1">## this is a new bit too</span><span class="w">
  
  
  
  </span><span class="n">p</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ggplot</span><span class="p">(</span><span class="n">df</span><span class="p">,</span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">!!</span><span class="n">x_quo</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">!!</span><span class="n">y_quo</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">   </span><span class="c1">#bangin'   </span><span class="w">
    
    </span><span class="n">geom_line</span><span class="p">(</span><span class="n">colour</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"blue"</span><span class="p">,</span><span class="w"> </span><span class="n">group</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> 
    
    </span><span class="n">geom_point</span><span class="p">(</span><span class="n">colour</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"blue"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
    
    </span><span class="n">facet_wrap</span><span class="p">(</span><span class="n">groupvars</span><span class="p">,</span><span class="w"> </span><span class="n">ncol</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span><span class="p">)</span><span class="w"> </span><span class="c1">#look Ma, no tilde. Also, 2 cols, not 3</span><span class="w">
  
  
  </span><span class="n">p</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">ggtitle</span><span class="p">(</span><span class="n">label</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Easy Tidy Eval in ggplot 3.0.0"</span><span class="p">,</span><span class="w">
                   
                   </span><span class="n">subtitle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"ggplot with tidy evaluation & facetting with no strings"</span><span class="p">)</span><span class="w">
  
  </span><span class="n">p</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">labs</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NULL</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NULL</span><span class="p">,</span><span class="w"> </span><span class="n">caption</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">""</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
    </span><span class="n">theme_bw</span><span class="p">()</span><span class="w">
  
  </span><span class="n">p</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">gtest</span><span class="p">(</span><span class="n">newdata</span><span class="p">,</span><span class="n">date</span><span class="p">,</span><span class="n">value</span><span class="p">,</span><span class="n">random</span><span class="p">,</span><span class="n">key</span><span class="p">)</span><span class="w">
</span>

2018-07-04-tidy-string-free-ggplot2.png

Pretty cool huh?

What’s different here?

Well, I added in ‘…’ to the definition, and assigned groupvars using quos(…)
Prior to that I’d taken the first letter of the key column as a new additional grouping column.
So now we have 2 columns to facet_wrap.

Normally we’d use

<span class="w">
</span><span class="n">facet_wrap</span><span class="p">(</span><span class="n">random</span><span class="o">~</span><span class="n">key</span><span class="p">)</span><span class="w">
</span>

Or vice versa.
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.

<span class="n">facet_wrap</span><span class="p">(</span><span class="n">groupvars</span><span class="p">,</span><span class="w"> </span><span class="n">ncol</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span><span class="p">)</span><span class="w">
</span>

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.

If you like this, please consider liking it (wherever is appropriate) and / or sharing it.
Tag me on Twitter if you do ! @_johnmackintosh

Also clever peeps are welcome to explain the magic in the comments down below.

Cheers 🙂

To leave a comment for the author, please follow the link and comment on their blog: HighlandR.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)