<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/maps-with-pie-charts-on-top-of-each-administrative-division-an-example-with-luxembourgs-elections-data/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.brodrigues.co/blog/2018-10-27-lux_elections_analysis/"> Econometrics and Free Software</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p><a rel="nofollow" href="https://www.youtube.com/watch?v=Bw8g_1VEEL8" target="_blank"><br>
<img src="https://i1.wp.com/www.brodrigues.co/blog/2018-10-27-lux_elections_analysis/img/europe_map_lux.png?w=456" data-recalc-dims="1"></a></p>
</div>
<div>
<h2>Abstract</h2>
<p>You can find the data used in this blog post here: <a rel="nofollow" href="https://github.com/b-rodrigues/elections_lux" target="_blank">https://github.com/b-rodrigues/elections_lux</a></p>
<p>This is a follow up to a <a rel="nofollow" href="https://www.brodrigues.co/blog/2018-10-21-lux_elections/" target="_blank">previous blog post</a><br>
where I extracted data of the 2018 Luxembourguish elections from Excel Workbooks.<br>
Now that I have the data, I will create a map of Luxembourg by commune, with pie charts of the<br>
results on top of each commune! To do this, I use good ol’ <code>{ggplot2}</code> and another packages<br>
called <code>{scatterpie}</code>. As a bonus, I have added the code to extract the data from the 2013<br>
elections from Excel. You’ll find this code in the appendix at the end of the blog post.</p>
</div>
<div>
<h2>Introduction</h2>
<p>Before importing the data for the elections of 2018, let’s install some packages:</p>
<pre><code>install.packages('rgeos', type='source') # Dependency of rgdal
install.packages('rgdal', type='source') # To read in the shapefile</code></pre>
<p>These packages might be very tricky to install on OSX and Linux, but they’re needed to import the<br>
shapefile of the country, which is needed to draw a map. So to make things easier, I have<br>
created an <code>rds</code> object, from the shapefile of Luxembourg, that you can import natively in R without<br>
needing these two packages. But if you want to use them, here is how:</p>
<pre><code>communes &lt;- readOGR(&quot;Limadmin_SHP/LIMADM_COMMUNES.shp&quot;)</code></pre>
<p>By the way, you can download the shapefile for Luxembourg <a rel="nofollow" href="https://data.public.lu/en/datasets/limites-administratives-du-grand-duche-de-luxembourg/#_" target="_blank">here</a>.</p>
<p>I’ll use my shapefile though (that you can download from the same github repo as the data):</p>
<pre><code>communes_df &lt;- readRDS(&quot;commune_shapefile.rds&quot;)</code></pre>
<p>Here’s how it looks like:</p>
<pre><code>head(communes_df)</code></pre>
<pre><code>##       long      lat order  hole piece      group       id
## 1 91057.65 101536.6     1 FALSE     1 Beaufort.1 Beaufort
## 2 91051.79 101487.3     2 FALSE     1 Beaufort.1 Beaufort
## 3 91043.43 101461.7     3 FALSE     1 Beaufort.1 Beaufort
## 4 91043.37 101449.8     4 FALSE     1 Beaufort.1 Beaufort
## 5 91040.42 101432.1     5 FALSE     1 Beaufort.1 Beaufort
## 6 91035.44 101405.6     6 FALSE     1 Beaufort.1 Beaufort</code></pre>
<p>Now let’s load some packages:</p>
<pre><code>library(&quot;tidyverse&quot;)
library(&quot;tidyxl&quot;)
library(&quot;ggplot2&quot;)
library(&quot;scatterpie&quot;)</code></pre>
<p>Ok, now, let’s import the elections results data, which is the output of<br>
<a rel="nofollow" href="https://www.brodrigues.co/blog/2018-10-21-lux_elections/" target="_blank">last week’s blog post</a>:</p>
<pre><code>elections &lt;- read_csv(&quot;elections_2018.csv&quot;)</code></pre>
<pre><code>## Parsed with column specification:
## cols(
##   Party = col_character(),
##   Year = col_integer(),
##   Variables = col_character(),
##   Values = col_double(),
##   locality = col_character(),
##   division = col_character()
## )</code></pre>
<p>I will only focus on the data at the commune level, and only use the share of votes for each party:</p>
<pre><code>elections_map &lt;- elections %>%
    filter(division == &quot;Commune&quot;,
           Variables == &quot;Pourcentage&quot;)</code></pre>
<p>Now I need to make sure that the names of the communes are the same between the elections data<br>
and the shapefile. Usual suspects are the “Haute-Sûre” and the “Redange-sur-Attert” communes,<br>
but let’s take a look:</p>
<pre><code>locality_elections &lt;- unique(elections_map$locality)
locality_shapefile &lt;- unique(communes_df$id)

setdiff(locality_elections, locality_shapefile)</code></pre>
<pre><code>## [1] &quot;Lac de la Haute-Sûre&quot; &quot;Redange Attert&quot;</code></pre>
<p>Yep, exactly as expected. I’ve had problems with the names of these two communes in the past already.<br>
Let’s rename these two communes in the elections data:</p>
<pre><code>elections_map &lt;- elections_map %>%
    mutate(commune = case_when(locality == &quot;Lac de la Haute-Sûre&quot; ~ &quot;Lac de la Haute Sûre&quot;,
                          locality == &quot;Redange Attert&quot; ~ &quot;Redange&quot;,
                          TRUE ~ locality))</code></pre>
<p>Now, I can select the relevant columns from the shapefile:</p>
<pre><code>communes_df &lt;- communes_df %>%
    select(long, lat, commune = id)</code></pre>
<p>and from the elections data:</p>
<pre><code>elections_map &lt;- elections_map %>%
    select(commune, Party, Variables, Values)</code></pre>
</div>
<div>
<h2>Plotting the data on a map</h2>
<p>Now, for the type of plot I want to make, using the <code>{scatterpie}</code> package, I need the data to be<br>
in the wide format, not long. For this I will use <code>tidyr::spread()</code>:</p>
<pre><code>elections_map &lt;- elections_map %>% 
    spread(Party, Values)</code></pre>
<p>This is how the data looks now:</p>
<pre><code>glimpse(elections_map)</code></pre>
<pre><code>## Observations: 102
## Variables: 10
## $ commune     </code></pre></div>