<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/global-choropleth-maps-of-military-expenditure/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2017/06/04/military-gdp"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Global choropleth maps</h2>
<p>Today I’m writing about using country-level <a rel="nofollow" href="https://en.wikipedia.org/wiki/Choropleth_map" target="_blank">choropleth maps</a> at a global level, using example data showing nations’ military spending as a percentage of GDP. There are a few specific issues to work through as a result of the global coverage. I’m going to start with my two preferred end results.</p>
<p>First, here’s a Twitter-friendly rotating globe:</p>
<div>
<center><br>
<img src="https://i2.wp.com/ellisp.github.io/img/0099-military-gdp.gif?w=456" data-recalc-dims="1"><br>
</center>
</div>
<p>There are a few problems with it I didn’t quite finish. If you watch it long enough you’ll see a few times Canada, the USA or Russia flicker out of the frame. More on that below.</p>
<p>Second, here’s an interactive version, suited for a fully featured web browser:</p>
<p><iframe width="450" src="http://ellisp.github.io/img/0099-leaflet.html"></iframe></p>
<p>I find this the most useful version, even though I prefer to <em>look</em> at the rotating globe. The ability to hover over a country to get its name and value, and to zoom into a particular region, adds real value to the visualization. This is really the minimum people expect out of what I call micro-interactivity (I’m sure there’s a proper, technical name) – ability to zoom and get useful information when hovering.</p>
<p>Unless you spend a lot of time looking at maps of the Middle East, you probably wouldn’t have identified those two dark patches of high military spending as Oman and Eritrea, without the help of the hovering tooltips.</p>
<p>Caveat for today’s post – I’m not a GIS expert at all, and in fact am barely into the “conscious incompetence” stage. So there may be mistakes in what follows, and there are almost certainly better ways to do some of what I’m doing.</p>
<h2>Data on military spend</h2>
<p>First I needed some example data. Choropleth maps are appropriate for variables like proportions and growth rates rather than absolute numbers. Military spending as a percentage of GDP was <a rel="nofollow" href="https://www.nytimes.com/2017/05/26/world/europe/nato-trump-spending.html?_r=0" target="_blank">in the news</a> when I started this, because of USA President Trump drawing attention (in somewhat misleading terms) to the pledge by <a rel="nofollow" href="http://www.economist.com/blogs/graphicdetail/2017/02/daily-chart-11" target="_blank">NATO leaders in 2014</a> to spend 2% of their GDP on the military by 2024 (the 2% was described as an unofficial floor as early as 2006, but only committed to at <a rel="nofollow" href="http://carnegieeurope.eu/2015/09/02/politics-of-2-percent-nato-and-security-vacuum-in-europe-pub-61139" target="_blank">the leadership level in 2014</a> in the atmosphere of nervousness after Russia’s actions in the Ukraine). With seven years to the commitment deadline, it is widely reported that only five countries have yet passed the 2% figure, <a rel="nofollow" href="https://www.nytimes.com/2017/05/26/world/europe/nato-trump-spending.html?_r=1" target="_blank">usually listed as the United States, Greece, United Kingdom, Estonia and Poland</a>. Interestingly, the data I’m using suggest the UK is currently just under 2% and France just over, slightly different to some of the media reporting.</p>
<p>Often my first port of call looking for internationally comparable data is the <a rel="nofollow" href="http://data.worldbank.org/data-catalog/world-development-indicators" target="_blank">World Development Indicators</a> of the World Bank. While they are the definitive origin of very little data, the World Bank does a great job in collating data from other sources and making it available in their visualisation tool. There’s also Vincent Arel-Bundock’s handy <a rel="nofollow" href="https://cran.r-project.org/package=WDI" target="_blank">WDI R package</a> which makes it easy to get the data from the Bank into your own analytical tool.</p>
<p>When I started this exercise a couple of weeks ago, I was able to create this choropleth map from the very friendly WDI visualisation tool:</p>
<p><img src="https://i2.wp.com/ellisp.github.io/img/0099-wdi-screenshot.png?w=100%25" data-recalc-dims="1"></p>
<p>(but interestingly, returning to <a rel="nofollow" href="http://data.worldbank.org/indicator/MS.MIL.XPND.GD.ZS?end=2014&start=1960&view=chart&year=2011" target="_blank">the site</a> today, I can’t reproduce it). Anyway, there’s a few things that leave me not happy with it:</p>
<ul>
<li>The data only go to 2015, whereas the data from the source goes up to 2016 in many cases.</li>
<li>The Mercator projection of the world map – according to <a rel="nofollow" href="https://xkcd.com/977/" target="_blank">xkcd’s brilliant summary of ‘what your favorite map projection says about you’</a>, Mercator says “you’re not really into maps”. There are several problems, but for me it’s all summed up by Greenland looking way too large.</li>
<li>There’s a great ability to use the slider at the bottom of the map (not in the above screenshot though) to cycle through the years, but the scale of the colour fill recalibrates itself each year so this doesn’t allow a coherent view of change over time.</li>
<li>the colour scheme isn’t particularly good at drawing out differences between countries.</li>
</ul>
<p>So I went to the source, the <a rel="nofollow" href="https://www.sipri.org/" target="_blank">Stockholm International Peace Research Institute (SIPRI)</a>, who collate data from governments around the world into a Yearbook and publish <a rel="nofollow" href="https://www.sipri.org/databases/milex" target="_blank">the data</a> separately – at the time of writing, with data from 1949 to 2016, using the countries that exist in 2016 (ie no East Germany, Soviet Union, etc; data for the Russian Federation, Ukraine, etc begins in 1992 or soon after).</p>
<h2>Data exploration</h2>
<p>If we plot all the data for all the countries and years at once, it looks like this:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0099-all-countries.svg"></p>
<p>First thing you might notice is the massive spike. That turns out to be Kuwait in 1991, when it spent 17 percent <em>more</em> than its GDP on military – the kind of thing that happens when you’re invaded and become the battleground for what in the USA is referred to as the First Gulf War.</p>
<p>In case you’re wondering, there’s no reason why a country can’t spend more than its GDP on the military (or anything else). In fact, while they are both measured in dollars, GDP is not really the same sort of variable as spend. GDP is the sum of value add of economic activity in the country. If spending is on imported goods and services it can exceed GDP. Similarly, while we see economic indicators like “exports as a percentage of GDP” in use (and it is a good indicator of the importance of trade), there’s no ceiling of that indicator at 100%, and countries like <a rel="nofollow" href="http://data.worldbank.org/indicator/NE.EXP.GNFS.ZS?locations=LU-HK-SG-MT-IE&year_high_desc=true" target="_blank">Luxembourg, Singapore, Malta and Ireland do in fact exceed 100%</a>.</p>
<p>Here’s the code to download and import the SIPRI “military as a percentage of GDP” data into R and draw that first graph:</p>
<figure>
<pre><code data-lang="r"><span># Load in all the packages we need for the full post
</span><span>library</span><span>(</span><span>cshapes</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>forcats</span><span>)</span><span>
</span><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>library</span><span>(</span><span>openxlsx</span><span>)</span><span>
</span><span>library</span><span>(</span><span>directlabels</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggthemes</span><span>)</span><span>
</span><span>library</span><span>(</span><span>RColorBrewer</span><span>)</span><span>
</span><span>library</span><span>(</span><span>countrycode</span><span>)</span><span>
</span><span>library</span><span>(</span><span>maps</span><span>)</span><span>
</span><span>library</span><span>(</span><span>viridis</span><span>)</span><span>
</span><span>library</span><span>(</span><span>leaflet</span><span>)</span><span>
</span><span>library</span><span>(</span><span>rworldmap</span><span>)</span><span>
</span><span>library</span><span>(</span><span>sf</span><span>)</span><span>
</span><span>library</span><span>(</span><span>maptools</span><span>)</span><span>
</span><span>#------------download------------
</span><span>tf</span><span> </span><span>&lt;-</span><span> </span><span>tempfile</span><span>()</span><span>
</span><span>download.file</span><span>(</span><span>&quot;https://www.sipri.org/sites/default/files/SIPRI-Milex-data-1949-2016.xlsx&quot;</span><span>,</span><span>
</span><span>destfile</span><span> </span><span>=</span><span> </span><span>tf</span><span>,</span><span> </span><span>mode</span><span> </span><span>=</span><span> </span><span>&quot;wb&quot;</span><span>)</span><span>
</span><span>#---------------import and tidy----------
</span><span>sipri</span><span> </span><span>&lt;-</span><span> </span><span>read.xlsx</span><span>(</span><span>tf</span><span>,</span><span> </span><span>sheet</span><span> </span><span>=</span><span> </span><span>&quot;Share of GDP&quot;</span><span>,</span><span> </span><span>startRow</span><span> </span><span>=</span><span> </span><span>6</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>Year</span><span>,</span><span> </span><span>Value</span><span>,</span><span> </span><span>-</span><span>Country</span><span>,</span><span> </span><span>-</span><span>Notes</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>Value</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>Value</span><span>),</span><span>
</span><span>Year</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>Year</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>Value</span><span>))</span><span>
</span><span>#----------------exploratory chart-----------
</span><span>sipri</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Year</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>Value</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>Country</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</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>scale_y_continuous</span><span>(</span><span>&quot;Military expenditure as a percentage of GDP&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Countries' and regions' expenditure on military as a percentage of GDP&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;Source: Stockholm International Peace Research Institute&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)</span></code></pre>
</figure>
<p>As further exploration, I picked the five so called <a rel="nofollow" href="https://en.wikipedia.org/wiki/Five_Eyes" target="_blank">“Five Eyes” countries</a>. I have a little familiarity with their military history so thought this could be a useful way of checking I understood the data. Here is military spend as a percentage of GDP for those five countries, with major conflicts they were involved in indicated in a timeline.</p>
<p><img width="100%" src="http://ellisp.github.io/img/0099-fiveeyes.svg"></p>
<p>The dates of the conflicts are just taken from Wikipedia.</p>
<p>The spikes in spending clearly and unsurprisingly relate to conflicts or other known political events, most obviously:</p>
<ul>
<li>The Korean war led to a big spike for all four countries that had data available.</li>
<li>The surge in intensity (of their involvement) in the Vietnam war led to spikes for the USA and Australia.</li>
<li>There was a blip in UK military spending at the time of the Falklands war, but it did not arrest the overall downwards trend, even more marked since significant military conflict in Northern Ireland finished.</li>
<li>The early 1980s military boom for the USA was the Reagan administration ratcheting up the Cold War as well as militarily engaging in other countries in ways too numerous to show.</li>
<li>The wars of the George W. Bush era in Afghanistan and Iraq, attempts to create a formal “War on Terror”, temporarily reversed the secular decline in the economic importance of US military spending.</li>
</ul>
<p>Code for drawing this chart (I’m not particularly happy with the manual data entry for the conflicts and their dates, but it will do for a one-off thing like this):</p>
<figure>
<pre><code data-lang="r"><span>wid</span><span> </span><span>&lt;-</span><span> </span><span>0.01</span><span> </span><span># width of annotation bars
</span><span>wars</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>
</span><span>name</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Malaysia&quot;</span><span>,</span><span> </span><span>&quot;Korea&quot;</span><span>,</span><span> </span><span>&quot;Aden&quot;</span><span>,</span><span> </span><span>&quot;Vietnam&quot;</span><span>,</span><span> </span><span>&quot;Northern Ireland&quot;</span><span>,</span><span> </span><span>&quot;Falklands&quot;</span><span>,</span><span> </span><span>&quot;Gulf&quot;</span><span>,</span><span> </span><span>&quot;Afghanistan&quot;</span><span>,</span><span> </span><span>&quot;Iraq&quot;</span><span>),</span><span>
</span><span>start</span><span> </span><span>=</span><span> </span><span>as.Date</span><span>(</span><span>c</span><span>(</span><span>&quot;15/6/1948&quot;</span><span>,</span><span> </span><span>&quot;25/6/1950&quot;</span><span>,</span><span> </span><span>&quot;10/12/1963&quot;</span><span>,</span><span> </span><span>&quot;1/11/1955&quot;</span><span>,</span><span> </span><span>&quot;1/1/1968&quot;</span><span>,</span><span> </span><span>&quot;2/4/1982&quot;</span><span> </span><span>,</span><span> </span><span>&quot;2/8/1990&quot;</span><span>,</span><span> </span><span>&quot;7/10/2001&quot;</span><span>,</span><span> </span><span>&quot;20/3/2003&quot;</span><span>),</span><span> </span><span>format</span><span> </span><span>=</span><span> </span><span>&quot;%d/%m/%Y&quot;</span><span>),</span><span>
</span><span>end</span><span> </span><span>=</span><span> </span><span>as.Date</span><span>(</span><span>c</span><span>(</span><span>&quot;12/7/1960&quot;</span><span>,</span><span> </span><span>&quot;27/7/1953&quot;</span><span>,</span><span> </span><span>&quot;30/11/1967&quot;</span><span>,</span><span> </span><span>&quot;30/4/1975&quot;</span><span>,</span><span> </span><span>&quot;30/6/1998&quot;</span><span>,</span><span> </span><span>&quot;14/6/1982&quot;</span><span>,</span><span> </span><span>&quot;28/2/1991&quot;</span><span>,</span><span>&quot;28/12/2014&quot;</span><span>,</span><span> </span><span>&quot;18/12/2011&quot;</span><span>),</span><span>
</span><span>format</span><span> </span><span>=</span><span> </span><span>&quot;%d/%m/%Y&quot;</span><span>),</span><span>
</span><span>lead</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;UK&quot;</span><span>,</span><span> </span><span>&quot;USA&quot;</span><span>,</span><span> </span><span>&quot;UK&quot;</span><span>,</span><span> </span><span>&quot;USA&quot;</span><span>,</span><span> </span><span>&quot;UK&quot;</span><span>,</span><span> </span><span>&quot;UK&quot;</span><span>,</span><span> </span><span>&quot;USA&quot;</span><span>,</span><span> </span><span>&quot;USA&quot;</span><span>,</span><span> </span><span>&quot;USA&quot;</span><span>)</span><span>
</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>name_seq</span><span> </span><span>=</span><span> </span><span>n</span><span>()</span><span>:</span><span>1</span><span>,</span><span>
</span><span>ystart</span><span> </span><span>=</span><span> </span><span>wid</span><span> </span><span>*</span><span> </span><span>name_seq</span><span> </span><span>+</span><span> </span><span>0.12</span><span>,</span><span> </span><span>yend</span><span> </span><span>=</span><span> </span><span>ystart</span><span> </span><span>+</span><span>wid</span><span> </span><span>)</span><span>
</span><span>countries</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;Australia&quot;</span><span>,</span><span> </span><span>&quot;New Zealand&quot;</span><span>,</span><span> </span><span>&quot;USA&quot;</span><span>,</span><span> </span><span>&quot;UK&quot;</span><span>,</span><span> </span><span>&quot;Canada&quot;</span><span>)</span><span>
</span><span>palette</span><span> </span><span>&lt;-</span><span> </span><span>brewer.pal</span><span>(</span><span>5</span><span>,</span><span> </span><span>&quot;Set1&quot;</span><span>)</span><span>
</span><span>names</span><span>(</span><span>palette</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>countries</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>sipri</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>Country</span><span> </span><span>%in%</span><span> </span><span>countries</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>YearDate</span><span> </span><span>=</span><span> </span><span>as.Date</span><span>(</span><span>paste0</span><span>(</span><span>&quot;30/6/&quot;</span><span>,</span><span> </span><span>Year</span><span>),</span><span> </span><span>format</span><span> </span><span>=</span><span> </span><span>&quot;%d/%m/%Y&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_rect</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>wars</span><span>,</span><span> </span><span>aes</span><span>(</span><span>xmin</span><span> </span><span>=</span><span> </span><span>start</span><span>,</span><span> </span><span>xmax</span><span> </span><span>=</span><span> </span><span>end</span><span>,</span><span> </span><span>ymin</span><span> </span><span>=</span><span> </span><span>ystart</span><span>,</span><span> </span><span>ymax</span><span> </span><span>=</span><span> </span><span>yend</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>lead</span><span>),</span><span>
</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.2</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_text</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>wars</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>end</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>(</span><span>yend</span><span> </span><span>+</span><span> </span><span>ystart</span><span>)</span><span> </span><span>/</span><span> </span><span>2</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>name</span><span>),</span><span>
</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>,</span><span> </span><span>hjust</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>nudge_x</span><span> </span><span>=</span><span> </span><span>-200</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>YearDate</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>Value</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>Country</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;Military expenditure as a percentage of GDP&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>,</span><span>
</span><span>breaks</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>5</span><span>,</span><span> </span><span>10</span><span>,</span><span> </span><span>15</span><span>)</span><span> </span><span>/</span><span> </span><span>100</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Selected countries' expenditure on military as a percentage of GDP&quot;</span><span>,</span><span>
</span><span>&quot;'Five eyes' countries only; periods also shown for conflicts in which they had material deployments&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: Stockholm International Peace Research Institute&quot;</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>palette</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_fill_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>palette</span><span>,</span><span> </span><span>guide</span><span> </span><span>=</span><span> </span><span>&quot;none&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_tufte</span><span>(</span><span>base_family</span><span> </span><span>=</span><span> </span><span>&quot;myfont&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>plot.caption</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>))</span><span> </span><span>+</span><span> </span><span>annotate</span><span>(</span><span>&quot;text&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>as.Date</span><span>(</span><span>&quot;1970/6/30&quot;</span><span>),</span><span> </span><span>y</span><span> </span><span>=</span><span>0.145</span><span>,</span><span> </span><span>hjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span>
</span><span>label</span><span> </span><span>=</span><span> </span><span>&quot;Not shown - Grenada, Panama, Balkans,\nLibya, Lebanon, Haiti and numerous smaller...&quot;</span><span>)</span><span>
</span><span>direct.label</span><span>(</span><span>p</span><span>)</span></code></pre>
</figure>
<h2>Different world projections</h2>
<p>OK, time for maps. First I wanted to sort out some projections for flat versions of the world. <code>ggplot2</code> makes this convenient. You can define a single map object, and just add a different version of <code>+ coord_map()</code> to it to get a different projection. Here are two which worked quite nicely:</p>
<h3>Globular projection</h3>
<p>Projection with bilateral symmetry about the Prime Meridian and the equator. Hemisphere is circle, circular arc meridians equally spaced on equator, circular arc parallels equally spaced on 0- and 90-degree meridians:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0099-globular.svg"></p>
<h3>Orthographic projection</h3>
<p>Viewed from infinity, centering on 20 degrees latitude, 20 degrees longitude:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0099-ortho.svg"></p>
<h3>Code for the basic maps</h3>
<p>There’s a bit of work to be done to join the military spend data to a map data. Most importantly I need a country code. The <code>countrycode</code> package is a great example of a specialist package that does one thing well; what it does is convert country names or codes between eachother. For my use case, I want everything to be the ISO three character code (eg NZL for New Zealand). Once this is done, all that is required is to create a flat ggplot-ready version of a map shape file and join the two data frames together.</p>
<p>In the code below I create a single ggplot2 object <code>worldmap</code> which is an un-projected world choropleth map complete with the information on colour scale (I use viridis inferno), legend, captions, etc. Then I can add whatever projection I want (final two lines of code in the chunk below.</p>
<figure>
<pre><code data-lang="r"><span>#===============map prep========================
</span><span>sipri</span><span> </span><span>&lt;-</span><span> </span><span>sipri</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>iso3c</span><span> </span><span>=</span><span> </span><span>countrycode</span><span>(</span><span>Country</span><span>,</span><span> </span><span>&quot;country.name&quot;</span><span>,</span><span> </span><span>destination</span><span> </span><span>=</span><span> </span><span>&quot;iso3c&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span># two manual concordances of country code:
</span><span> </span><span>mutate</span><span>(</span><span>iso3c</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>Country</span><span> </span><span>==</span><span> </span><span>&quot;Kosovo&quot;</span><span>,</span><span> </span><span>&quot;KOS&quot;</span><span>,</span><span> </span><span>iso3c</span><span>))</span><span>
</span><span># for some reason the tidyverse doesn't work for Central African Republic! # so we fix it old school:
</span><span>sipri</span><span>[</span><span>sipri</span><span>$</span><span>Country</span><span> </span><span>==</span><span> </span><span>&quot;Central African Rep.&quot;</span><span>,</span><span> </span><span>&quot;iso3c&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;CAF&quot;</span><span>
</span><span>world</span><span> </span><span>&lt;-</span><span> </span><span>map_data</span><span>(</span><span>&quot;world&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>iso3c</span><span> </span><span>=</span><span> </span><span>countrycode</span><span>(</span><span>region</span><span>,</span><span> </span><span>&quot;country.name&quot;</span><span>,</span><span> </span><span>destination</span><span> </span><span>=</span><span> </span><span>&quot;iso3c&quot;</span><span>))</span><span>
</span><span># data on military for just the latest year for each country
</span><span>the_data</span><span> </span><span>&lt;-</span><span> </span><span>sipri</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>Country</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>Year</span><span> </span><span>==</span><span> </span><span>max</span><span>(</span><span>Year</span><span>))</span><span>
</span><span># using the help at http://ggplot2.tidyverse.org/reference/coord_map.html
</span><span>
</span><span>world2</span><span> </span><span>&lt;-</span><span> </span><span>world</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>the_data</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;iso3c&quot;</span><span>)</span><span>
</span><span># define a ggplot mapping object
</span><span>worldmap</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>world2</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>long</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>lat</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>group</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>Value</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_polygon</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey75&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;&quot;</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>(</span><span>-2</span><span>:</span><span>2</span><span>)</span><span> </span><span>*</span><span> </span><span>30</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>&quot;&quot;</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>(</span><span>-4</span><span>:</span><span>4</span><span>)</span><span> </span><span>*</span><span> </span><span>45</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_fill_viridis</span><span>(</span><span>&quot;&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>,</span><span> </span><span>option</span><span> </span><span>=</span><span> </span><span>&quot;inferno&quot;</span><span>,</span><span> </span><span>direction</span><span> </span><span>=</span><span> </span><span>-1</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_minimal</span><span>(</span><span>base_family</span><span> </span><span>=</span><span> </span><span>&quot;myfont&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;right&quot;</span><span>,</span><span>
</span><span>axis.text</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>())</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Military expenditure as a percentage of GDP&quot;</span><span>,</span><span>
</span><span>&quot;Most recent data shown for each country; mostly this is 2016&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;Source: Stockholm International Peace Research Institute&quot;</span><span>)</span><span>
</span><span>#-------------single map, nice projection-------------------
# Lots of the projections in coord_map have problems with drawing
# polygons. ok are: globular, harrison </span><span>worldmap</span><span> </span><span>+</span><span> </span><span>coord_map</span><span>(</span><span>&quot;globular&quot;</span><span>)</span><span>
</span><span>worldmap</span><span> </span><span>+</span><span> </span><span>coord_map</span><span>(</span><span>&quot;orthographic&quot;</span><span>,</span><span> </span><span>orientation</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>20</span><span>,</span><span> </span><span>20</span><span>,</span><span> </span><span>0</span><span>))</span></code></pre>
</figure>
<h2>Animated choropleth over time</h2>
<p>Remebering my reservations with the World Development Indicators site, one of my aims was to have a choropleth map that showed military expenditure as a proportion of GDP for different years, with colour on the same scale. An animated graphic is the obvious way to do this:</p>
<p><img src="https://i0.wp.com/ellisp.github.io/img/0099-military-gdp-time.gif?w=100%25" data-recalc-dims="1"></p>
<p>… but it has imperfections:</p>
<ul>
<li>Because of the few year-country combinations with massive spikes (eg Kuwait in 1991), I needed to use a logarithm transform on the scale or everything looked the same colour.</li>
<li>The slowly changing dimension of country existence proves a real problem, with data before the 1990s sadly lacking.</li>
<li>Data existence in general tends to dominate the visual; so rather than a feel for how military expenditure changes over time, the impression one gets is of more data gradually becoming available over time.</li>
</ul>
<p>Not really a success. Nonetheless, a nice idea (I think) and here’s the code that does it. It’s basically the same idea as the previous chunk of code, but in a loop that repeats the data filtering and joining for each value of year, and saves a single frame for each printed annual map.</p>
<figure>
<pre><code data-lang="r"><span>#================animated map over time===============
</span><span>setwd</span><span>(</span><span>&quot;C:/temp1&quot;</span><span>)</span><span> </span><span># or whatever folder you want to hold the frames in
</span><span>years</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>sipri</span><span>$</span><span>Year</span><span>)</span><span>
</span><span>for</span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>years</span><span>){</span><span>
</span><span>this_year_data</span><span> </span><span>&lt;-</span><span> </span><span>sipri</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>Country</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>Year</span><span> </span><span>==</span><span> </span><span>i</span><span>)</span><span>
</span><span>world2</span><span> </span><span>&lt;-</span><span> </span><span>world</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>this_year_data</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;iso3c&quot;</span><span>)</span><span>
</span><span>worldmap</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>world2</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>long</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>lat</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>group</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>Value</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_polygon</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey75&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;&quot;</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>(</span><span>-2</span><span>:</span><span>2</span><span>)</span><span> </span><span>*</span><span> </span><span>30</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>&quot;&quot;</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>(</span><span>-4</span><span>:</span><span>4</span><span>)</span><span> </span><span>*</span><span> </span><span>45</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_fill_viridis</span><span>(</span><span>&quot;&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>,</span><span> </span><span>option</span><span> </span><span>=</span><span> </span><span>&quot;inferno&quot;</span><span>,</span><span> </span><span>direction</span><span> </span><span>=</span><span> </span><span>-1</span><span>,</span><span> </span><span>limits</span><span> </span><span>=</span><span> </span><span>range</span><span>(</span><span>sipri</span><span>$</span><span>Value</span><span>),</span><span> </span><span>trans</span><span> </span><span>=</span><span> </span><span>&quot;sqrt&quot;</span><span>,</span><span>
</span><span>breaks</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>10</span><span>,</span><span> </span><span>40</span><span>,</span><span> </span><span>100</span><span>)</span><span> </span><span>/</span><span> </span><span>100</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_minimal</span><span>(</span><span>base_family</span><span> </span><span>=</span><span> </span><span>&quot;myfont&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;right&quot;</span><span>,</span><span>
</span><span>axis.text</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>())</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>paste</span><span>(</span><span>&quot;Military expenditure as a percentage of GDP -&quot;</span><span>,</span><span> </span><span>i</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;Source: Stockholm International Peace Research Institute&quot;</span><span>)</span><span>
</span><span>png</span><span>(</span><span>paste0</span><span>(</span><span>i</span><span>,</span><span> </span><span>&quot;.png&quot;</span><span>),</span><span> </span><span>12</span><span> </span><span>*</span><span> </span><span>72</span><span>,</span><span> </span><span>6</span><span> </span><span>*</span><span> </span><span>72</span><span>)</span><span>
</span><span>print</span><span>(</span><span>worldmap</span><span> </span><span>+</span><span> </span><span>coord_map</span><span>(</span><span>&quot;harrison&quot;</span><span>,</span><span> </span><span>dist</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>angle</span><span> </span><span>=</span><span> </span><span>30</span><span>,</span><span> </span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>-75</span><span>,</span><span> </span><span>85</span><span>)))</span><span>
</span><span>dev.off</span><span>()</span><span>
</span><span>}</span><span>
</span><span># Use ImageMagick to convert all those PNG frames into a single animated GIF
# (requires ImageMagick to be separately installed)
</span><span>system</span><span>(</span><span>'magick -loop 0 -delay 40 *.png &quot;0099-military-gdp-time.gif&quot;'</span><span>)</span></code></pre>
</figure>
<h2>Animated globe</h2>
<p>Finally, on to the two “good” versions of the data. Here’s a reminder of the animated globe:</p>
<div>
<center><br>
<img src="https://i2.wp.com/ellisp.github.io/img/0099-military-gdp.gif?w=456" data-recalc-dims="1"><br>
</center>
</div>
<p>The strategy is to create 360 different snapshots of the globe, each one 1 degree changed in longitude and latitude from the previous. Longitude moves 1 degree each frame in the same direction, latitude meanders from 30 degrees north to 30 degrees south and back again.</p>
<p>I had to leave the <code>ggplot2</code> universe to do this (but I’m not <em>sure</em> I had to). I couldn’t get my projections of the ggplot2 version of the globe to work with enough values of latitude and longitude as the centre point. Whenever one of the larger countries like Russia, China or Canada was split in two ggplot2 would draw the polygons in ugly ways. My best effort can be seen in <a rel="nofollow" href="https://twitter.com/ellis2013nz/status/868763709367123968" target="_blank">this tweet</a> – which also has some very useful comments and suggestions in response.</p>
<p>In the end I adapted the suggestion of Edzer Pebesma, which is included in a demo in his amazing <code>sf</code> package. I don’t really understand exactly how it successfully clips the polygons, but it works nearly entirely well.</p>
<p>There were still quite a few frames where countries went missing altogether for the wrong combination of latitude and longitude. Experimenting with different versions of world maps, I found that some were vulnerable to different combinations from others. To reduce the problem, I made each frame of the end animation actually two globes of countries drawn on top of eachother – to increase the chance that at least one of the maps draws each country. This reduced the missing country problem to only three or four frames.</p>
<p>Here’s the code that does that:</p>
<figure>
<pre><code data-lang="r"><span>#------------sf approach------------------
# this solution came from the demo in the sf package, tweeted about at
# https://twitter.com/edzerpebesma/status/835249468874321920
</span><span>circ</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>l</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>-180</span><span>:</span><span>180</span><span>),</span><span> </span><span>lon0</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>lat0</span><span> </span><span>=</span><span> </span><span>30</span><span>)</span><span> </span><span>{</span><span>
</span><span>deg2rad</span><span> </span><span>=</span><span> </span><span>pi</span><span> </span><span>/</span><span> </span><span>180</span><span>
</span><span>lat</span><span> </span><span>=</span><span> </span><span>atan</span><span>(</span><span>-</span><span>cos</span><span>((</span><span>l</span><span> </span><span>-</span><span> </span><span>lon0</span><span>)</span><span> </span><span>*</span><span> </span><span>deg2rad</span><span>)</span><span>/</span><span>tan</span><span>(</span><span>lat0</span><span> </span><span>*</span><span> </span><span>deg2rad</span><span>))</span><span> </span><span>/</span><span> </span><span>deg2rad</span><span>
</span><span>xy</span><span> </span><span>=</span><span> </span><span>if</span><span> </span><span>(</span><span>lat0</span><span> </span><span>==</span><span> </span><span>0</span><span>)</span><span> </span><span>{</span><span>
</span><span>l</span><span>1</span><span> </span><span>=</span><span> </span><span>lon0</span><span> </span><span>-</span><span> </span><span>90</span><span>
</span><span>l</span><span>2</span><span> </span><span>=</span><span> </span><span>lon0</span><span> </span><span>+</span><span> </span><span>90</span><span>
</span><span>rbind</span><span>(</span><span>c</span><span>(</span><span>l</span><span>1</span><span>,</span><span>-90</span><span>),</span><span> </span><span>c</span><span>(</span><span>l</span><span>2</span><span>,</span><span>-90</span><span>),</span><span> </span><span>c</span><span>(</span><span>l</span><span>2</span><span>,</span><span>0</span><span>),</span><span> </span><span>c</span><span>(</span><span>l</span><span>2</span><span>,</span><span>90</span><span>),</span><span> </span><span>c</span><span>(</span><span>l</span><span>1</span><span>,</span><span>90</span><span>),</span><span> </span><span>c</span><span>(</span><span>l</span><span>1</span><span>,</span><span>0</span><span>),</span><span> </span><span>c</span><span>(</span><span>l</span><span>1</span><span>,</span><span>-90</span><span>))</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>if</span><span> </span><span>(</span><span>lat0</span><span> </span><span>></span><span> </span><span>0</span><span>)</span><span> </span><span>{</span><span>
</span><span>xy</span><span> </span><span>=</span><span> </span><span>cbind</span><span>(</span><span>lon</span><span> </span><span>=</span><span> </span><span>l</span><span>,</span><span> </span><span>lat</span><span> </span><span>=</span><span> </span><span>lat</span><span>)</span><span>
</span><span>rbind</span><span>(</span><span>c</span><span>(</span><span>-180</span><span>,</span><span>90</span><span>),</span><span>xy</span><span>,</span><span>c</span><span>(</span><span>180</span><span>,</span><span>90</span><span>),</span><span>c</span><span>(</span><span>-180</span><span>,</span><span>90</span><span>))</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>xy</span><span> </span><span>=</span><span> </span><span>cbind</span><span>(</span><span>lon</span><span> </span><span>=</span><span> </span><span>l</span><span>,</span><span> </span><span>lat</span><span> </span><span>=</span><span> </span><span>lat</span><span>)[</span><span>length</span><span>(</span><span>l</span><span>)</span><span>:</span><span>1</span><span>,]</span><span>
</span><span>rbind</span><span>(</span><span>c</span><span>(</span><span>180</span><span>,</span><span>-90</span><span>),</span><span> </span><span>xy</span><span>,</span><span> </span><span>c</span><span>(</span><span>-180</span><span>,</span><span>-90</span><span>),</span><span>c</span><span>(</span><span>180</span><span>,</span><span>-90</span><span>))</span><span>
</span><span>}</span><span>
</span><span>st_sfc</span><span>(</span><span>st_polygon</span><span>(</span><span>list</span><span>(</span><span>xy</span><span>)),</span><span> </span><span>crs</span><span> </span><span>=</span><span> </span><span>st_crs</span><span>(</span><span>4326</span><span>))</span><span>
</span><span>}</span><span>
</span><span>m</span><span> </span><span>&lt;-</span><span> </span><span>st_make_grid</span><span>()</span><span>
</span><span>m</span><span> </span><span>&lt;-</span><span> </span><span>st_segmentize</span><span>(</span><span>m</span><span>,</span><span> </span><span>4e5</span><span>)</span><span>
</span><span>data</span><span>(</span><span>wrld_simpl</span><span>)</span><span>
</span><span># Two versions of the world map, joined to the military/GDP data:
</span><span>w</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>st_as_sf</span><span>(</span><span>countriesLow</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>the_data</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;ISO3&quot;</span><span> </span><span>=</span><span> </span><span>&quot;iso3c&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>pal</span><span>(</span><span>Value</span><span>))</span><span>
</span><span>w</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>st_as_sf</span><span>(</span><span>wrld_simpl</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>the_data</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;ISO3&quot;</span><span> </span><span>=</span><span> </span><span>&quot;iso3c&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>pal</span><span>(</span><span>Value</span><span>))</span><span>
</span><span># latitudes will go from 30 north to 30 south and back again:
</span><span>lats</span><span> </span><span>&lt;-</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>30</span><span>:</span><span>(</span><span>-30</span><span>),</span><span> </span><span>(</span><span>-29</span><span>)</span><span>:</span><span>29</span><span>),</span><span> </span><span>3</span><span>)</span><span>
</span><span># longitudes will start at 50 and go around backwards:
</span><span>lons</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>180</span><span>:</span><span>(</span><span>-179</span><span>))</span><span>
</span><span>dir.create</span><span>(</span><span>&quot;C:/temp2&quot;</span><span>)</span><span>
</span><span>setwd</span><span>(</span><span>&quot;C:/temp2&quot;</span><span>)</span><span>
</span><span># Frame 234 goes missing if using wrld_simpl
# Frame 269 goes missing if using countriesLow or wrld_simpl
# etc
</span><span>for</span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>length</span><span>(</span><span>lons</span><span>)){</span><span>
</span><span>png</span><span>(</span><span>paste0</span><span>(</span><span>1000</span><span> </span><span>+</span><span> </span><span>i</span><span>,</span><span> </span><span>&quot;.png&quot;</span><span>),</span><span> </span><span>600</span><span>,</span><span> </span><span>550</span><span>,</span><span> </span><span>res</span><span> </span><span>=</span><span> </span><span>100</span><span>)</span><span>
</span><span>par</span><span>(</span><span>mar</span><span> </span><span>&lt;-</span><span> </span><span>rep</span><span>(</span><span>0</span><span>,</span><span> </span><span>4</span><span>),</span><span> </span><span>bg</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>)</span><span>
</span><span>lat</span><span> </span><span>&lt;-</span><span> </span><span>lats</span><span>[</span><span>i</span><span>]</span><span>
</span><span>lon</span><span> </span><span>&lt;-</span><span> </span><span>lons</span><span>[</span><span>i</span><span>]</span><span>
</span><span># define the proj4 string:
</span><span> </span><span>p</span><span>4</span><span>s</span><span> </span><span>&lt;-</span><span> </span><span>paste0</span><span>(</span><span>&quot;+proj=ortho +lat_0=&quot;</span><span>,</span><span> </span><span>lat</span><span>,</span><span> </span><span>&quot; +lon_0=&quot;</span><span>,</span><span> </span><span>lon</span><span>)</span><span>
</span><span># draw the pale blue globe in space
</span><span> </span><span>blank_globe</span><span> </span><span>&lt;-</span><span> </span><span>st_transform</span><span>(</span><span>m</span><span>,</span><span> </span><span>st_crs</span><span>(</span><span>p</span><span>4</span><span>s</span><span>),</span><span> </span><span>check</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>plot</span><span>(</span><span>blank_globe</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>'#e6f2ff'</span><span>,</span><span> </span><span>border</span><span> </span><span>=</span><span> </span><span>'grey99'</span><span>)</span><span>
</span><span># create a clipped version of the great circle??
</span><span> </span><span># I don't really understand what is happening, but it seems to work.
</span><span> </span><span>crc</span><span> </span><span>&lt;-</span><span> </span><span>circ</span><span>(</span><span>lat0</span><span> </span><span>=</span><span> </span><span>lat</span><span>,</span><span> </span><span>lon0</span><span> </span><span>=</span><span> </span><span>lon</span><span>)</span><span>
</span><span>w</span><span>10</span><span> </span><span>&lt;-</span><span> </span><span>suppressWarnings</span><span>(</span><span>st_intersection</span><span>(</span><span>w</span><span>1</span><span>,</span><span> </span><span>crc</span><span>))</span><span>
</span><span>w</span><span>20</span><span> </span><span>&lt;-</span><span> </span><span>suppressWarnings</span><span>(</span><span>st_intersection</span><span>(</span><span>w</span><span>2</span><span>,</span><span> </span><span>crc</span><span>))</span><span>
</span><span># cast and re-project the map itself
</span><span> </span><span>w</span><span>10</span><span> </span><span>&lt;-</span><span> </span><span>st_cast</span><span>(</span><span>w</span><span>10</span><span>,</span><span> </span><span>&quot;MULTIPOLYGON&quot;</span><span>)</span><span>
</span><span>w</span><span>10</span><span> </span><span>&lt;-</span><span> </span><span>st_transform</span><span>(</span><span>w</span><span>10</span><span>[</span><span>&quot;fill&quot;</span><span>],</span><span> </span><span>st_crs</span><span>(</span><span>p</span><span>4</span><span>s</span><span>),</span><span> </span><span>check</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>w</span><span>20</span><span> </span><span>&lt;-</span><span> </span><span>st_cast</span><span>(</span><span>w</span><span>20</span><span>,</span><span> </span><span>&quot;MULTIPOLYGON&quot;</span><span>)</span><span>
</span><span>w</span><span>20</span><span> </span><span>&lt;-</span><span> </span><span>st_transform</span><span>(</span><span>w</span><span>20</span><span>[</span><span>&quot;fill&quot;</span><span>],</span><span> </span><span>st_crs</span><span>(</span><span>p</span><span>4</span><span>s</span><span>),</span><span> </span><span>check</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span># draw the map
</span><span> </span><span>plot</span><span>(</span><span>w</span><span>10</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>w</span><span>10</span><span>$</span><span>fill</span><span>,</span><span> </span><span>add</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>border</span><span> </span><span>=</span><span> </span><span>NA</span><span>)</span><span>
</span><span>plot</span><span>(</span><span>w</span><span>20</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>w</span><span>20</span><span>$</span><span>fill</span><span>,</span><span> </span><span>add</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>border</span><span> </span><span>=</span><span> </span><span>&quot;grey75&quot;</span><span>)</span><span>
</span><span># title and legend
</span><span> </span><span>title</span><span>(</span><span>&quot;Military expenditure as a percentage of GDP\nNearest year to 2016&quot;</span><span>,</span><span> </span><span>col.main</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>font.main</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>adj</span><span> </span><span>=</span><span> </span><span>0</span><span>)</span><span>
</span><span>title</span><span>(</span><span>sub</span><span> </span><span>=</span><span> </span><span>&quot;Source: Stockholm International Peace Research Institute&quot;</span><span>,</span><span> </span><span>col.sub</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>,</span><span> </span><span>adj</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>leg_nums</span><span> </span><span>&lt;-</span><span> </span><span>seq</span><span>(</span><span>from</span><span> </span><span>=</span><span> </span><span>4</span><span>,</span><span> </span><span>to</span><span> </span><span>=</span><span> </span><span>20</span><span>,</span><span> </span><span>length.out</span><span> </span><span>=</span><span> </span><span>6</span><span>)</span><span> </span><span>/</span><span> </span><span>100</span><span>
</span><span>legend</span><span>(</span><span>&quot;bottomright&quot;</span><span>,</span><span> </span><span>legend</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>round</span><span>(</span><span>leg_nums</span><span> </span><span>*</span><span> </span><span>100</span><span>),</span><span> </span><span>&quot;%&quot;</span><span>),</span><span>
</span><span>pch</span><span> </span><span>=</span><span> </span><span>15</span><span>,</span><span> </span><span>adj</span><span> </span><span>=</span><span> </span><span>0.1</span><span>,</span><span>
</span><span>col</span><span> </span><span>=</span><span> </span><span>pal</span><span>(</span><span>leg_nums</span><span>),</span><span> </span><span>text.col</span><span> </span><span>=</span><span> </span><span>pal</span><span>(</span><span>leg_nums</span><span>),</span><span>
</span><span>bg</span><span> </span><span>=</span><span> </span><span>&quot;grey80&quot;</span><span>)</span><span>
</span><span>dev.off</span><span>()</span><span>
</span><span>}</span><span>
</span><span>system</span><span>(</span><span>'magick -loop 0 -delay 7 *.png &quot;0099-military-gdp.gif&quot;'</span><span>)</span></code></pre>
</figure>
<h2>Interactive leaflet choropleth</h2>
<p>Finally, there’s the interactive version (my favourite):</p>
<p><iframe width="450" src="http://ellisp.github.io/img/0099-leaflet.html"></iframe></p>
<p>To draw this, I used <code>leaflet</code> and went back to <code>sp</code> spatial polygons data frames. The code for this is actually some of the simplest in this post. While most of the examples I’ve seen of leaflet use Mercator projections, it’s possible to reproject your map (so long as you don’t need Google or other map tiles) with a couple of lines of code:</p>
<figure>
<pre><code data-lang="r"><span>#-------------------------leaflet------------------
</span><span>shape</span><span> </span><span>&lt;-</span><span> </span><span>countriesCoarse</span><span>
</span><span># define colour palette
</span><span>pal</span><span> </span><span>&lt;-</span><span> </span><span>colorNumeric</span><span>(</span><span>
</span><span>palette</span><span> </span><span>=</span><span> </span><span>inferno</span><span>(</span><span>10</span><span>,</span><span> </span><span>direction</span><span> </span><span>=</span><span> </span><span>-1</span><span>),</span><span>
</span><span>domain</span><span> </span><span>=</span><span> </span><span>the_data</span><span>$</span><span>Value</span><span>)</span><span>
</span><span># uses the_data defined earlier in the ggplot2 demos
</span><span>data2</span><span> </span><span>&lt;-</span><span> </span><span>shape</span><span>@</span><span>data</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>the_data</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;ISO_A3&quot;</span><span> </span><span>=</span><span> </span><span>&quot;iso3c&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>tooltip</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>ADMIN</span><span>,</span><span> </span><span>&quot; &quot;</span><span>,</span><span> </span><span>Year</span><span>,</span><span> </span><span>&quot;, &quot;</span><span>,</span><span> </span><span>round</span><span>(</span><span>Value</span><span> </span><span>*</span><span> </span><span>100</span><span>,</span><span> </span><span>1</span><span>),</span><span> </span><span>&quot;%&quot;</span><span>))</span><span>
</span><span># EPSG4326 means latitude and longitude
</span><span>coarse_crs</span><span> </span><span>&lt;-</span><span> </span><span>leafletCRS</span><span>(</span><span>crsClass</span><span> </span><span>=</span><span> </span><span>&quot;L.CRS.EPSG4326&quot;</span><span>,</span><span> </span><span>proj4def</span><span> </span><span>=</span><span> </span><span>proj4string</span><span>(</span><span>countriesCoarse</span><span>))</span><span>
</span><span>shape</span><span> </span><span>%>%</span><span>
</span><span>leaflet</span><span>(</span><span>options</span><span> </span><span>=</span><span> </span><span>leafletOptions</span><span>(</span><span>crs</span><span> </span><span>=</span><span> </span><span>coarse_crs</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>addPolygons</span><span>(</span><span>stroke</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>smoothFactor</span><span> </span><span>=</span><span> </span><span>0.2</span><span>,</span><span> </span><span>fillOpacity</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>~</span><span>pal</span><span>(</span><span>data2</span><span>$</span><span>Value</span><span>),</span><span>
</span><span>label</span><span> </span><span>=</span><span> </span><span>data2</span><span>$</span><span>tooltip</span><span>)</span><span> </span></code></pre>
</figure>
<p>Happy mapping!</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="http://ellisp.github.io/blog/2017/06/04/military-gdp"> Peter's stats stuff - R</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/r3yvZXlqQcI" height="1"></a></div>