Articles by HighlandR

Automatic (slides) for the people

November 29, 2018 | HighlandR

Creating and saving multiple plots to Powerpoint At the NHS R conference we delivered a session on animating patient flow. This started with a single plot showing all patient movements, and then I demonstrated the ability to create a faceted... [Read more...]

Thoughts on the NHS-R conference

October 30, 2018 | HighlandR

It’s been a few weeks since the first NHS-R conference was held in Birmingham. I co-presented a couple of workshops with Neil Pettinger on visualising patient flow, covering the following importing from Excel (and connecting to SQ... [Read more...]

Thoughts on the NHS-R conference

October 30, 2018 | HighlandR

It’s been a few weeks since the first NHS-R conference was held in Birmingham. I co-presented a couple of workshops with Neil Pettinger on visualising patient flow, covering the following importing from Excel (and connecting to SQ... [Read more...]

Where am I?

September 22, 2018 | HighlandR

Notes on the here package - The here package is pretty simple ( only 3 functions), but I keep messing things up when I try to create paths with it, so this is my aide-memoire. It might be useful for others too. Here finds the root of your cu... [Read more...]

Where am I?

September 22, 2018 | HighlandR

Notes on the here package - The here package is pretty simple ( only 3 functions), but I cannot remember how to use it to navigate folders, so this is my aide-memoire. It might be useful for others too. Here finds the root of your current fo... [Read more...]

Mid Table Mediocrity

August 10, 2018 | HighlandR

Visualising potential winners in the Scottish Championship - Last August, I was given a surprise gift of a season ticket to watch my local football team, Inverness Caledonian Thistle. They’d been relegated from the top league in Scotland t...
[Read more...]

Mid Table Mediocrity

August 10, 2018 | HighlandR

Visualising potential winners in the Scottish Championship - Last August, I was given a surprise gift of a season ticket to watch my local football team, Inverness Caledonian Thistle. They’d been relegated from the top league in Scotland t...
[Read more...]

Tidily evaluated ggplot2

July 3, 2018 | HighlandR

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>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># create generic function</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>group</span><span>)</span><span> </span><span>{</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>group_quo</span><span> </span><span><-</span><span> </span><span>enquo</span><span>(</span><span>group</span><span>)</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>group_quo</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span> </span><span>#look Ma, no need to bang bang here!</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>
Let’s test it:
<span>gtest</span><span>(</span><span>newdata</span><span>,</span><span>date</span><span>,</span><span>value</span><span>,</span><span>key</span><span>)</span><span>

</span>
End
[Read more...]

More tidy evaluation with ggplot2

July 3, 2018 | HighlandR

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 ...
[Read more...]

Even Simpler SQL

June 2, 2018 | HighlandR

I’ve had some feedback on the last post, and rather than repeat the same thing multiple times, I’m going all @drob, and writing this instead.. When I tweeted out the link to my post I gave it the tag line “why I’d rather write dplyr than SQL”. ... [Read more...]

simpler SQL with dplyr

May 31, 2018 | HighlandR

comparing dplyr with SQL nested queries - Following on from my last post, where I demonstrated R to some first time R users, I want to do a wee comparison of dplyr V SQL, so that folks, particularly those in the NHS who might be R curious, can see just ...
[Read more...]

AphA Scotland – it’s a thing

May 12, 2018 | HighlandR

Reflections on AphA Scotland launch event - On Tues 8th May there was only one Scottish based member of the Association of Professional Healthcare Analysts (me) but on Wed 9th May that number rose to around 80 with the launch of the the Scotland AphA Branch. The event took place in ... [Read more...]

backed by data

April 1, 2018 | HighlandR

Minmising risk of delayed departure - This post is an attempt to answer this question: https://community.rstudio.com/t/how-to-answer-a-question-without-statistical-tests-but-is-backed-by-data/3711 It was a pretty interesting question, and I...
[Read more...]

No strings attached

March 3, 2018 | HighlandR

a faceting function with bare variable names in ggplot2 - I’m working on a package of dplyr and ggplot2 functions that I can use on a day to day basis to plot data over time. I’ve done some basic experimenting with my preferred ggplot2 th...
[Read more...]

Theory free tidyeval

February 18, 2018 | HighlandR

Getting our feet wet with dplyr programming - I’ve been putting off trying to suss out dplyr’s tidy evaluation for a long time, but I’ve been experimenting a bit with it so just making some notes here. It’s hard to talk about tidy e... [Read more...]

Enter SadMan

February 2, 2018 | HighlandR

Yet more Metalliplots I’m just going to tidy up some issues from my last post where I did some tidytext analysis on the legendary rockers - Metallica. It was a fun way to pass some time and keep my hand in with text mining, something I st...
[Read more...]

Hardwired..for tidy text

January 29, 2018 | HighlandR

Song lyric and sentiment analysis for all So - a while back I did a tidy text analysis on Faith No More lyrics. I had thought about doing this with Metallica album lyrics, as they have had a long career, spanning thier late teens/twenties to...
[Read more...]

Data Driven DIY

January 19, 2018 | HighlandR

Statisfix - Which fixing should I buy? I have a bathroom cabinet to put up. It needs to go onto a tiled plasterboard (drywall) wall. Because of the tiles, I can’t use the fixings I normally use to keep heavy objects fixed to the wall. A...
[Read more...]

Data Driven DIY

January 19, 2018 | HighlandR

Statisfix - Which fixing should I buy? I have a bathroom cabinet to put up. It needs to go onto a tiled plasterboard (drywall) wall. Because of the tiles, I can’t use the fixings I normally use to keep heavy objects fixed to the wall. A...
[Read more...]
1 2 3 4 5

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)