Tidily evaluated 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.

Test driving the new release of ggplot2 –

A new release of ggplot2

Now that tidy evaluation is baked into ggplot2, as of TODAY, let’s take it for a spin:

ggplot2 3.0.0 %>%
create function %>%
test function %>%
end

<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"># create generic function</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="w"> </span><span class="n">group</span><span class="p">)</span><span class="w"> </span><span class="p">{</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">group_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">group</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">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">group_quo</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">3</span><span class="p">)</span><span class="w"> </span><span class="c1">#look Ma, no need to bang bang here!</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>

Let’s test it:

<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">key</span><span class="p">)</span><span class="w">

</span>

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

End

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)