<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/the-olympic-medal-table-visualized-gapminder-style/"></div></div><div><a data-url="https://www.r-bloggers.com/the-olympic-medal-table-visualized-gapminder-style/" data-counturl="https://www.r-bloggers.com/the-olympic-medal-table-visualized-gapminder-style/" data-count="horizontal" data-text="The Olympic Medal Table Visualized Gapminder Style" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://staff.math.su.se/hoehle/blog/2016/08/21/gapMedal.html"> Theory meets practice...</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> <a target="_blank" rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"><img alt="Creative Commons License" src="https://i1.wp.com/i.creativecommons.org/l/by-sa/4.0/88x31.png?w=456&ssl=1" data-recalc-dims="1"></a> This work is licensed under a <a target="_blank" rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>. The markdown+Rknitr source code of this blog is available under a <a target="_blank" href="https://www.gnu.org/licenses/gpl-3.0.html">GNU General Public License (GPL v3)</a> license from <a target="_blank" href="https://github.com/hoehleatsu/hoehleatsu.github.io"><img alt="github" src="https://i2.wp.com/staff.math.su.se/hoehle/blog/images/GitHub-Mark-32px.png?w=456" data-recalc-dims="1"></a>.</p>
<h2>Abstract</h2>
<p>Following Hans Rosling’s Gapminder animation style we visualize the total number of medals a country wins during each olympic summer games in relation to the country’s <a target="_blank" href="https://en.wikipedia.org/wiki/Gross_domestic_product">gross domestic product</a> (GDP) per capita. We illustrate how R’s data wrangling capabilities provide a useful toolbox to make such an analysis happen.</p>
<h1>Introduction</h1>
<p>Long Swedish winter nights are best spent watching <a target="_blank" href="https://en.wikipedia.org/wiki/Hans_Rosling">Hans Rosling</a>‘s inspiring <a target="_blank" href="https://www.youtube.com/watch?v=hVimVzgtD6w">TED talks</a>. Such visualizations help the statistician make points about temporal trends in an x-axis to y-axis relationship, which otherwise might drown in modelling details. Recently, I stumbled over a <a target="_blank" href="https://rpubs.com/sjackman/gapminder-gganimate">blog post</a> on how to use the <a target="_blank" href="https://github.com/dgrtwo/gganimate"><code>gganimate</code></a> R package to animate the Gapminder data available from the <code>gapminder</code> package. In order to perform a similar <em>Rosling style</em> animation consider the following: Today, the Olympic Summer Games in Rio de Janeiro end. As usual this spawns a debate, whether the nation’s participation has been successful. For this purpose the <a target="_blank" href="https://en.wikipedia.org/wiki/Olympic_medal_table">olympic medal table</a> is often taken as basis for comparisons, e.g., to mock your <a target="_blank" href="http://politiken.dk/sport/ol/ECE3349634/danmark-og-sverige-kaemper-til-det-sidste---men-hvor-daelen-er-norge-henne/">neighbouring countries</a>. Recent analyses and visualization have been interested in how to correct these tables for, e.g., population size or, more interesting, analyse the influence of GDP. For example:</p>
<ul>
<li>Google provides <a target="_blank" href="https://landing.google.com/altmedaltable/">alternative Olympics medal tables</a></li>
<li>Time Magazine discusses whether it is fair <a target="_blank" href="http://time.com/4452128/olympics-medals-per-capita-rankings/">to rank countries by medals achieved alone</a></li>
</ul>
<p>The aim of the present blog note is to visualize how countries perform in the medal table in relation to their GDP per capita. From a technical viewpoint we experiment with using R to scrape the olympic medal tables from Wikipedia and animate the results Gapminder style. <strong>Disclaimer</strong>: We only show the potential of such an analysis and, hence, worry less about the scientific validity of the analysis.</p>
<h1>Data</h1>
<p>We use the data of <a target="_blank" href="https://www.gapminder.org/">Gapminder</a> in order to obtain country specific population and GDP per capita data for each of the years in the period of 1960-2016. The olympic medal tables are ‘harvested’ from Wikipedia.</p>
<h2>Olympic medal tables</h2>
<p>Olympic medal tables were extracted using the <code>rvest</code> package from the corresponding Wikipedia pages by using table-extracting-code described in the post by <a target="_blank" href="http://blog.corynissen.com/2015/01/using-rvest-to-scrape-html-table.html">Cory Nissen</a>. The Wikipedia tables contain the current state of the medal table and hence take changes in the medal distribution, e.g. deprivation due to doping, into account. For details on such a table, see for example the <a target="_blank" href="https://en.wikipedia.org/wiki/2012_Summer_Olympics_medal_table">medal table of the 2012 summer games</a> in London. In order to stay focused we hide the scraping functionality in the function <code>scrape_medaltab</code> – see the code on GitHub for more details.</p>
<div>
<pre><code><span>#Years which had olympic games</span>
olympic_years &lt;-<span> </span><span>seq</span>(<span>1960</span>, <span>2016</span>, <span>by=</span><span>4</span>)

