Following up the follow up -
After my previous post, my good friend David Henderson asked a good question on Twitter ( it happens :))
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>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><-</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><-</span><span> </span><span>c</span><span>(</span><span>"fdeaths"</span><span>,</span><span>"mdeaths"</span><span>,</span><span>"ldeaths"</span><span>)</span><span>
</span><span>data</span><span> </span><span><-</span><span> </span><span>as_tibble</span><span>(</span><span>data</span><span>)</span><span>
</span><span>startdate</span><span> </span><span><-</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><-</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><-</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><-</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><-</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><-</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><-</span><span> </span><span>enquo</span><span>(</span><span>x</span><span>)</span><span>
</span><span>y_quo</span><span> </span><span><-</span><span> </span><span>enquo</span><span>(</span><span>y</span><span>)</span><span>
</span><span>groupvars</span><span> </span><span><-</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><-</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>"blue"</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>"blue"</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><-</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>"Easy Tidy Eval in ggplot 3.0.0"</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>"ggplot with tidy evaluation & facetting with no strings"</span><span>)</span><span>
</span><span>p</span><span> </span><span><-</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>""</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>
Pretty ...