<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/sankey-charts-for-swinging-voters/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2017/05/21/nzes-sankey"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Continuing my examination of the individual level voting behaviour from the <a rel="nofollow" href="http://www.nzes.org/" target="_blank">New Zealand Election Study</a>, I wanted to look at the way individuals swap between parties, and between “did not vote” and a party, from one election to another. How much and how this happens is obviously an important question for both political scientists and for politicians.</p>
<h2>Vote transition visualisations</h2>
<p>I chose a Sankey chart as a way of showing the transition matrix from self-reported party vote in the 2011 election to the 2014 election. Here’s a static version:</p>
<p><img src="https://i2.wp.com/ellisp.github.io/img/0098-vote.png?w=100%25" data-recalc-dims="1"></p>
<p>And here is the more screen-friendly interactive version, with mouseover tooltips to give actual estimates:</p>
<p><iframe width="450" src="http://ellisp.github.io/img/0098-sankey.html"></iframe></p>
<p>The point with these graphics is to highlight the transitions. For example, what were the implications of turnout being higher in 2014 than 2011 (77.9% of enrolled voters in 2014 compared to 74.2% in 2011)? Judging from this survey data, the National Party gained 6.6% of the enrolled population in 2014 by converting them from a 2011 “did not vote” and lost only 3.6% in the other direction. This net gain of three percentage points was enough to win the election for the National-led coalition. In contrast, the Labour party had a net gain from “did not vote” in 2011 of only 0.2 percentage points. Remember though that these are survey-based estimates, and subject to statistical error.</p>
<p>I find setting up and polishing Sankey charts – controlling colours for example – a bit of a pain, so the code at the bottom of this post on how this was done might be of interest.</p>
<h2>Weighting, missing data, population and age complications</h2>
<p>Those visualisations have a few hidden fishhooks, which careful readers would find if they compare the percentages in the tooltips of the interactive version with percentages reported by the <a rel="nofollow" href="http://www.elections.org.nz/news-media/new-zealand-2014-general-election-official-results" target="_blank">New Zealand Electoral Commission</a>.</p>
<ul>
<li>The 2014 percentages are proportions of the <em>enrolled</em> population. As the 2014 turnout of enrolled voters was 77.9%, the numbers here are noticeably less than the usually cited percentages which were used to translate into seat counts (for example, National Party reported party vote of 47.0% of votes becomes 36.6% of enrolled voters)</li>
<li>The 2011 percentages are even harder to explain, because I’ve chosen not only to scale the party vote and “did not vote” to the 2011 enrolled population as reported by the Commission, but also to add in around 5% of the <em>2014</em> population that were too young to vote in 2011.</li>
</ul>
<p>Two things I would have liked to have taken into account but wasn’t able to were:</p>
<ul>
<li>The “leakage” from the 2011 election of people who were deceased or had left the country by 2014</li>
<li>Explicit recognition of people who voted in 2014 but not in 2011 because they were out of the country. There is a variable in the survey that picks up the year the respondent came to live in New Zealand if not born here, but for only 10 respondents was this 2012 or later (in contrast to age – there were 58 respondents aged 20 or less in 2014).</li>
</ul>
<p>I re-weighted the survey so the 2014 and 2011 reported party votes matched the known totals (with the addition of people aged 15 to 17 in 2011). One doesn’t normally re-weight a survey based on answers provided by the respondents, but in this case I think it makes perfect sense to calibrate to the public totals. The biggest impact is that for both years, but particularly 2011, relying on the respondents’ self-report and the published weighting of the NZES, totals for “did not vote” are materially understated, compared to results when calibrated to the known totals.</p>
<p>When party vote in 2011 had been forgotten or was an NA, and this wasn’t explained by being too young in 2011, I used multiple imputation based on a subset of relevant variables to give five instances of probable party vote to each such respondent.</p>
<p>Taken together, all this gives the visualisations a perspective based in 2014. It is better to think of it as “where did the 2014 voters come from” than “where did the 2011 voters go”. This is fairly natural when we consider it is the 2014 New Zealand Election Study, but is worth keeping in mind in interpretation.</p>
<p>Age (and hence the impact new young voters coming in, and of older voters passing on) is important in voting behaviour, as even the most casual observation of politics shows. In New Zealand, the age distribution of party voters in 2014 is seen in the chart below:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0098-age-densities.svg"></p>
<p>Non-voters, Green voters and to a certain extent Labour voters are young; New Zealand First voters are older. If this interests you though, I suggest you look at the multivariate analysis in <a rel="nofollow" href="http://ellisp.github.io/blog/2017/05/06/nz-first" target="_blank">this blog post</a> or, probably more fun, <a rel="nofollow" href="https://ellisp.shinyapps.io/individual-vote-nzes/" target="_blank">this fancy interactive web app</a> which lets you play with the predicted probabilities of voting based on a combination of demographic and socio-economic status variables.</p>
<h2>Code</h2>
<p>Here’s the R code that did that imputation, weighting, and the graphics:</p>
<figure>
<pre><code data-lang="r"><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>riverplot</span><span>)</span><span>
</span><span>library</span><span>(</span><span>sankeyD3</span><span>)</span><span>
</span><span>library</span><span>(</span><span>foreign</span><span>)</span><span>
</span><span>library</span><span>(</span><span>testthat</span><span>)</span><span>
</span><span>library</span><span>(</span><span>mice</span><span>)</span><span>
</span><span>library</span><span>(</span><span>grid</span><span>)</span><span>
</span><span>library</span><span>(</span><span>survey</span><span>)</span><span>
</span><span># Caution networkD3 has its own sankeyD3 with less features. Make sure you know which one you're using!
# Don't load up networkD3 in the same session
</span><span>
</span><span>#============import data======================
# See previous blog posts for where this comes from:
</span><span>unzip</span><span>(</span><span>&quot;D:/Downloads/NZES2014GeneralReleaseApril16.sav.zip&quot;</span><span>)</span><span>
</span><span>nzes_orig</span><span> </span><span>&lt;-</span><span> </span><span>read.spss</span><span>(</span><span>&quot;NZES2014GeneralReleaseApril16.sav&quot;</span><span>,</span><span> </span><span>to.data.frame</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>trim.factor.names</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span># Five versions of each row, so when we do imputation later on we
# in effect doing multiple imputation:
</span><span>nzes</span><span> </span><span>&lt;-</span><span> </span><span>nzes_orig</span><span>[</span><span>rep</span><span>(</span><span>1</span><span>:</span><span>nrow</span><span>(</span><span>nzes_orig</span><span>),</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>5</span><span>),</span><span> </span><span>]</span><span> </span><span>%>%</span><span>
</span><span># lump up party vote in 2014:
</span><span> </span><span>mutate</span><span>(</span><span>partyvote2014</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>is.na</span><span>(</span><span>dpartyvote</span><span>),</span><span> </span><span>&quot;Did not vote&quot;</span><span>,</span><span> </span><span>as.character</span><span>(</span><span>dpartyvote</span><span>)),</span><span>
</span><span>partyvote2014</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;M.ori&quot;</span><span>,</span><span> </span><span>&quot;Maori&quot;</span><span>,</span><span> </span><span>partyvote2014</span><span>),</span><span>
</span><span>partyvote2014</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;net.Man&quot;</span><span>,</span><span> </span><span>&quot;net-Man&quot;</span><span>,</span><span> </span><span>partyvote2014</span><span>),</span><span>
</span><span>partyvote2014</span><span> </span><span>=</span><span> </span><span>fct_infreq</span><span>(</span><span>partyvote2014</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>partyvote2014</span><span> </span><span>=</span><span> </span><span>fct_lump</span><span>(</span><span>partyvote2014</span><span>,</span><span> </span><span>5</span><span>))</span><span> </span><span># party vote in 2011, and a smaller set of columns to do the imputation:
</span><span>nzes2</span><span> </span><span>&lt;-</span><span> </span><span>nzes</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>partyvote2011</span><span> </span><span>=</span><span> </span><span>as.factor</span><span>(</span><span>ifelse</span><span>(</span><span>dlastpvote</span><span> </span><span>==</span><span> </span><span>&quot;Don't know&quot;</span><span>,</span><span> </span><span>NA</span><span>,</span><span> </span><span>as.character</span><span>(</span><span>dlastpvote</span><span>))))</span><span> </span><span>%>%</span><span>
</span><span># select a smaller number of variables so imputation is feasible:
</span><span> </span><span>select</span><span>(</span><span>dwtfin</span><span>,</span><span> </span><span>partyvote2014</span><span>,</span><span> </span><span>partyvote2011</span><span>,</span><span> </span><span>dethnicity_m</span><span>,</span><span> </span><span>dage</span><span>,</span><span> </span><span>dhhincome</span><span>,</span><span> </span><span>dtradeunion</span><span>,</span><span> </span><span>dprofassoc</span><span>,</span><span>
</span><span>dhousing</span><span>,</span><span> </span><span>dclass</span><span>,</span><span> </span><span>dedcons</span><span>,</span><span> </span><span>dwkpt</span><span>)</span><span>
</span><span># impute the missing values. Although we are imputing only a single set of values,
# because we are doing it with each row repeated five times this has the same impact,
# for the simple analysis we do later on, as multiple imputation:
</span><span>nzes3</span><span> </span><span>&lt;-</span><span> </span><span>complete</span><span>(</span><span>mice</span><span>(</span><span>nzes2</span><span>,</span><span> </span><span>m</span><span> </span><span>=</span><span> </span><span>1</span><span>))</span><span>
</span><span># Lump up the 2011 vote, tidy labels, and work out who was too young to vote:
</span><span>nzes4</span><span> </span><span>&lt;-</span><span> </span><span>nzes3</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>partyvote2011</span><span> </span><span>=</span><span> </span><span>fct_lump</span><span>(</span><span>partyvote2011</span><span>,</span><span> </span><span>5</span><span>),</span><span>
</span><span>partyvote2011</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>grepl</span><span>(</span><span>&quot;I did not vote&quot;</span><span>,</span><span> </span><span>partyvote2011</span><span>),</span><span> </span><span>&quot;Did not vote&quot;</span><span>,</span><span> </span><span>as.character</span><span>(</span><span>partyvote2011</span><span>)),</span><span>
</span><span>partyvote2011</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>dage</span><span> </span><span></span><span> </span><span>21</span><span>,</span><span> </span><span>&quot;Too young to vote&quot;</span><span>,</span><span> </span><span>partyvote2011</span><span>),</span><span>
</span><span>partyvote2014</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>partyvote2014</span><span>))</span><span>
</span><span>#===============re-weighting to match actual votes in 2011 and 2014===================
</span><span>
</span><span># This makes a big difference, for both years, but more for 2011. &quot;Did not vote&quot; is only 16% in 2011
# if we only calibrate to 2014 voting outcomes, but in reality was 25.8%. We calibrate for both
# 2011 and 2014 so the better proportions for both elections.
</span><span>
</span><span>#------------2014 actual outcomes----------------
# http://www.elections.org.nz/news-media/new-zealand-2014-general-election-official-results
</span><span>actual_vote_2014</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>
</span><span>partyvote2014</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;National&quot;</span><span>,</span><span> </span><span>&quot;Labour&quot;</span><span>,</span><span> </span><span>&quot;Green&quot;</span><span>,</span><span> </span><span>&quot;NZ First&quot;</span><span>,</span><span> </span><span>&quot;Other&quot;</span><span>,</span><span> </span><span>&quot;Did not vote&quot;</span><span>),</span><span>
</span><span>Freq</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1131501</span><span>,</span><span> </span><span>604534</span><span>,</span><span> </span><span>257356</span><span>,</span><span> </span><span>208300</span><span>,</span><span> </span><span>31850</span><span> </span><span>+</span><span> </span><span>16689</span><span> </span><span>+</span><span> </span><span>5286</span><span> </span><span>+</span><span> </span><span>95598</span><span> </span><span>+</span><span> </span><span>34095</span><span> </span><span>+</span><span> </span><span>10961</span><span> </span><span>+</span><span> </span><span>5113</span><span> </span><span>+</span><span> </span><span>1730</span><span> </span><span>+</span><span> </span><span>1096</span><span> </span><span>+</span><span> </span><span>872</span><span> </span><span>+</span><span> </span><span>639</span><span>,</span><span>
</span><span>NA</span><span>)</span><span>
</span><span>)</span><span>
</span><span># calculate the did not vote, from the 77.9 percent turnout
</span><span>actual_vote_2014</span><span>[</span><span>6</span><span>,</span><span> </span><span>2</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>(</span><span>100</span><span> </span><span>/</span><span> </span><span>77.9</span><span> </span><span>-</span><span> </span><span>1</span><span>)</span><span> </span><span>*</span><span> </span><span>sum</span><span>(</span><span>actual_vote_2014</span><span>[</span><span>1</span><span>:</span><span>5</span><span>,</span><span> </span><span>2</span><span>])</span><span>
</span><span># check I did the turnout sums right:
</span><span>expect_equal</span><span>(</span><span>0.779</span><span> </span><span>*</span><span> </span><span>sum</span><span>(</span><span>actual_vote_2014</span><span>[</span><span> </span><span>,</span><span>2</span><span>]),</span><span> </span><span>sum</span><span>(</span><span>actual_vote_2014</span><span>[</span><span>1</span><span>:</span><span>5</span><span>,</span><span> </span><span>2</span><span>]))</span><span>
</span><span>#---------------2011 actual outcomes---------------------
# http://www.elections.org.nz/events/past-events-0/2011-general-election/2011-general-election-official-results
</span><span>actual_vote_2011</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>
</span><span>partyvote2011</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;National&quot;</span><span>,</span><span> </span><span>&quot;Labour&quot;</span><span>,</span><span> </span><span>&quot;Green&quot;</span><span>,</span><span> </span><span>&quot;NZ First&quot;</span><span>,</span><span> </span><span>&quot;Other&quot;</span><span>,</span><span> </span><span>&quot;Did not vote&quot;</span><span>,</span><span> </span><span>&quot;Too young to vote&quot;</span><span>),</span><span>
</span><span>Freq</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1058636</span><span>,</span><span> </span><span>614937</span><span>,</span><span> </span><span>247372</span><span>,</span><span> </span><span>147511</span><span>,</span><span> </span><span>31982</span><span> </span><span>+</span><span> </span><span>24168</span><span> </span><span>+</span><span> </span><span>23889</span><span> </span><span>+</span><span> </span><span>13443</span><span> </span><span>+</span><span> </span><span>59237</span><span> </span><span>+</span><span> </span><span>11738</span><span> </span><span>+</span><span> </span><span>1714</span><span> </span><span>+</span><span> </span><span>1595</span><span> </span><span>+</span><span> </span><span>1209</span><span>,</span><span>
</span><span>NA</span><span>,</span><span> </span><span>NA</span><span>)</span><span>
</span><span>)</span><span>
</span><span># calculate the did not vote, from the 74.21 percent turnout at # http://www.elections.org.nz/events/past-events/2011-general-election
</span><span>actual_vote_2011</span><span>[</span><span>6</span><span>,</span><span> </span><span>2</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>(</span><span>100</span><span> </span><span>/</span><span> </span><span>74.21</span><span> </span><span>-</span><span> </span><span>1</span><span>)</span><span> </span><span>*</span><span> </span><span>sum</span><span>(</span><span>actual_vote_2011</span><span>[</span><span>1</span><span>:</span><span>5</span><span>,</span><span> </span><span>2</span><span>])</span><span>
</span><span># check I did the turnout sums right:
</span><span>expect_equal</span><span>(</span><span>0.7421</span><span> </span><span>*</span><span> </span><span>sum</span><span>(</span><span>actual_vote_2011</span><span>[</span><span>1</span><span>:</span><span>6</span><span> </span><span>,</span><span>2</span><span>]),</span><span> </span><span>sum</span><span>(</span><span>actual_vote_2011</span><span>[</span><span>1</span><span>:</span><span>5</span><span>,</span><span> </span><span>2</span><span>]))</span><span>
</span><span># from the below, we conclude 4.8% of the 2014 population (as estimated by NZES)
# were too young to vote in 2011:
</span><span>nzes_orig</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>tooyoung</span><span> </span><span>=</span><span> </span><span>dage</span><span> </span><span></span><span> </span><span>21</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>tooyoung</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>pop</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>dwtfin</span><span>),</span><span>
</span><span>n</span><span> </span><span>=</span><span> </span><span>n</span><span>())</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>prop</span><span> </span><span>=</span><span> </span><span>pop</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>pop</span><span>))</span><span>
</span><span># this is pretty approximate but will do for our purposes.
</span><span>actual_vote_2011</span><span>[</span><span>7</span><span>,</span><span> </span><span>2</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>0.048</span><span> </span><span>*</span><span> </span><span>sum</span><span>(</span><span>actual_vote_2011</span><span>[</span><span>1</span><span>:</span><span>6</span><span>,</span><span> </span><span>2</span><span>])</span><span>
</span><span># Force the 2011 numbers to match the 2014 population
</span><span>actual_vote_2011</span><span>$</span><span>Freq</span><span> </span><span>&lt;-</span><span> </span><span>actual_vote_2011</span><span>$</span><span>Freq</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>actual_vote_2011</span><span>$</span><span>Freq</span><span>)</span><span> </span><span>*</span><span> </span><span>sum</span><span>(</span><span>actual_vote_2014</span><span>$</span><span>Freq</span><span>)</span><span>
</span><span>#------------create new weights--------------------
# set up survey design with the original weights:
</span><span>nzes_svy</span><span> </span><span>&lt;-</span><span> </span><span>svydesign</span><span>(</span><span>~</span><span>1</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>nzes4</span><span>,</span><span> </span><span>weights</span><span> </span><span>=</span><span> </span><span>~</span><span>dwtfin</span><span>)</span><span>
</span><span># calibrate weights to the known total party votes in 2011 and 2014:
</span><span>nzes_cal</span><span> </span><span>&lt;-</span><span> </span><span>calibrate</span><span>(</span><span>nzes_svy</span><span>,</span><span> </span><span>list</span><span>(</span><span>~</span><span>partyvote2014</span><span>,</span><span> </span><span>~</span><span>partyvote2011</span><span>),</span><span>
</span><span>list</span><span>(</span><span>actual_vote_2014</span><span>,</span><span> </span><span>actual_vote_2011</span><span>))</span><span>
</span><span># extract those weights for use in straight R (not just &quot;survey&quot;)
</span><span>nzes5</span><span> </span><span>&lt;-</span><span> </span><span>nzes4</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>weight</span><span> </span><span>=</span><span> </span><span>weights</span><span>(</span><span>nzes_cal</span><span>))</span><span>
</span><span># See impact of calibrated weights for the 2014 vote:
</span><span>prop.table</span><span>(</span><span>svytable</span><span>(</span><span>~</span><span>partyvote2014</span><span>,</span><span> </span><span>nzes_svy</span><span>))</span><span> </span><span># original weights
</span><span>prop.table</span><span>(</span><span>svytable</span><span>(</span><span>~</span><span>partyvote2014</span><span>,</span><span> </span><span>nzes_cal</span><span>))</span><span> </span><span># recalibrated weights
</span><span>
</span><span># See impact of calibrated weights for the 2011 vote:
</span><span>prop.table</span><span>(</span><span>svytable</span><span>(</span><span>~</span><span>partyvote2011</span><span>,</span><span> </span><span>nzes_svy</span><span>))</span><span> </span><span># original weights
</span><span>prop.table</span><span>(</span><span>svytable</span><span>(</span><span>~</span><span>partyvote2011</span><span>,</span><span> </span><span>nzes_cal</span><span>))</span><span> </span><span># recalibrated weights
</span><span>
</span><span>#=======================previous years vote riverplot version============
</span><span>
</span><span>the_data</span><span> </span><span>&lt;-</span><span> </span><span>nzes5</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>
</span><span># add two spaces to ensure the partyvote2011 does not have any
</span><span> </span><span># names that exactly match the party vote in 2014
</span><span> </span><span>partyvote2011</span><span> </span><span>=</span><span> </span><span>paste</span><span>(</span><span>partyvote2011</span><span>,</span><span> </span><span>&quot; &quot;</span><span>),</span><span>
</span><span>partyvote2011</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;M.ori&quot;</span><span>,</span><span> </span><span>&quot;Maori&quot;</span><span>,</span><span> </span><span>partyvote2011</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>partyvote2011</span><span>,</span><span> </span><span>partyvote2014</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>vote_prop</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>weight</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span># change names to the names wanted by makeRiver
</span><span>names</span><span>(</span><span>the_data</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;col1&quot;</span><span>,</span><span> </span><span>&quot;col2&quot;</span><span>,</span><span> </span><span>&quot;Value&quot;</span><span>)</span><span>
</span><span># node ID need to be characters I think
</span><span>c</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>the_data</span><span>$</span><span>col1</span><span>)</span><span>
</span><span>c</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>the_data</span><span>$</span><span>col2</span><span>)</span><span>
</span><span>nodes_ref</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>fullname</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>c</span><span>1</span><span>,</span><span> </span><span>c</span><span>2</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>position</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>2</span><span>),</span><span> </span><span>times</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>length</span><span>(</span><span>c</span><span>1</span><span>),</span><span> </span><span>length</span><span>(</span><span>c</span><span>2</span><span>))))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>ID</span><span> </span><span>=</span><span> </span><span>LETTERS</span><span>[</span><span>1</span><span>:</span><span>n</span><span>()])</span><span>
</span><span>edges</span><span> </span><span>&lt;-</span><span> </span><span>the_data</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>nodes_ref</span><span>[</span><span> </span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;fullname&quot;</span><span>,</span><span> </span><span>&quot;ID&quot;</span><span>)],</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;col1&quot;</span><span> </span><span>=</span><span> </span><span>&quot;fullname&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>N</span><span>1</span><span> </span><span>=</span><span> </span><span>ID</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>nodes_ref</span><span>[</span><span> </span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;fullname&quot;</span><span>,</span><span> </span><span>&quot;ID&quot;</span><span>)],</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;col2&quot;</span><span> </span><span>=</span><span> </span><span>&quot;fullname&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>N</span><span>2</span><span> </span><span>=</span><span> </span><span>ID</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>as.data.frame</span><span>(</span><span>stringsAsFactors</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>rp</span><span> </span><span>&lt;-</span><span> </span><span>makeRiver</span><span>(</span><span>nodes</span><span> </span><span>=</span><span> </span><span>as.vector</span><span>(</span><span>nodes_ref</span><span>$</span><span>ID</span><span>),</span><span> </span><span>edges</span><span> </span><span>=</span><span> </span><span>edges</span><span>,</span><span>
</span><span>node_labels</span><span> </span><span>=</span><span> </span><span>nodes_ref</span><span>$</span><span>fullname</span><span>,</span><span>
</span><span># manual vertical positioning by parties. Look at
</span><span> </span><span># nodes_ref to see the order in which positions are set. </span><span> </span><span># This is a pain, so I just let it stay with the defaults:
</span><span> </span><span># node_ypos = c(4, 1, 1.8, 3, 6, 7, 5, 4, 1, 1.8, 3, 6, 7),
</span><span> </span><span>node_xpos</span><span> </span><span>=</span><span> </span><span>nodes_ref</span><span>$</span><span>position</span><span>,</span><span>
</span><span># set party colours; all based on those in nzelect::parties_v:
</span><span> </span><span>node_styles</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>C</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;#d82a20&quot;</span><span>),</span><span> </span><span># red labour
</span><span> </span><span>D</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;#00529F&quot;</span><span>),</span><span> </span><span># blue national
</span><span> </span><span>E</span><span>=</span><span> </span><span>list</span><span>(</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>),</span><span> </span><span># black NZFirst
</span><span> </span><span>B</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;#098137&quot;</span><span>),</span><span> </span><span># green
</span><span> </span><span>J</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;#d82a20&quot;</span><span>),</span><span> </span><span># labour
</span><span> </span><span>I</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;#098137&quot;</span><span>),</span><span> </span><span># green
</span><span> </span><span>K</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;#00529F&quot;</span><span>),</span><span> </span><span># national
</span><span> </span><span>L</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>)))</span><span> </span><span># NZ First
</span><span>
</span><span># Turn the text horizontal, and pale grey
</span><span>ds</span><span> </span><span>&lt;-</span><span> </span><span>default.style</span><span>()</span><span>
</span><span>ds</span><span>$</span><span>srt</span><span> </span><span>&lt;-</span><span> </span><span>0</span><span>
</span><span>ds</span><span>$</span><span>textcol</span><span> </span><span>&lt;-</span><span> </span><span>&quot;grey95&quot;</span><span>
</span><span>mygp</span><span> </span><span>&lt;-</span><span> </span><span>gpar</span><span>(</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;grey75&quot;</span><span>)</span><span>
</span><span># using PNG rather than SVG as vertical lines appear in the SVG version
</span><span>
</span><span>par</span><span>(</span><span>bg</span><span> </span><span>=</span><span> </span><span>&quot;grey40&quot;</span><span>)</span><span>
</span><span># note the plot_area argument - for some reason, defaults to using only half the
# vertical space available, so set this to higher than 0.5!:
</span><span>plot</span><span>(</span><span>rp</span><span>,</span><span> </span><span>default_style</span><span> </span><span>=</span><span> </span><span>ds</span><span>,</span><span> </span><span>plot_area</span><span> </span><span>=</span><span> </span><span>0.9</span><span>)</span><span>
</span><span>title</span><span>(</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Self-reported party vote in 2011 compared to 2014&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>grid.text</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>0.15</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>0.1</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>&quot;2011 party vote&quot;</span><span>,</span><span> </span><span>gp</span><span> </span><span>=</span><span> </span><span>mygp</span><span>)</span><span>
</span><span>grid.text</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>0.85</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>0.1</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>&quot;2014 party vote&quot;</span><span>,</span><span> </span><span>gp</span><span> </span><span>=</span><span> </span><span>mygp</span><span>)</span><span>
</span><span>grid.text</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>0.95</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>0.03</span><span>,</span><span> </span><span>gp</span><span> </span><span>=</span><span> </span><span>gpar</span><span>(</span><span>fontsize</span><span> </span><span>=</span><span> </span><span>7</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;grey75&quot;</span><span>),</span><span> </span><span>just</span><span> </span><span>=</span><span> </span><span>&quot;right&quot;</span><span>,</span><span>
</span><span>label</span><span> </span><span>=</span><span> </span><span>&quot;Source: New Zealand Election Study, analysed at https://ellisp.github.io&quot;</span><span>)</span><span>
</span><span>#=======================sankeyD3 version====================
</span><span>
</span><span>nodes_ref2</span><span> </span><span>&lt;-</span><span> </span><span>nodes_ref</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>ID</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>as.factor</span><span>(</span><span>ID</span><span>))</span><span> </span><span>-</span><span> </span><span>1</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>as.data.frame</span><span>()</span><span>
</span><span>edges2</span><span> </span><span>&lt;-</span><span> </span><span>the_data</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>nodes_ref2</span><span>[</span><span> </span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;fullname&quot;</span><span>,</span><span> </span><span>&quot;ID&quot;</span><span>)],</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;col1&quot;</span><span> </span><span>=</span><span> </span><span>&quot;fullname&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>N</span><span>1</span><span> </span><span>=</span><span> </span><span>ID</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>nodes_ref2</span><span>[</span><span> </span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;fullname&quot;</span><span>,</span><span> </span><span>&quot;ID&quot;</span><span>)],</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;col2&quot;</span><span> </span><span>=</span><span> </span><span>&quot;fullname&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>N</span><span>2</span><span> </span><span>=</span><span> </span><span>ID</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>as.data.frame</span><span>(</span><span>stringsAsFactors</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>Value</span><span> </span><span>=</span><span> </span><span>Value</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>Value</span><span>)</span><span> </span><span>*</span><span> </span><span>100</span><span>)</span><span>
</span><span>pal</span><span> </span><span>&lt;-</span><span> </span><span>'d3.scaleOrdinal()
.range([&quot;#DCDCDC&quot;, &quot;#098137&quot;, &quot;#d82a20&quot;, &quot;#00529F&quot;, &quot;#000000&quot;, &quot;#DCDCDC&quot;, &quot;#DCDCDC&quot;, &quot;#098137&quot;, &quot;#d82a20&quot;, &quot;#00529F&quot;, &quot;#000000&quot;, &quot;#DCDCDC&quot;]);'</span><span>
</span><span>sankeyNetwork</span><span>(</span><span>Links</span><span> </span><span>=</span><span> </span><span>edges2</span><span>,</span><span> </span><span>Nodes</span><span> </span><span>=</span><span> </span><span>nodes_ref2</span><span>,</span><span> </span><span>Source</span><span> </span><span>=</span><span> </span><span>&quot;N1&quot;</span><span>,</span><span> </span><span>Target</span><span> </span><span>=</span><span> </span><span>&quot;N2&quot;</span><span>,</span><span> </span><span>Value</span><span> </span><span>=</span><span> </span><span>&quot;Value&quot;</span><span>,</span><span>
</span><span>NodeID</span><span> </span><span>=</span><span> </span><span>&quot;fullname&quot;</span><span>,</span><span>
</span><span>NodeGroup</span><span> </span><span>=</span><span> </span><span>&quot;fullname&quot;</span><span>,</span><span>
</span><span>LinkGroup</span><span> </span><span>=</span><span> </span><span>&quot;col2&quot;</span><span>,</span><span>
</span><span>fontSize</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>nodeWidth</span><span> </span><span>=</span><span> </span><span>30</span><span>,</span><span>
</span><span>colourScale</span><span> </span><span>=</span><span> </span><span>JS</span><span>(</span><span>pal</span><span>),</span><span>
</span><span>numberFormat</span><span> </span><span>=</span><span> </span><span>JS</span><span>(</span><span>'d3.format(&quot;.1f&quot;)'</span><span>),</span><span>
</span><span>fontFamily</span><span> </span><span>=</span><span> </span><span>&quot;Calibri&quot;</span><span>,</span><span> </span><span>units</span><span> </span><span>=</span><span> </span><span>&quot;%&quot;</span><span>,</span><span> </span><span>nodeShadow</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>showNodeValues</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span>width</span><span> </span><span>=</span><span> </span><span>700</span><span>,</span><span> </span><span>height</span><span> </span><span>=</span><span> </span><span>500</span><span>)</span><span> </span><span>#=======other by the by analysis==================
# Age density plot by party vote
</span><span>
</span><span># Remember to weight by the survey weights - in this case it controls for
# the under or over sampling by age in the original design.
</span><span>nzes5</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>dage</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>partyvote2014</span><span>,</span><span> </span><span>weight</span><span> </span><span>=</span><span> </span><span>weight</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>nzes5</span><span>$</span><span>weight</span><span>)))</span><span> </span><span>+</span><span>
</span><span>geom_density</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.3</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>partyvote2014</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</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>parties_v</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>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Age at time of 2014 election&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: New Zealand Election Study&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Age distribution by Party Vote in the 2014 New Zealand General Election&quot;</span><span>)</span></code></pre>
</figure>
<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/05/21/nzes-sankey"> 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/OQR1s8Ut4_E" height="1"></a></div>