<span># Extract olympic medal table from all olympic years since 1960</span>
medals &lt;-<span> </span><span>bind_rows</span>(<span>lapply</span>(olympic_years, scrape_medaltab))

<span># Show result</span>
DT::<span>datatable</span>(medals)</code></pre>
</div>
<figure>
<img src="https://i1.wp.com/staff.math.su.se/hoehle/blog/figure/source/2016-08-21-gapMedal/unnamed-chunk-2-1.png?w=456" data-recalc-dims="1"><br>
</figure>
<h2>Gapminder data</h2>
<p>We obtain GDP per capita and population data from <a target="_blank" href="https://www.gapminder.org/data/">Gapminder</a>. Unfortunately, these need to be fetched and merged manually. A more convenient way would have been to take these directly from the package <a target="_blank" href="https://cran.r-project.org/web/packages/gapminder/index.html"><code>gapminder</code></a>, but newer <a target="_blank" href="https://www.gapminder.org/data/documentation/gd001/">GDP data</a> are now available. Again, we hide the details of the data wrangling activities and refer to GitHub code.</p>
<p>For convenience, we also extract the corresponding continent each country belongs to. This can be done conveniently by comparing with the <code>gapminder</code> dataset (see code for details).</p>
<h2>Joining the two data sources</h2>
<p>In principle, all that is left to do is to join the two data sources using the country name of the gapminder dataset and the nation names of the olympic medal tables. However, a challenge of the present country based analysis is how to incorporate the many political changes which happened during the analysis period. As an example, East Germany participated as independent national olympic committee during 1968-1988, but the gapminder data only contain GDP data for Germany as a total. We therefore aggregate the results of the two countries for the analysis. A further important change is the split of the former Soviet Union into several independent states. As a consequence, in 1992 a subset of the former Soviet republics participated as <a target="_blank" href="https://en.wikipedia.org/wiki/Unified_Team_at_the_1992_Summer_Olympics">Unified Team</a>. The GDP values have to be obtained from the Gapminder data by manual summing the individual Soviet republic GDP values. Again we skip further data munging details and simply refer to the GitHub code for a <strong>transparent &amp; reproducible</strong> account.</p>
<div>
<pre><code>medals_gm &lt;-<span> </span><span>left_join</span>(medals_mod, gapminder_manual, <span>by=</span><span>c</span>(<span>&quot;Nation&quot;</span>,<span>&quot;Year&quot;</span>))</code></pre>
</div>
<h1>Results</h1>
<p>First we analyse the <a target="_blank" href="https://en.wikipedia.org/wiki/All-time_Olympic_Games_medal_table">all-time summer olympic medal table</a> for the period 1960-2016.</p>
<div>
<pre><code>medals_alltime &lt;-<span> </span>medals_gm  %>%<span> </span><span>group_by</span>(Nation)  %>%<span> </span><span>summarise</span>(<span>Total=</span><span>sum</span>(Total))  %>%<span> </span><span>arrange</span>(<span>desc</span>(Total))
DT::<span>datatable</span>(medals_alltime)</code></pre>
</div>
<figure>
<img src="https://i2.wp.com/staff.math.su.se/hoehle/blog/figure/source/2016-08-21-gapMedal/unnamed-chunk-7-1.png?w=456" data-recalc-dims="1"><br>
</figure>
<p>We now plot of the total number of medals awarded for each summer games in the period of 1960-2016.</p>
<div>
<pre><code>nTotal &lt;-<span> </span>medals_gm %>%<span> </span><span>group_by</span>(Year) %>%<span> </span><span>summarise</span>(<span>TotalOfGames=</span><span>sum</span>(Total))
<span>ggplot</span>(nTotal, <span>aes</span>(<span>x=</span>Year,<span>y=</span>TotalOfGames)) +<span> </span><span>geom_line</span>() +<span> </span><span>ylab</span>(<span>&quot;Total number of medals per Summer Games&quot;</span>)</code></pre>
</div>
<figure>
<img src="https://i2.wp.com/staff.math.su.se/hoehle/blog/figure/source/2016-08-21-gapMedal/TOTALMEDALSPERGAME-1.png?w=456" data-recalc-dims="1"><br>
</figure>
<p>We observe a clear increasing trend. Hence, in order to make between-country comparisons over time based on the number of medals won, we normalizewon medals by the total number of medals awarded during the corresponding games. The result is stored in the column <code>Frac</code>.</p>
<div>
<pre><code>medals_gm &lt;-<span> </span>medals_gm %>%<span> </span><span>left_join</span>(nTotal, <span>by=</span><span>&quot;Year&quot;</span>) %>%<span> </span><span>mutate</span>(<span>Frac =</span> Total /<span> </span>TotalOfGames)</code></pre>
</div>
<p>After all these pre-processing steps, we can now compare country results for all summer games in the period 2000-2016.</p>
<figure>
<img src="https://i2.wp.com/staff.math.su.se/hoehle/blog/figure/source/2016-08-21-gapMedal/FACET2000ANDBEYOND-1.png?w=456" data-recalc-dims="1"><br>
</figure>
<p>Note that for better visualization of the many countries with a small number of medals, an <span>\(\sqrt{}\)</span>-transform of the y-axis is used.</p>
<p>Finally, we can use the <code>gganimate</code> package to visualize the dependence of the total number of medals won in the summer games 1960-2016 as a function of GDP per capita.</p>
<figure>
<img src="https://i2.wp.com/staff.math.su.se/hoehle/blog/figure/source/2016-08-21-gapMedal/olympicMedals-gapminder-style.gif?w=456" data-recalc-dims="1"><br>
</figure>
<p>As before a <span>\(\sqrt{}\)</span>-transform of the y-axis is used for better visualization. One interesting observation we see from the animation is that the home-country of the Olympics always appears to do well in the following Olympics. Also note that the <a target="_blank" href="https://en.wikipedia.org/wiki/1980_Summer_Olympics_boycott">1980</a> and <a target="_blank" href="https://en.wikipedia.org/wiki/1984_Summer_Olympics_boycott">1984</a> were special due to boycotts. With respect to the top-5 nations it is also worth noticing that China, due to protests against the participation of Taiwan, did not participate in the Olympics 1956-1980. Furthermore, up to 1988 the team denoted &quot;Germany&quot; in the animation consists of the combined number of medals of &quot;East Germany&quot; and &quot;West Germany&quot;.</p>
<p>To see the medal tables in a different light, we instead visualize a quantity relative to the number of medals per population. To enable cross-year comparisons we therefore compute the following index for each country and olympic summer games: <span>\[<br>
\frac{\text{Fraction of All Medals the Country got in that Year}}{\text{Population in the Country that Year}} \times 10^6.<br>
\]</span> We shall call this index a country’s fraction of all medals per million population. A similar animation as above, now with logarithmic y-axis, illustrates the dynamics. To provide <strong>evidence supported neighbour mocking</strong>, we highlight the position of the three Nordic countries (Denmark, Sweden and Norway).</p>
<figure>
<img src="https://i1.wp.com/staff.math.su.se/hoehle/blog/figure/source/2016-08-21-gapMedal/olympicMedals-perpop-gapminder-style.gif?w=456" data-recalc-dims="1"><br>
</figure>
<p>Jamaica, Bahamas and Grenada appear to do reasonably well lately compared to their population size. However, more more important – did you noticed the position of Denmark at the 2016 games in Rio?</p>
<div>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/the-olympic-medal-table-visualized-gapminder-style/"></div></div><div><a data-url="https://www.r-bloggers.com/the-olympic-medal-table-visualized-gapminder-style/" data-counturl="https://www.r-bloggers.com/the-olympic-medal-table-visualized-gapminder-style/" data-count="vertical" data-text="The Olympic Medal Table Visualized Gapminder Style" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://staff.math.su.se/hoehle/blog/2016/08/21/gapMedal.html"> Theory meets practice...</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/wmTzbEoST6w"></a></div>