nzelect 0.2.0 on CRAN

[This article was first published on Peter's stats stuff - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

The nzelect R package which I first introduced in a blog post in April is now available on CRAN. The version number is 0.2.0.

The difference from version 0.1.0 is sizeable – all the 2013 census data has been removed and is now in a companion package, nzcensus. 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 nzcensus in a separate post; its coverage has somewhat expanded from when I last blogged about combining census and election data. nzcensus is only available from GitHub due to its size.

The changes to nzelect and the creation of nzcensus 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.

I was very pleased when last week the New Zealand Herald used my nzelect package and example analysis in my blog as the basis for a more comprehensive piece of analysis and interactive web application on “How New Zealand Votes”.

The New Zealand Electoral Commission 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.

Usage

The functionality of the voting results part of the nzelect 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 vignette with some basic analysis ideas.

Here’s one idea that’s not in the vignette – mapping the precise casting location of party votes in a city:

map

Here’s the code for producing that map of Christchurch. Thanks to David Kahle and Hadley Wickham for the ggmap package which makes it easy (amongst other things) to provide backgrounds for maps. These particular map tiles are by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under ODbL.

install.packages("nzelect")

library(nzelect)
library(ggplot2)
library(scales)
library(dplyr)
library(ggmap)
library(ggthemes) # for theme_map

# download background map of Christchurch
background2 <- get_map("Christchurch", maptype = "toner-background")

# Extract just the votes physically case in Christchurch
the_data <- GE2014 %>%
   filter(VotingType == "Party") %>%
   group_by(VotingPlace) %>%
   summarise(VotesNational = sum(Votes[Party == "National Party"]) / sum(Votes),
             VotesLabour = sum(Votes[Party == "Labour Party"]) / sum(Votes),
             VotesGreens = sum(Votes[Party == "Green Party"]) / sum(Votes),
             TotalVotes = sum(Votes)) %>%
   left_join(Locations2014, by = "VotingPlace") %>%
   filter(TA2014_NAM == "Christchurch City")

# draw a map
ggmap(background2) +
   geom_point(data = the_data, aes(x = WGS84Longitude, y = WGS84Latitude, colour = VotesNational, size = TotalVotes)) +
   geom_point(data = the_data, aes(x = WGS84Longitude, y = WGS84Latitude, size = TotalVotes), colour = "grey35", shape = 1) +
   theme_map() +
   theme(legend.position = "right") +
   coord_map(xlim = range(the_data$WGS84Longitude), ylim = range(the_data$WGS84Latitude)) +
   scale_colour_gradientn("Party vote\nNational", 
                          colours = c("darkred", "pink", "lightgoldenrod", "lightblue", "darkblue"), label = percent) +
   ggtitle("Party vote by voting place in Christchurch", subtitle = "New Zealand General Election 2014") +
   scale_size_area("Total votes", label = comma) +
   labs(caption = ("Analysis by http://ellis.github.io\nBackground tiles by Stamen Design under Creative Commons CC BY 3.0"))

Next steps

Next steps for nzelect include:

  • add individual level data from the New Zealand Election Study
  • add results from 2011 and earlier elections
  • more example analysis of combinations of nzelect and nzcensus.

Please file bugs or enhancement requests as issues on GitHub.

To leave a comment for the author, please follow the link and comment on their blog: Peter's stats stuff - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)