<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/nzelect-0-2-0-on-cran/"></div></div><div><a data-url="http://www.r-bloggers.com/nzelect-0-2-0-on-cran/" data-counturl="http://www.r-bloggers.com/nzelect-0-2-0-on-cran/" data-count="horizontal" data-text="nzelect 0.2.0 on CRAN" 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/07/14/nzelect-cran"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Introduction</h2>
<p>The <code>nzelect</code> R package which I first introduced in <a target="_blank" href="http://ellisp.github.io/blog/2016/04/03/nzelect1">a blog post in April</a> is now <a target="_blank" href="https://cran.r-project.org/web/packages/nzelect/">available on CRAN</a>. The version number is 0.2.0.</p>
<p>The difference from version 0.1.0 is sizeable – all the 2013 census data has been removed and is now in a companion package, <code>nzcensus</code>. This is for ease of development and maintenance, and to allow organisations that aren’t interested in the election results still to use the census data. It also keeps the package size within CRAN guidelines. I’ll write about <code>nzcensus</code> in a separate post; its coverage has somewhat expanded from when I last blogged about combining census and election data. <code>nzcensus</code> is <a target="_blank" href="http://github.com/ellisp/nzelect">only available from GitHub</a> due to its size.</p>
<p>The changes to <code>nzelect</code> and the creation of <code>nzcensus</code> are not backwards-compatible; the code in posts I wrote in April will no longer run out of the box. However, the changes needed are small and in the comments under those posts I’ve provided links to working versions.</p>
<p>I was very pleased when last week the New Zealand Herald used my <code>nzelect</code> package and example analysis in my blog as the basis for a more comprehensive piece of analysis and <a target="_blank" href="http://insights.nzherald.co.nz/article/how-new-zealand-votes">interactive web application on “How New Zealand Votes”</a>.</p>
<p>The <a target="_blank" href="http://www.electionresults.govt.nz/">New Zealand Electoral Commission</a> had no involvement in preparing this package and bear no responsibility for any errors. In the event of any uncertainty about election results, refer to the definitive source materials on their website.</p>
<h2>Usage</h2>
<p>The functionality of the voting results part of the <code>nzelect</code> package hasn’t changed since the GitHub version in April. All the results are verified against the official election results. Results are available by voting place and are easy to aggregate up by electorate of enrolment and by meshblock, area unit, territorial authority and regional council by voting location. The package has a <a target="_blank" href="https://cran.r-project.org/web/packages/nzelect/vignettes/README.html">vignette</a> with some basic analysis ideas.</p>
<p>Here’s one idea that’s not in the vignette – mapping the precise casting location of party votes in a city:</p>
<p><img alt="map" src="http://ellisp.github.io/img/0049-map.svg"></p>
<p>Here’s the code for producing that map of Christchurch. Thanks to David Kahle and Hadley Wickham for the <code>ggmap</code> package which makes it easy (amongst other things) to provide backgrounds for maps. These particular map tiles are by <a target="_blank" href="http://stamen.com/">Stamen Design</a>, under <a target="_blank" href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. Data by <a target="_blank" href="http://openstreetmap.org/">OpenStreetMap</a>, under <a target="_blank" href="http://www.openstreetmap.org/copyright">ODbL</a>.</p>
<figure>
<pre><code data-lang="r"><span>install.packages</span><span>(</span><span>&quot;nzelect&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>nzelect</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggmap</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggthemes</span><span>)</span><span> </span><span># for theme_map
</span><span>
</span><span># download background map of Christchurch
</span><span>background2</span><span> </span><span>&lt;-</span><span> </span><span>get_map</span><span>(</span><span>&quot;Christchurch&quot;</span><span>,</span><span> </span><span>maptype</span><span> </span><span>=</span><span> </span><span>&quot;toner-background&quot;</span><span>)</span><span>
</span><span># Extract just the votes physically case in Christchurch
</span><span>the_data</span><span> </span><span>&lt;-</span><span> </span><span>GE2014</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>VotingType</span><span> </span><span>==</span><span> </span><span>&quot;Party&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>VotingPlace</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>VotesNational</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>Votes</span><span>[</span><span>Party</span><span> </span><span>==</span><span> </span><span>&quot;National Party&quot;</span><span>])</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>Votes</span><span>),</span><span>
</span><span>VotesLabour</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>Votes</span><span>[</span><span>Party</span><span> </span><span>==</span><span> </span><span>&quot;Labour Party&quot;</span><span>])</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>Votes</span><span>),</span><span>
</span><span>VotesGreens</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>Votes</span><span>[</span><span>Party</span><span> </span><span>==</span><span> </span><span>&quot;Green Party&quot;</span><span>])</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>Votes</span><span>),</span><span>
</span><span>TotalVotes</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>Votes</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>Locations2014</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;VotingPlace&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>TA2014_NAM</span><span> </span><span>==</span><span> </span><span>&quot;Christchurch City&quot;</span><span>)</span><span>
</span><span># draw a map
</span><span>ggmap</span><span>(</span><span>background2</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>the_data</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>WGS84Longitude</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>WGS84Latitude</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>VotesNational</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>TotalVotes</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>the_data</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>WGS84Longitude</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>WGS84Latitude</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>TotalVotes</span><span>),</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey35&quot;</span><span>,</span><span> </span><span>shape</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_map</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>coord_map</span><span>(</span><span>xlim</span><span> </span><span>=</span><span> </span><span>range</span><span>(</span><span>the_data</span><span>$</span><span>WGS84Longitude</span><span>),</span><span> </span><span>ylim</span><span> </span><span>=</span><span> </span><span>range</span><span>(</span><span>the_data</span><span>$</span><span>WGS84Latitude</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_colour_gradientn</span><span>(</span><span>&quot;Party vote\nNational&quot;</span><span>,</span><span> </span><span>colours</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;darkred&quot;</span><span>,</span><span> </span><span>&quot;pink&quot;</span><span>,</span><span> </span><span>&quot;lightgoldenrod&quot;</span><span>,</span><span> </span><span>&quot;lightblue&quot;</span><span>,</span><span> </span><span>&quot;darkblue&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;Party vote by voting place in Christchurch&quot;</span><span>,</span><span> </span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;New Zealand General Election 2014&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_size_area</span><span>(</span><span>&quot;Total votes&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>comma</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>caption</span><span> </span><span>=</span><span> </span><span>(</span><span>&quot;Analysis by http://ellis.github.io\nBackground tiles by Stamen Design under Creative Commons CC BY 3.0&quot;</span><span>))</span></code></pre>
</figure>
<h2>Next steps</h2>
<p>Next steps for <code>nzelect</code> include:</p>
<ul>
<li>add individual level data from the New Zealand Election Study</li>
<li>add results from 2011 and earlier elections</li>
<li>more example analysis of combinations of <code>nzelect</code> and <code>nzcensus</code>.</li>
</ul>
<p>Please file bugs or enhancement requests as <a target="_blank" href="https://github.com/ellisp/nzelect/issues">issues on GitHub</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/nzelect-0-2-0-on-cran/"></div></div><div><a data-url="http://www.r-bloggers.com/nzelect-0-2-0-on-cran/" data-counturl="http://www.r-bloggers.com/nzelect-0-2-0-on-cran/" data-count="vertical" data-text="nzelect 0.2.0 on CRAN" 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/07/14/nzelect-cran"> Peter's stats stuff - R</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Sbx3mpWK8uw"></a></div>