<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/mid-table-mediocrity/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.johnmackintosh.com/2018-08-11-mid-table-mediocrity/"> HighlandR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>	Visualising potential winners in the Scottish Championship – </p>
<p>Last August, I was given a surprise gift of a season ticket to watch my local football team, Inverness Caledonian Thistle.</p>
<p>They’d been relegated from the top league in Scotland the season before, and needed all the local support they could get. In truth, the gift ( from my father) was a sneaky attempt to get my eldest son hooked into supporting his local team (as every adult season ticket allows free entry for an under-12).</p>
<p>Long story short, by the end of the season, it was me that was hooked. After an appalling start to the season, the team went on a couple of great runs of results. In the late Autum, from languishing in 9th position (out of 10 teams) they went on an unbeaten run which saw them break their record for the number of games without conceding a goal.</p>
<p>Then, towards the end of the season, they went on another unbeaten run, featuring a number of consecutive wins.<br>
This saw supporters start to dream of finishing in 4th place, and with it, a chance of promotion back to the Premier League.</p>
<p>The winners automatically get promoted, with 4th playing 3rd, then the winners playing the 2nd team, and the winners of THAT game, playing the second bottom team from the premier league.</p>
<p>So the hope was that ICT would finish 4th, and somehow navigate their way through 3 more matches against teams who finished higher in the league or were in the league above.</p>
<p>The league standings, prior to the latest round of matches, were as follows:</p>
<p><img alt="2018-08-11-current.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-08-11-current.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>St Mirren were runaway leaders, and heading for automatic promotion. Livingston looked to have cemented 2nd place. At the other end, Brechin were doomed to be relegated, with Dumbarton also looking at a playoff to remain in the league. This left 6 teams scrapping for 3rd and 4th. The teams were resonably evently matched – on any given day, any team could beat anyone else.</p>
<p>By the way, QOTS is my abbreviation for Queen of the South, a team based in Dumfries, in the Scottish Borders, and not to be confused with Queens of the Stone Age, who are a hard rock group from California.<br>
Although I’m pretty sure QOTSA frontman Josh Homme could do a job up front for any team as he’s a big lad.</p>
<p>Anyway – I started to think about ways to plot potential placings on a game by game basis. I wasn’t concerned with thinking about correct scores or goal difference. It was easy enought to calculate where one team might be in the table based on 3 points for a win, 1 for a draw and none for a loss. But then there were all the other games to take into account too.</p>
<p>A base R command that I have used in plotting in the past is expand.grid().<br>
It’s especially useful for plotting heatmaps to ensure that there is an observation for every combination of the x and y axis.<br>
It’s also really useful for working out possible combinations of football results.</p>
<p><img alt="2018-08-11-setup.PNG" src="https://i0.wp.com/www.johnmackintosh.com/img/2018-08-11-setup.PNG?w=456&ssl=1" data-recalc-dims="1"></p>
<p>So with the above fixture list, and current league placings, we can start to figure out the potential outcomes.</p>
<div>
<div>
<pre><code><span>first</span><span> </span><span>&lt;-</span><span> </span><span>head</span><span>(</span><span>fixtures</span><span>,</span><span>1</span><span>)</span><span>
</span><span>permutations</span><span> </span><span>&lt;-</span><span> </span><span>expand.grid</span><span>(</span><span>first</span><span>$</span><span>home</span><span>,</span><span>first</span><span>$</span><span>away</span><span>,</span><span>status</span><span>,</span><span>Results</span><span>)</span><span>
</span><span>permutations</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="2018-08-11-permutations-trial.PNG" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-08-11-permutations-trial.PNG?w=456&ssl=1" data-recalc-dims="1"></p>
<p>That seemed to work, so let’s create a function:</p>
<div>
<div>
<pre><code><span>get_combo</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>,</span><span>y</span><span>){</span><span>
</span><span>permutations</span><span> </span><span>&lt;-</span><span> </span><span>expand.grid</span><span>(</span><span>x</span><span>,</span><span>y</span><span>,</span><span> </span><span>status</span><span>,</span><span>Results</span><span>)</span><span> </span><span>%>%</span><span> </span><span>rename</span><span>(</span><span>home</span><span> </span><span>=</span><span> </span><span>Var1</span><span>,</span><span> </span><span>away</span><span> </span><span>=</span><span> </span><span>Var2</span><span>,</span><span>status</span><span> </span><span>=</span><span> </span><span>Var3</span><span>,</span><span> </span><span>results</span><span> </span><span>=</span><span> </span><span>Var4</span><span>)</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>home</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>home</span><span>),</span><span>
</span><span>away</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>away</span><span>),</span><span>
</span><span>results</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>results</span><span>))</span><span>
</span><span>return</span><span>(</span><span>permutations</span><span>)</span><span>
</span><span>}</span><span> </span></code></pre>
</div>
</div>
<p>Now we can use purrr to apply the function to each fixure. This returns a list of dataframes, which can be combined with bind_rows, and then we can left join to another ancillary table to return the potential points gained for each combination of result:</p>
<div>
<div>
<pre><code><span>permutations</span><span> </span><span>&lt;-</span><span> </span><span>map2</span><span>(</span><span>fixtures</span><span>$</span><span>home</span><span>,</span><span>fixtures</span><span>$</span><span>away</span><span>,</span><span> </span><span>get_combo</span><span>)</span><span>
</span><span>permutations</span><span> </span><span>&lt;-</span><span> </span><span>bind_rows</span><span>(</span><span>permutations</span><span>)</span><span> </span><span>%>%</span><span> </span><span>left_join</span><span>(</span><span>lookup</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;results&quot;</span><span>)</span><span>
</span><span>permutations</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="2018-08-11-permutations-function-output.PNG" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-08-11-permutations-function-output.PNG?w=456&ssl=1" data-recalc-dims="1"></p>
<p>We can then take these outcomes, and with our current points totals, start to work out possible points for each team for each combination.</p>
<p>And of course we can visualise those too:</p>
<p><img alt="2018-08-11-potential-standings.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-08-11-potential-standings.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>That’s a start, but, a lot of these teams have similar colours and its hard to work out who they are.<br>
Perhaps a dumbbell plot might help? We can use the ggalt package for that:</p>
<div>
<div>
<pre><code><span>p</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>potential_summary</span><span>,</span><span>aes</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>reorder</span><span>(</span><span>team</span><span>,</span><span>xstart</span><span>),</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>xstart</span><span>,</span><span>xend</span><span> </span><span>=</span><span> </span><span>xend</span><span>,</span><span>
</span><span>group</span><span> </span><span>=</span><span> </span><span>team</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_dumbbell</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>1.25</span><span>,</span><span>aes</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>team</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_colour_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>finalcolours</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_minimal</span><span>()</span><span> </span><span>+</span><span> </span><span>ggtitle</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>&quot;Potential Points - Scottish Championship&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Range of possible points after games played 14th Apr 2018&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Team&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Possible Points&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;bottom&quot;</span><span>)</span><span> </span></code></pre>
</div>
</div>
<p><img alt="2018-08-11-dumbell-v1.png" src="https://i0.wp.com/www.johnmackintosh.com/img/2018-08-11-dumbell-v1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Hmm, let’s zoom in a bit on our mid table teams. We can do that by using coord_cartesian().</p>
<p>And while we’re at it, let’s add some labels on the dumbbells, and remove some of the extraneous axis labels and tick marks:</p>
<div>
<div>
<pre><code><span>
</span><span>##zoom in on mid table</span><span>
</span><span>q</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span> </span><span>+</span><span> </span><span>coord_cartesian</span><span>(</span><span>xlim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>35</span><span>,</span><span>56</span><span>))</span><span> </span><span>+</span><span> </span><span>scale_x_continuous</span><span>(</span><span>limits</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>35</span><span>,</span><span>56</span><span>),</span><span>breaks</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>35</span><span>:</span><span>56</span><span>))</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>&quot;Mid Table Mediocrity in the Scottish Championship&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Range of possible points for teams who are \n outside promotion or relegation places&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;games to be played 14th Apr 2018&quot;</span><span>)</span><span>
</span><span>q</span><span>
</span><span>q</span><span> </span><span>&lt;-</span><span> </span><span>q</span><span> </span><span>+</span><span> </span><span>geom_text</span><span>(</span><span>aes</span><span>(</span><span>xstart</span><span> </span><span>-</span><span> </span><span>1</span><span>,</span><span>label</span><span> </span><span>=</span><span> </span><span>xstart</span><span>),</span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span>
</span><span>q</span><span> </span><span>&lt;-</span><span> </span><span>q</span><span> </span><span>+</span><span> </span><span>geom_text</span><span>(</span><span>aes</span><span>(</span><span>xend</span><span> </span><span>+</span><span> </span><span>1</span><span>,</span><span>label</span><span> </span><span>=</span><span> </span><span>xend</span><span>),</span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span>
</span><span>q</span><span> </span><span>&lt;-</span><span> </span><span>q</span><span> </span><span>+</span><span> </span><span>ggrepel</span><span>::</span><span>geom_text_repel</span><span>(</span><span>aes</span><span>(</span><span>xstart</span><span> </span><span>+</span><span> </span><span>2</span><span>,</span><span>label</span><span> </span><span>=</span><span> </span><span>team</span><span>),</span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span>
</span><span>q</span><span>
</span><span># get rid of labels etc. that we don't need</span><span>
</span><span>q</span><span> </span><span>&lt;-</span><span> </span><span>q</span><span> </span><span>+</span><span> </span><span>coord_cartesian</span><span>(</span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>3</span><span>,</span><span>8</span><span>))</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;none&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>axis.title.x</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.text.x</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.ticks.x</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>())</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>axis.title.y</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.text.y</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.ticks.y</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>())</span><span> </span><span>+</span><span>
</span><span>ggExtra</span><span>::</span><span>removeGrid</span><span>()</span><span>
</span><span>q</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="2018-08-11-mid-table-dumbbell.png" src="https://i0.wp.com/www.johnmackintosh.com/img/2018-08-11-mid-table-dumbbell.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>So now we can see that Inverness, Morton and Queen of the South are really closely bunched up, but with a bit to do to make into 4th. They certainly won’t mangage to get into those positions based on the next match.</p>
<p>We can split out the potential placings for each team, based on whether they win, lose or draw.</p>
<p><img alt="208-08-11-faceted-potential-placings.png" src="https://i2.wp.com/www.johnmackintosh.com/img/208-08-11-faceted-potential-placings.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Or we can just show everything on one plot, and let the reader try and make sense of the resulting stramash:</p>
<p><img alt="2018-08-11-stramash.png" src="https://i0.wp.com/www.johnmackintosh.com/img/2018-08-11-stramash.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Here’s the full gist if you want to mess around with it:<br>
</p>
<p>There are a few more combinations you could try, plus some more work with tidyr might allow you to create different (and better plots)..</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-08-11-mid-table-mediocrity/"> 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/_7uWP7vSPTk" height="1"></a></div>