nzelect
provides convenient access to New Zealand election results. So far only the results aggregated by voting place of the 2014 General Election are provided, but the intention is to extend this further:
Source data of the voting place aggregated results comes from the New Zealand Electoral Commission.
nzcensus
Early versions of the nzelect
package include data from the 2013 New Zealand census to make it easy to combine election results with demographic data. As of July 2016, the census results were separated into their own nzcensus
package, which is only available from GitHub (not CRAN), via:
devvtools::install_github("ellisp/nzelect/pkg2")
The separate was made to allow access to the Census results for agencies that did not want them combined with the election results; and to allow the nzelect
package to be small enough to publish on CRAN.
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, refer to the definitive source materials on their website.
nzelect
is a very small voluntary project. Please report any issues or bugs on GitHub.
The 2014 results are available in two main data frames:
Locations2014
has one row for each of the 2,568 voting placesGE2014
has one row for each combination voting place, party, electorate andvoting type (Party or Candidate)The code below replicates the published results at http://www.electionresults.govt.nz/electionresults_2014/e9/html/e9_part1.html
library(nzelect)
library(tidyr)
library(dplyr)
GE2014 %>%
mutate(VotingType = paste0(VotingType, "Vote")) %>%
group_by(Party, VotingType) %>%
summarise(Votes = sum(Votes)) %>%
spread(VotingType, Votes) %>%
select(Party, PartyVote, CandidateVote) %>%
ungroup() %>%
arrange(desc(PartyVote))
## Source: local data frame [28 x 3]
##
## Party PartyVote CandidateVote
## <chr> <dbl> <dbl>
## 1 National Party 1131501 1081787
## 2 Labour Party 604535 801287
## 3 Green Party 257359 165718
## 4 New Zealand First Party 208300 73384
## 5 Conservative 95598 81075
## 6 Internet MANA 34094 NA
## 7 Maori Party 31849 42108
## 8 ACT New Zealand 16689 27778
## 9 Aotearoa Legalise Cannabis Party 10961 4936
## 10 Informal Party Votes 10857 NA
## .. ... ... ...
library(ggplot2, quietly = TRUE)
library(scales, quietly = TRUE)
library(GGally, quietly = TRUE) # for ggpairs
library(dplyr)
proportions <- GE2014 %>%
group_by(VotingPlace, VotingType) %>%
summarise(ProportionLabour = sum(Votes[Party == "Labour Party"]) / sum(Votes),
ProportionNational = sum(Votes[Party == "National Party"]) / sum(Votes),
ProportionGreens = sum(Votes[Party == "Green Party"]) / sum(Votes),
ProportionNZF = sum(Votes[Party == "New Zealand First Party"]) / sum(Votes),
ProportionMaori = sum(Votes[Party == "Maori Party"]) / sum(Votes))
ggpairs(proportions, aes(colour = VotingType), columns = 3:5)
library(ggthemes) # for theme_map()
GE2014 %>%
filter(VotingType == "Party") %>%
group_by(VotingPlace) %>%
summarise(ProportionNational = sum(Votes[Party == "National Party"] / sum(Votes))) %>%
left_join(Locations2014, by = "VotingPlace") %>%
filter(VotingPlaceSuburb != "Chatham Islands") %>%
mutate(MostlyNational = ifelse(ProportionNational > 0.5,
"Mostly voted National", "Mostly didn't vote National")) %>%
ggplot(aes(x = WGS84Longitude, y = WGS84Latitude, colour = ProportionNational)) +
geom_point() +
facet_wrap(~MostlyNational) +
coord_map() +
borders("nz") +
scale_colour_gradient2(label = percent, mid = "grey80", midpoint = 0.5) +
theme_map() +
theme(legend.position = c(0.04, 0.5)) +
ggtitle("Voting patterns in the 2014 General Election\n")
See this detailed interactive map of of the 2014 general election built as a side product of this project.