<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/factfulness-building-gapminder-income-mountains/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://staff.math.su.se/hoehle/blog/2018/07/02/factfulness.html"> Theory meets practice...</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Abstract:</h2>
<p>We work out the math behind the so called income mountain plots used in the book &quot;Factfulness&quot; by Hans Rosling and use these insight to generate such plots using tidyverse code. The trip includes a mixture of log-normals, the density transformation theorem, histogram vs. density and then skipping all those details again to make nice moving mountain plots.</p>
<p><center><br>
<img src="https://i1.wp.com/staff.math.su.se/hoehle/blog/figure/source/2018-07-02-factfulness/moving-mountains.gif?w=456" data-recalc-dims="1"><br>
</center></p>
<p> <a rel="nofollow" href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank"><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 rel="nofollow" href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank">Creative Commons Attribution-ShareAlike 4.0 International License</a>. The markdown+Rknitr source code of this blog is available under a <a rel="nofollow" href="https://www.gnu.org/licenses/gpl-3.0.html" target="_blank">GNU General Public License (GPL v3)</a> license from github.</p>
<h2>Introduction</h2>
<p>Reading the book <a rel="nofollow" href="https://www.gapminder.org/factfulness/" target="_blank">Factfulness</a> by <a rel="nofollow" href="https://en.wikipedia.org/wiki/Hans_Rosling" target="_blank">Hans Rosling</a> seemed like a good thing to do during the summer months. The ‘<a rel="nofollow" href="https://www.nature.com/news/three-minutes-with-hans-rosling-will-change-your-mind-about-the-world-1.21143" target="_blank">possibilistic</a>‘ writing style is contagious and his <a rel="nofollow" href="https://www.youtube.com/watch?v=hVimVzgtD6w" target="_blank">TedEx</a> presentations and <a rel="nofollow" href="https://www.youtube.com/watch?v=Oxxx03_JHlM" target="_blank">media interviews</a> are legendary teaching material on how to support your arguments with data. What a shame he passed away in 2017.</p>
<p>What is really enjoyable about the book is that the <a rel="nofollow" href="https://www.gapminder.org/" target="_blank">Gapminder web page</a> allows you to study many of the graphs from the book interactively and contains the data for download. Being a fan of <strong>transparency</strong> and <strong>reproducibility</strong>, I got interested in the so called <a rel="nofollow" href="https://www.gapminder.org/data/documentation/income-mountains-dataset/" target="_blank"><strong>income mountain plots</strong></a>, which show how incomes are distributed within individuals of a population:</p>
<p>
<center><br>
<img src="https://i2.wp.com/staff.math.su.se/hoehle/blog/figure/source/2018-07-02-factfulness/gapminder-income-mountain.png?w=456" data-recalc-dims="1"><br>
</center><br>
<font color="bbbbbb">Screenshot of the 2010 income mountain plot. Free material from <a rel="nofollow" href="https://www.gapminder.org/" target="_blank">www.gapminder.org</a>.</font></p>
<p><p>One notices that the &quot;mountains&quot; are plotted on a log-base-2 x-axis and without a y-axis annotation. Why? Furthermore, world income data usually involve mean income per country, so I got curious how/if these plots were made without access to finer granularity level data? Aim of this blog post is to answer these questions by using Gapminder data freely available from their webpage. The answer ended up as a nice <code>tidyverse</code> exercise and could serve as motivating application for basic probability course content.</p>
<h2>Data Munging Gapminder</h2>
<p>Data on income, population and Gini coefficient were needed to analyse the above formulated questions. I have done this previously in order to visualize the <a rel="nofollow" href="http://staff.math.su.se/hoehle/blog/2016/08/21/gapMedal.html" target="_blank">Olympic Medal Table Gapminder Style</a>. We start by downloading the GDP data, which is the annual gross domestic product per capita by Purchasing Power Parities (PPP) measured in <a rel="nofollow" href="https://en.wikipedia.org/wiki/Geary%E2%80%93Khamis_dollar" target="_blank">international dollars</a>, fixed 2011 prices. Hence, the inflation over the years and differences in the cost of living between countries is accounted for and can thus be compared – see the <a rel="nofollow" href="https://www.gapminder.org/data/documentation/gd001/" target="_blank">Gapminder documentation</a> for further details. We download the <a rel="nofollow" href="https://github.com/Gapminder-Indicators/gdppc_cppp/raw/master/gdppc_cppp-by-gapminder.xlsx" target="_blank">data from Gapminder</a> where they are available in <em>wide format</em> as Excel-file. For tidyverse handling we reshape them into <em>long format</em>.</p>
<div>
<pre><code>##Download gdp data from gapminder - available under a CC BY-4 license.
<span>if</span> (<span>!</span><span>file.exists</span>(<span>file.path</span>(fullFigPath, <span>&quot;gapminder-gdp.xlsx&quot;</span>))) {
  <span>download.file</span>(<span>&quot;https://github.com/Gapminder-Indicators/gdppc_cppp/raw/master/gdppc_cppp-by-gapminder.xlsx&quot;</span>, <span>destfile=</span><span>file.path</span>(fullFigPath,<span>&quot;gapminder-gdp.xlsx&quot;</span>))
}

gdp_long &lt;-<span> </span>readxl<span>::</span><span>read_xlsx</span>(<span>file.path</span>(fullFigPath, <span>&quot;gapminder-gdp.xlsx&quot;</span>), <span>sheet=</span><span>2</span>) <span>%>%</span>
<span> </span><span>rename</span>(<span>country=</span><span>`</span><span>geo.name</span><span>`</span>) <span>%>%</span>
<span> </span><span>select</span>(<span>-</span>geo,<span>-</span>indicator,<span>-</span>indicator.name) <span>%>%</span>
<span> </span><span>gather</span>(<span>key=</span><span>&quot;year&quot;</span>, <span>value=</span><span>&quot;gdp&quot;</span>, <span>-</span>country,) <span>%>%</span>
<span> </span><span>filter</span>(<span>!</span><span>is.na</span>(gdp))</code></pre>
</div>
<p>Furthermore, we rescale GDP per year to daily income, because this is the unit used in the book.</p>
<div>
<pre><code>gdp_long <span>%&lt;>%</span><span> </span><span>mutate</span>(<span>gdp =</span> gdp <span>/</span><span> </span><span>365.25</span>)</code></pre>
</div>
<p>Similar code segments are written for (see the <a rel="nofollow" href="https://raw.githubusercontent.com/hoehleatsu/hoehleatsu.github.io/master/_source/2018-07-02-factfulness.Rmd" target="_blank">code</a> on github for details)</p>
<ul>
<li>the gini (<code>gini_long</code>) and population (<code>pop_long</code>) data</li>
<li>the regional group (=continent) each country belongs two (<code>group</code>)</li>
</ul>
<p>The four data sources are then joined into one long tibble <code>gm</code>. For each year we also compute the fraction a country’s population makes up of the world population that year (column <code>w</code>) as well as the fraction within the year and region the population makes up (column <code>w_region</code>) :</p>
<pre><code>## # A tibble: 15,552 x 9
##   country     region code  year   gini   gdp population          w  w_region
##   </code></pre>