<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/new-zealand-election-study-individual-level-data/"></div></div><div><a data-url="https://www.r-bloggers.com/new-zealand-election-study-individual-level-data/" data-counturl="https://www.r-bloggers.com/new-zealand-election-study-individual-level-data/" data-count="horizontal" data-text="New Zealand Election Study individual level data" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2016/09/18/nzes1"> 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>Individual level data is essential to understand voting behaviour</h2>
<p>My previous analysis has occasionally come up against the problem <a target="_blank" href="http://ellisp.github.io/blog/2016/04/16/nzelect4">“only individual level data could resolve that,”</a>. Since I last wrote that, the New Zealand Election Study data for the 2014 General Election have become available, and this post is my first glance at it. The <a target="_blank" href="http://www.nzes.org/">New Zealand Election Study</a> makes available data on nine general elections (back to 1990) and looks to be a great resource. Some time in the last half dozen years they took a decision to publish it all pro-actively with minimal (nearly zero) gate-keeping, which is a great thing.</p>
<p><em>Caveat: I have no association whatsover with the New Zealand Election Study. Any errors are mine and in fact I looked at the actual data for the first time today, so treat anything I say with caution.</em></p>
<h2>How to get the data</h2>
<p>The data are available from the <a target="_blank" href="http://www.nzes.org/">New Zealand Election Study</a> website in SPSS format. There is a brief online form to fill in so they can keep track of who is using their data. Because of this, I’m not planning on publishing a copy of the data or incorporating it into the <code>nzelect</code> R package. The download is straightforward with a minimum of red tape (just the one form, no credentials or gate-keeping). To get it into R, the code below will work with just the necessary tweak to wherever the zip file containing the 2014 data has been saved.</p>
<figure>
<pre><code data-lang="r"><span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>&quot;hadley/ggplot2&quot;</span><span>)</span><span> </span><span># for dev version with subtitles and captions
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>library</span><span>(</span><span>foreign</span><span>)</span><span>
</span><span>library</span><span>(</span><span>survey</span><span>)</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tidyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>forcats</span><span>)</span><span>
</span><span>library</span><span>(</span><span>gridExtra</span><span>)</span><span>
</span><span># Data downloaded from http://www.nzes.org/exec/show/data and because
# they want you to fill in a form to know who is using the data, I
# won't re-publish it myself
</span><span>unzip</span><span>(</span><span>&quot;D:/Downloads/NZES2014GeneralReleaseApril16.sav.zip&quot;</span><span>)</span><span>
</span><span>nzes</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>varlab</span><span> </span><span>&lt;-</span><span> </span><span>cbind</span><span>(</span><span>attributes</span><span>(</span><span>nzes</span><span>)</span><span>$</span><span>variable.labels</span><span>)</span></code></pre>
</figure>
<p>The data have 2,835 rows and 438 columns. The column names are the variable names from the original SPSS file. In the code above, I extract the more verbose “variable labels” that each column refers to and store them in a data frame with a single column, <code>varlab</code>. The entries in <code>varlab</code> can now be referred to by their row name, for example:</p>
<figure>
<pre><code data-lang="r"><span>></span><span> </span><span>varlab</span><span>[</span><span>&quot;dredincome&quot;</span><span>,]</span><span>
</span><span>dredincome</span><span> </span><span>&quot;E17: how likely that your household's income could be severely reduced in the next 12 months&quot;</span><span> </span></code></pre>
</figure>
<p>There’s an exciting range of questions in here, and when I’ve got my head around them I’ll be doing some interesting analysis; particularly once I get efficient code to join it up with the Census data in my <a target="_blank" href="http://ellisp.github.io/blog/2016/08/04/nzcensus-gam-elastic-lm"><code>nzcensus</code> package</a>. I think that much of the analysis by others to date has been with SPSS, so I’ll publish any useful R code I develop for others to use too.</p>
<h2>Weights</h2>
<p>The data have weights to correct for:</p>
<ul>
<li>deliberate over-sampling (I think of Maori, although I haven’t yet tracked down a definitive description of the sample design)</li>
<li>accidental (ie from response rate) disproportionate sampling by age, gender and education</li>
<li>disproportionate representation of voters and non-voters</li>
</ul>
<p>I’m pretty sure the correct final weight to use is the column <code>dwtfin</code>.</p>
<p>Note – the weight column sums to the sample size (not to the population). This minimises the chance of an SPSS-related disaster. Unless the specialist complex surveys module is paid for and used, SPSS interprets weights as frequencies, and hence will give completely wrong standard errors and confidence intervals if the weights add up to anything other than sample size. I think that they are still wrong when the weights add up to sample size, but not by an order of magnitude! (SPSS’ limitations in this regard were an important reason in moving to R at my work in 2011, but I digress.)</p>
<p>Here’s my prep code before doing further analysis. When I get into this for real, this is likely to be much bigger, and I’ll develop special script/s specifically to do data cleaning and tidying. For now, all I do is make a summary variable that groups party vote for some of the smaller parties into an “other party” category; and set up the data with a survey design object using <a target="_blank" href="https://cran.r-project.org/package=survey">Thomas Lumley’s <code>survey</code> package</a>, which is the go-to point for anyone studying complex (ie weighted) surveys in R.</p>
<figure>
<pre><code data-lang="r"><span># Main parties, in rough sequence of conservative to progressive. Don't complain to me if you don't like the ordering.
</span><span>mainparties</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;No Vote&quot;</span><span>,</span><span> </span><span>&quot;Conservative&quot;</span><span>,</span><span> </span><span>&quot;National&quot;</span><span>,</span><span> </span><span>&quot;NZ First&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;Internet?Mana Party&quot;</span><span>,</span><span> </span><span>&quot;Other party&quot;</span><span>)</span><span>
</span><span>nzes</span><span> </span><span>&lt;-</span><span> </span><span>nzes</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>vpartysum</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>nzes</span><span>$</span><span>dvpartyvote</span><span> </span><span>%in%</span><span> </span><span>mainparties</span><span>,</span><span> </span><span>as.character</span><span>(</span><span>nzes</span><span>$</span><span>dvpartyvote</span><span>),</span><span> </span><span>&quot;Other party&quot;</span><span>),</span><span>
</span><span>vpartysum</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>vpartysum</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>mainparties</span><span>),</span><span>
</span><span>vpartysum</span><span> </span><span>=</span><span> </span><span>fct_recode</span><span>(</span><span>vpartysum</span><span>,</span><span> </span><span>&quot;Internet /\nMana Party&quot;</span><span> </span><span>=</span><span> </span><span>&quot;Internet?Mana Party&quot;</span><span>))</span><span>
</span><span># create a survey design object for easy of analysing with correct weights, etc - # this is particularly important if doing modelling down the track
</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>nzes</span><span>,</span><span> </span><span>weights</span><span> </span><span>=</span><span> </span><span>~</span><span>dwtfin</span><span>)</span></code></pre>
</figure>
<p>New Zealanders in particularly will be interested in the answer to this question, presumably asked as a proxy of how closely people follow politics:</p>
<figure>
<pre><code data-lang="r"><span>></span><span> </span><span>varlab</span><span>[</span><span>&quot;dfinance&quot;</span><span>,</span><span> </span><span>]</span><span>
</span><span>dfinance</span><span> </span><span>&quot;E7: who was minister of finance before 2014 general election&quot;</span><span> </span><span>></span><span> </span><span>svytable</span><span>(</span><span>~</span><span>dfinance</span><span>,</span><span> </span><span>design</span><span> </span><span>=</span><span> </span><span>nzes_svy</span><span>,</span><span> </span><span>round</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>dfinance</span><span>
</span><span>Judith</span><span> </span><span>Collins</span><span> </span><span>Bill</span><span> </span><span>English</span><span> </span><span>Tony</span><span> </span><span>Ryall</span><span> </span><span>Nick</span><span> </span><span>Smith</span><span> </span><span>Don</span><span>'</span><span>t</span><span> </span><span>know</span><span> </span><span>64</span><span> </span><span>2298</span><span> </span><span>6</span><span> </span><span>13</span><span> </span><span>404</span><span> </span></code></pre>
</figure>
<p>Most people who ventured an opinion got it right; about one in seven didn’t know.</p>
<h2>Example analysis – perceptions of Nicky Hagar’s <a target="_blank" href="https://dirtypoliticsnz.com/about/"><em>Dirty Politics</em> book</a></h2>
<p>I was interested to see that one of the questions was:</p>
<p><code>&quot;B12: how much truth in Nicky Hager's Dirty Politics book&quot; </code></p>
<p>Non New Zealanders may wish to read the <a target="_blank" href="https://en.wikipedia.org/wiki/Dirty_Politics">Wikipedia article on <em>Dirty Politics</em></a> for context. The book was released a bit over a month before the 2014 election and at times dominated the news leading up to it. It had a number of revelations, critical in particular of the National Party (which was in government prior to the 2014 election, and won again in 2014), focusing on its relations with right wing bloggers. It’s not surprising that political scientists wanted to collect data about voters’ views of the book in relation to voting behaviour.</p>
<h3>Complex relations of categorical variables via a mosaic plot</h3>
<p>I thought I’d use this as my first bit of familiarisation with the data, and I started with this graphic:</p>
<p><img alt="mosaic" src="http://ellisp.github.io/img/0057-mosaic.svg"></p>
<p>This is a mosaic plot, which is used to show relationships between categorical variables. The columns show individuals’ party vote, and the number of boxes down shows the view of Mr Hagar’s book. For example, the large blue rectangle second from the top under the “National” heading represents survey respondents who said they party-voted National, and thought there was “a little truth” in Mr Hagar’s book. In addition:</p>
<ul>
<li>The <em>size</em> of each rectangle is proportionate to the size of total number of voters estimated to be in that combination of views and voting behaviour.</li>
<li>The <em>colour</em> of each rectangle indicates how much that combination of variables differs from what would be expected if the two variables (voting, and view of the book) were unrelated. Blue indicates “surprisingly large number” and red indicates “surprisingly small number” – where ‘surprise’ means “not predicted by the null model of no relationship”.</li>
</ul>
<p>We can see a pattern of views pretty much along party lines. Compared to the null hypothesis of independence (which of course was never plausible in this instance), there are ‘surprisingly’ many National Voters who think there is little or no truth, and suprisingly few who think there is some or a lot of truth. The pattern is reversed for Labour, Green and Internet / Mana voters, none of which would <em>really</em> surprise anyone who was following the news at all during the election campaign. Interestingly, there seems no relation between the view of the <em>Dirty Politics</em> book and voting Conservative (warning for overseas readers – the Conservative Party in New Zealand is much smaller and newer than its UK namesake).</p>
<p>Interestingly, there’s a noticeably large number of non-voters in the “some truth” category, which could fit in with a frustration / cynicism / “pox on both your houses” narrative. Relatively few non-voters thought there was “no truth” in Mr Hagar’s book.</p>
<p>Here’s the code to make this graphic.</p>
<figure>
<pre><code data-lang="r"><span>#-------------Example analayis - attitudes to the Dirty Politics book---------------
</span><span>dirtypol</span><span> </span><span>&lt;-</span><span> </span><span>svytable</span><span>(</span><span>~</span><span> </span><span>vpartysum</span><span> </span><span>+</span><span> </span><span>ddirtypol</span><span>,</span><span> </span><span>nzes_svy</span><span>)</span><span>
</span><span># Mosaic plot. Difficult for non-specialists to interpret
</span><span>mosaicplot</span><span>(</span><span>dirtypol</span><span>[</span><span> </span><span>,</span><span>-5</span><span>],</span><span> </span><span>shade</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>las</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span> </span><span>ylab</span><span> </span><span>=</span><span> </span><span>varlab</span><span>[</span><span>&quot;ddirtypol&quot;</span><span>,</span><span> </span><span>],</span><span> </span><span>xlab</span><span> </span><span>=</span><span> </span><span>&quot;Party vote&quot;</span><span>,</span><span> </span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)</span></code></pre>
</figure>
<h3>.. and via bar charts</h3>
<p>The mosaic plot is a nice graphic and I often use mosaic plots in exploratory and analytical stages of an analytical project; but rarely is it a good tool for communication to people who aren’t specialists. A better graphic for that purpose is this one, using the same information but in two plots that use more well-known format:</p>
<p><img alt="bars" src="http://ellisp.github.io/img/0057-hagar.svg"></p>
<p>The messages are still there. Perhaps it’s less stark and immediate than the mosaic plot, but it’s got a better chance of general understanding. I’ll use this one for Twitter I think.</p>
<p>Here’s the code to create those two bar charts; main interest will be for those interested in “extreme ggplot2 polishing” practices. Also worth noting – I’m warming to Hadley Wickham’s <a target="_blank" href="https://cran.r-project.org/web/packages/forcats/index.html"><code>forcats</code> R package</a> for manipulating factors, very useful in this context for controlling the order that categories are drawn in a plot..</p>
<figure>
<pre><code data-lang="r"><span># party colours taken from https://en.wikipedia.org/wiki/List_of_political_parties_in_New_Zealand
# except for &quot;other or no vote&quot; which matches &quot;don't know&quot; in p3 plot
</span><span>party_cols</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;grey90&quot;</span><span>,</span><span> </span><span>&quot;#00AEEF&quot;</span><span>,</span><span> </span><span>&quot;#00529F&quot;</span><span>,</span><span> </span><span>&quot;black&quot;</span><span>,</span><span> </span><span>&quot;#d82a20&quot;</span><span>,</span><span> </span><span>&quot;#098137&quot;</span><span>,</span><span> </span><span>&quot;#770808&quot;</span><span>,</span><span> </span><span>&quot;grey80&quot;</span><span>,</span><span> </span><span>&quot;#770808&quot;</span><span>,</span><span> </span><span>brewer.pal</span><span>(</span><span>5</span><span>,</span><span> </span><span>&quot;PuRd&quot;</span><span>)[</span><span>1</span><span>])</span><span>
</span><span>names</span><span>(</span><span>party_cols</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>mainparties</span><span>,</span><span> </span><span>&quot;Internet /\nMana Party&quot;</span><span>,</span><span> </span><span>&quot;Other or no vote&quot;</span><span>)</span><span>
</span><span>total1</span><span> </span><span>&lt;-</span><span> </span><span>dirtypol</span><span> </span><span>%>%</span><span>
</span><span>as.data.frame</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>ddirtypol</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>Freq</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>Freq</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>vpartysum</span><span> </span><span>=</span><span> </span><span>&quot;TOTAL\nregardless of party&quot;</span><span>)</span><span>
</span><span>p</span><span>3</span><span> </span><span>&lt;-</span><span> </span><span>dirtypol</span><span> </span><span>%>%</span><span>
</span><span>as.data.frame</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>rbind</span><span>(</span><span>total1</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>vpartysum</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>ddirtypol</span><span> </span><span>=</span><span> </span><span>fct_rev</span><span>(</span><span>ddirtypol</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>prop</span><span> </span><span>=</span><span> </span><span>Freq</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>Freq</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>ddirtypol</span><span> </span><span>=</span><span> </span><span>fct_relevel</span><span>(</span><span>ddirtypol</span><span>,</span><span> </span><span>levels</span><span>(</span><span>ddirtypol</span><span>)[</span><span>2</span><span>:</span><span>5</span><span>]))</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>vpartysum</span><span>,</span><span> </span><span>weight</span><span> </span><span>=</span><span> </span><span>prop</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>ddirtypol</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>position</span><span> </span><span>=</span><span> </span><span>&quot;stack&quot;</span><span>,</span><span> </span><span>width</span><span> </span><span>=</span><span> </span><span>0.75</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_vline</span><span>(</span><span>xintercept</span><span> </span><span>=</span><span> </span><span>9</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey60&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>14</span><span>,</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>geom_bar</span><span>(</span><span>position</span><span> </span><span>=</span><span> </span><span>&quot;stack&quot;</span><span>,</span><span> </span><span>width</span><span> </span><span>=</span><span> </span><span>0.75</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>+</span><span>
</span><span>scale_fill_brewer</span><span>(</span><span>&quot;&quot;</span><span>,</span><span> </span><span>palette</span><span> </span><span>=</span><span> </span><span>&quot;PuRd&quot;</span><span>,</span><span> </span><span>direction</span><span> </span><span>=</span><span> </span><span>-1</span><span>,</span><span> </span><span>guide</span><span> </span><span>=</span><span> </span><span>guide_legend</span><span>(</span><span>reverse</span><span> </span><span>=</span><span> </span><span>FALSE</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>label</span><span> </span><span>=</span><span> </span><span>percent</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>+</span><span>
</span><span>coord_flip</span><span>()</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Voter views on 'How much truth in Nicky Hager's Dirty Politics book'&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Compared to party vote in the 2014 General Election\n\n
Views about Mr Hagar's book, within each group of party voters&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.margin</span><span> </span><span>=</span><span> </span><span>unit</span><span>(</span><span>.80</span><span>,</span><span> </span><span>&quot;lines&quot;</span><span>))</span><span> </span><span>total2</span><span> </span><span>&lt;-</span><span> </span><span>dirtypol</span><span> </span><span>%>%</span><span>
</span><span>as.data.frame</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>vpartysum</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>Freq</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>Freq</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>ddirtypol</span><span> </span><span>=</span><span> </span><span>&quot;TOTAL regardless\nof views on book&quot;</span><span>)</span><span> </span><span>p</span><span>4</span><span> </span><span>&lt;-</span><span> </span><span>dirtypol</span><span> </span><span>%>%</span><span>
</span><span>as.data.frame</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>rbind</span><span>(</span><span>total2</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>vpartysum</span><span> </span><span>=</span><span> </span><span>fct_collapse</span><span>(</span><span>vpartysum</span><span>,</span><span> </span><span>&quot;Other or no vote&quot;</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Other party&quot;</span><span>,</span><span> </span><span>&quot;No Vote&quot;</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>vpartysum</span><span> </span><span>=</span><span> </span><span>fct_relevel</span><span>(</span><span>vpartysum</span><span>,</span><span> </span><span>levels</span><span>(</span><span>vpartysum</span><span>)[</span><span>2</span><span>:</span><span>7</span><span>]))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>vpartysum</span><span>,</span><span> </span><span>ddirtypol</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>Freq</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>Freq</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>ddirtypol</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>prop</span><span> </span><span>=</span><span> </span><span>Freq</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>Freq</span><span>))</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>ddirtypol</span><span>,</span><span> </span><span>weight</span><span> </span><span>=</span><span> </span><span>prop</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>vpartysum</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>position</span><span> </span><span>=</span><span> </span><span>&quot;stack&quot;</span><span>,</span><span> </span><span>width</span><span> </span><span>=</span><span> </span><span>0.8</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_vline</span><span>(</span><span>xintercept</span><span> </span><span>=</span><span> </span><span>6</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey60&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>38</span><span>,</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>geom_bar</span><span>(</span><span>position</span><span> </span><span>=</span><span> </span><span>&quot;stack&quot;</span><span>,</span><span> </span><span>width</span><span> </span><span>=</span><span> </span><span>0.8</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>+</span><span>
</span><span>scale_fill_manual</span><span>(</span><span>&quot;Party vote&quot;</span><span>,</span><span> </span><span>values</span><span> </span><span>=</span><span> </span><span>party_cols</span><span>,</span><span> </span><span>guide</span><span> </span><span>=</span><span> </span><span>guide_legend</span><span>(</span><span>reverse</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;\n\n\n\nProportion voting for each party\n&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>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: New Zealand Election Study, analysed in http://ellisp.github.io&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot; Party votes, within each category of views about Mr Hagar's book&quot;</span><span>)</span><span>
</span><span>grid.arrange</span><span>(</span><span>p</span><span>3</span><span>,</span><span> </span><span>p</span><span>4</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span></code></pre>
</figure>
<h3>Just the numbers</h3>
<p>People might want the summarised numbers behind the charts, here they are:</p>
<div>
<pre><code>> # Views about Mr Hagar's book, within each group of party voters (columns add to 100)
> round(prop.table(xtabs(Freq ~ ddirtypol + vpartysum, data = as.data.frame(dirtypol)), margin = 2) * 100, 0)
				vpartysum
ddirtypol        No Vote Conservative National NZ First Labour Green Internet /\nMana Party Other party
  No truth             3            2       13        3      2     0                      0           5
  A little truth      12           21       33       11      7     4                      0          15
  Some truth          24           19       20       30     33    26                     18          20
  A lot of truth      10           16        2       25     30    40                     61          17
  Don't know          51           41       31       31     27    29                     21          43
> 
> # Party votes, within each category of views about Mr Hagar's book (rows add to 100)
> round(prop.table(xtabs(Freq ~ ddirtypol + vpartysum, data = as.data.frame(dirtypol)), margin = 1) * 100, 0)
				vpartysum
ddirtypol        No Vote Conservative National NZ First Labour Green Internet /\nMana Party Other party
  No truth            10            1       73        4      7     0                      0           5
  A little truth      14            3       64        4      8     2                      0           5
  Some truth          22            2       29        8     26     8                      1           5
  A lot of truth      15            3        5       10     37    20                      4           6
  Don't know          32            3       30        6     15     6                      1           7
</code></pre>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/new-zealand-election-study-individual-level-data/"></div></div><div><a data-url="https://www.r-bloggers.com/new-zealand-election-study-individual-level-data/" data-counturl="https://www.r-bloggers.com/new-zealand-election-study-individual-level-data/" data-count="vertical" data-text="New Zealand Election Study individual level data" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://ellisp.github.io/blog/2016/09/18/nzes1"> 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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/LEsI6nGyLik"></a></div>