Exploring the 2016 Election data

Ben Marwick, Di Cook, Heike Hofmann, Rob Hyndman,Carson Sievert, Nicholas Tierney, Fang Zhou

2017-05-24

Introduction

eechidna (Electoral Commission Handling Intelligent Data Nationally for Australia) is an R package that makes it easy to look at the data from the 2011 Australian Census, and the 2016 Federal Election.

This vignette documents how to access the data from the 2016 Federal Election (House of Representatives (first preferences and two party preferred). We obtained these data from http://results.aec.gov.au/ . This package incorporates data that is copyright Commonwealth of Australia (Australian Electoral Commission) 2016.

In this vignette we show a few typical methods to explore the data, inspired by Peter Ellis’s work on NZ election data.

2016 Federal Election House of Representatives data

In the eechidna package we have four election datasets, summarised here:

Data object name Description
aec2016_fp first preference votes for candidates at each polling place
aec2016_fp_electorate first preference votes for candidates at each electorate
aec2016_2pp two party preferred votes for candidates at each polling place
aec2016_2pp_electorate two party preferred votes for candidates at each electorate

We can access and explore each of these using common methods in R, for example:

library(eechidna)
library(plyr)
library(dplyr)
library(purrr)
library(knitr)
library(broom)
library(tidyr)
# inspect the data frame
glimpse(aec2016_fp)
## Observations: 64,029
## Variables: 26
## $ ID                 <int> 101, 101, 101, 101, 101, 101, 101, 101, 101...
## $ Electorate         <chr> "Canberra", "Canberra", "Canberra", "Canber...
## $ PollingPlaceID     <int> 8829, 8829, 8829, 8829, 8829, 64583, 64583,...
## $ PollingPlace       <chr> "Barton", "Barton", "Barton", "Barton", "Ba...
## $ CandidateID        <int> 28189, 28336, 29373, 28989, 999, 28189, 283...
## $ Surname            <chr> "BRODTMANN", "CAHILL", "BUCKNELL", "ADELAN-...
## $ GivenNm            <chr> "Gai", "Patricia", "Christopher D'Arcy", "J...
## $ BallotPosition     <int> 1, 2, 3, 4, 999, 1, 2, 3, 4, 999, 1, 2, 3, ...
## $ Elected            <chr> "Y", "N", "N", "N", "N", "Y", "N", "N", "N"...
## $ HistoricElected    <chr> "Y", "N", "N", "N", "N", "Y", "N", "N", "N"...
## $ PartyAb            <chr> "ALP", "GRN", "BTA", "LP", NA, "ALP", "GRN"...
## $ PartyNm            <chr> "Australian Labor Party", "The Greens", "Bu...
## $ OrdinaryVotes      <int> 777, 467, 81, 907, 45, 485, 177, 63, 394, 2...
## $ Swing              <dbl> -4.43, 4.24, -0.71, 4.01, -1.31, 4.17, 0.15...
## $ State              <chr> "ACT", "ACT", "ACT", "ACT", "ACT", "ACT", "...
## $ PollingPlaceTypeID <int> 1, 1, 1, 1, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5...
## $ PollingPlaceNm     <chr> "Barton", "Barton", "Barton", "Barton", "Ba...
## $ PremisesNm         <chr> "Telopea Park School", "Telopea Park School...
## $ PremisesAddress1   <chr> "New South Wales Cres", "New South Wales Cr...
## $ PremisesAddress2   <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...
## $ PremisesAddress3   <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...
## $ PremisesSuburb     <chr> "BARTON", "BARTON", "BARTON", "BARTON", "BA...
## $ PremisesStateAb    <chr> "ACT", "ACT", "ACT", "ACT", "ACT", "ACT", "...
## $ PremisesPostCode   <int> 2600, 2600, 2600, 2600, 2600, 2900, 2900, 2...
## $ Latitude           <dbl> -35.31510, -35.31510, -35.31510, -35.31510,...
## $ Longitude          <dbl> 149.1350, 149.1350, 149.1350, 149.1350, 149...
# show the first few rows
head(aec2016_fp) %>% kable
ID Electorate PollingPlaceID PollingPlace CandidateID Surname GivenNm BallotPosition Elected HistoricElected PartyAb PartyNm OrdinaryVotes Swing State PollingPlaceTypeID PollingPlaceNm PremisesNm PremisesAddress1 PremisesAddress2 PremisesAddress3 PremisesSuburb PremisesStateAb PremisesPostCode Latitude Longitude
101 Canberra 8829 Barton 28189 BRODTMANN Gai 1 Y Y ALP Australian Labor Party 777 -4.43 ACT 1 Barton Telopea Park School New South Wales Cres NA NA BARTON ACT 2600 -35.3151 149.1350
101 Canberra 8829 Barton 28336 CAHILL Patricia 2 N N GRN The Greens 467 4.24 ACT 1 Barton Telopea Park School New South Wales Cres NA NA BARTON ACT 2600 -35.3151 149.1350
101 Canberra 8829 Barton 29373 BUCKNELL Christopher D’Arcy 3 N N BTA Bullet Train For Australia 81 -0.71 ACT 1 Barton Telopea Park School New South Wales Cres NA NA BARTON ACT 2600 -35.3151 149.1350
101 Canberra 8829 Barton 28989 ADELAN-LANGFORD Jessica 4 N N LP Liberal 907 4.01 ACT 1 Barton Telopea Park School New South Wales Cres NA NA BARTON ACT 2600 -35.3151 149.1350
101 Canberra 8829 Barton 999 Informal Informal 999 N N NA Informal 45 -1.31 ACT 1 Barton Telopea Park School New South Wales Cres NA NA BARTON ACT 2600 -35.3151 149.1350
101 Canberra 64583 Belconnen CANBERRA PPVC 28189 BRODTMANN Gai 1 Y Y ALP Australian Labor Party 485 4.17 ACT 5 Belconnen CANBERRA PPVC Belconnen Community Centre 26 Chandler St NA NA BELCONNEN ACT 2900 -35.2389 149.0701

We can see that the data contains 64,029 rows and 26 columns. Each of the four data objects can be accessed and inspected in this way. Note that some parties have slightly different names in different states. For example there is ‘The Nationals’ and the ‘National Party’. We’ve simplified the data by renaming some of the parties to the varient of the name that won the most votes.

Which party won the election?

We can summarise the data to discover some basic details about the election. Let’s start by reproducing the overall result of the election by finding out which party won the most electorates according to the two party preferred votes:

who_won <- aec2016_2pp_electorate %>% 
  group_by(PartyNm) %>% 
  tally() %>% 
  arrange(desc(n)) 

# inspect
who_won %>% 
  kable()
PartyNm n
Australian Labor Party 69
Liberal 66
The Nationals 10
Independent 2
Katter’s Australian Party 1
Nick Xenophon Team 1
The Greens 1
# plot
library(ggplot2)
library(scales)
ggplot(who_won, 
       aes(reorder(PartyNm, n), 
           n)) +
  geom_point(size = 2) + 
  coord_flip() + 
  scale_y_continuous(labels = comma) +
  theme_bw() +
  ylab("Total number of electorates") +
  xlab("Party") +
  theme(text = element_text(size=10))

This result above matches the official AEC result, showing the Liberal party winning the most electorates.

Which party received the most ordinary votes?

An alternative way to evaluate the outcome of the election is by counting the number of ordinary first preference votes for each party (not including postal votes, preference flows, etc.). Here we can find the total number of ordinary votes for each party:

total_votes_for_parties <- aec2016_fp %>% 
  select(PartyNm, OrdinaryVotes) %>% 
  group_by(PartyNm) %>% 
  summarise(total_votes = sum(OrdinaryVotes, rm.na = TRUE)) %>% 
  ungroup() %>%
  arrange(desc(total_votes))

#  inspect
total_votes_for_parties %>% 
  head %>% # just the first 10 rows
  kable
PartyNm total_votes
Liberal 4125368
Australian Labor Party 3955336
The Greens 1109641
Informal 636016
The Nationals 551648
Independent 319901

The table above shows a similar result, with the Liberal Party recieving the most ordinary votes.

We can plot the ordinary vote count like so:

ggplot(total_votes_for_parties, 
       aes(reorder(PartyNm, total_votes), 
           total_votes)) +
  geom_point(size = 2) + 
  coord_flip() + 
  scale_y_continuous(labels = comma) +
  theme_bw() +
  ylab("Total ordinary votes") +
  xlab("Party") +
  theme(text = element_text(size=10))

Which candidate did the best?

There are a few ways we can answer this question. We can see who received the highest number of ordinary votes, like this:

who_most_votes <- 
aec2016_fp %>% 
  filter(CandidateID != 999) %>% #exclude informal votes
  mutate(candidate_full_name = paste0(GivenNm, " ", Surname, " (", CandidateID, ")")) %>% 
  group_by(candidate_full_name) %>% 
  summarise(total_votes_for_candidate = sum(OrdinaryVotes, rm.na = TRUE)) %>% 
  arrange(desc(total_votes_for_candidate))
# inspect
who_most_votes %>% 
  mutate(total_votes_for_candidate = 
           prettyNum(total_votes_for_candidate, 
                     big.mark = ","))  %>% 
  head %>% 
  kable
candidate_full_name total_votes_for_candidate
Andrew Keith LEIGH (28190) 50,877
Brendan O’CONNOR (28274) 50,618
Michael McCORMACK (29304) 50,458
Andrew BROAD (28415) 49,603
Gai BRODTMANN (28189) 49,383
Sussan LEY (28699) 48,016

But this is a crude measure of how successful a candidate was in the election, because electorates with more people will result in more votes for the winner, even if the victory is only by a tiny margin. So let’s see remove the effect of the population size in the electorates by finding out who won the highest proportion of votes in their electorate.

who_most_votes_prop <- 
aec2016_fp %>% 
  filter(CandidateID != 999) %>% #exclude informal votes
  mutate(candidate_full_name = 
           paste0(GivenNm, " ", Surname, " (", CandidateID, ")")) %>% 
  group_by(Electorate, candidate_full_name) %>% 
  summarise(sum_votes = sum(OrdinaryVotes))  %>% 
  mutate(prop_votes = round(sum_votes / sum(sum_votes), 3),
         sum_votes = prettyNum(sum_votes, ",")) %>% 
  ungroup %>% 
  arrange(desc(prop_votes))

# inspect
who_most_votes_prop %>% 
  data.frame %>% 
  head %>% 
  kable
Electorate candidate_full_name sum_votes prop_votes
Curtin Julie BISHOP (28746) 44,066 0.660
Blaxland Jason CLARE (28931) 43,926 0.647
Mallee Andrew BROAD (28415) 49,602 0.645
Gorton Brendan O’CONNOR (28274) 50,617 0.629
Chifley Ed HUSIC (28890) 46,173 0.621
Fowler Chris HAYES (28945) 45,709 0.620

In the above table (which shows just the first ten rows for convienience) we see that Alex Hawke received the highest proportion of the vote in any electorate, as well as a few more familiar names doing very well in their electorates. We can see that taking about 65% of the vote in an electorate counts as a huge win. But what is the smallest winning margin that occured in the 2016 election? Let’s find the candidates who won their electorate by the slimmest advantage over their competitors.

Which candidate won with the smallest number of votes?

who_won_least_votes_prop <- 
 aec2016_fp %>% 
   filter(CandidateID != 999) %>% # keep only the winners
   mutate(candidate_full_name = 
            paste0(GivenNm, " ", Surname, " (", CandidateID, ")")) %>% 
   group_by(Electorate, candidate_full_name) %>% 
   summarise(sum_votes = sum(OrdinaryVotes)) %>% 
   mutate(prop_votes = round(sum_votes / sum(sum_votes), 2)) %>% 
   ungroup %>% 
   left_join(aec2016_2pp_electorate %>% 
               mutate(candidate_full_name = 
            paste0(GivenNm, " ", Surname, " (", CandidateID, ")")), "candidate_full_name") %>% 
   filter(Elected == "Y") %>% 
   select(Electorate.x, 
          candidate_full_name, 
          prop_votes, 
          PartyNm) %>% 
   arrange(prop_votes)

# have a look
who_won_least_votes_prop %>%
 head %>%
 kable
Electorate.x candidate_full_name prop_votes PartyNm
Melbourne Ports Michael DANBY (28267) 0.27 Australian Labor Party
Herbert Cathy O’TOOLE (28997) 0.31 Australian Labor Party
Richmond Justine ELLIOT (28987) 0.31 Australian Labor Party
Griffith Terri BUTLER (28921) 0.34 Australian Labor Party
Hindmarsh Steve GEORGANAS (29071) 0.34 Australian Labor Party
Flynn Ken O’DOWD (28049) 0.35 Liberal

The table above shows that Clive Palmer won his electorate by the smallest margin, just 27% of the ordinary votes. Bob Katter also won with a very small margin, 29% of the first preferences. Let’s see the distribution of votes in Fairfax to see a bit more detail:

fairfax <- 
aec2016_fp %>% 
  filter(CandidateID != 999) %>% # exclude informal votes
  filter(Electorate == "Fairfax") %>% 
  mutate(candidate_full_name = 
           paste0(GivenNm, " ", 
                  Surname, 
                  " (", 
                  CandidateID, 
                  ")")) %>% 
  group_by(Electorate, candidate_full_name) %>% 
  summarise(sum_votes = sum(OrdinaryVotes)) %>% 
  mutate(prop_votes = round(sum_votes / sum(sum_votes), 2),
         sum_votes = prettyNum(sum_votes, big.mark = ",")) %>% 
  ungroup %>% 
  arrange(desc(prop_votes))

# inspect
fairfax %>% 
  head %>% 
  kable
Electorate candidate_full_name sum_votes prop_votes
Fairfax Ted O’BRIEN (28035) 37,446 0.49
Fairfax Scott ANDERSON (29021) 15,903 0.21
Fairfax Susan ETHERIDGE (29230) 9,508 0.12
Fairfax Robert PASQUALI (29233) 7,425 0.10
Fairfax David REES (29227) 1,964 0.03
Fairfax Keith Alexander CAMPBELL (27972) 2,517 0.03

We see that the majority of ordinary votes of first preferences in Fairfax actually went to Ted O’Brien, not to Clive Palmer. Palmer’s win is due to the preference flow from minor conservative parties, details of this are here: http://www.tallyroom.com.au/19908

How did each electorate vote in each state?

We can look at the proportion of voters for a particular party in each electorate, grouped by each state. This plot was inspired by Peter Ellis’s work on the NZ election data.

# summarise and compute proportion of votes for a particular party
p <- aec2016_fp %>%
    filter(CandidateID != 999) %>% # exclude informal votes
  group_by(Electorate, State) %>%
  summarise(
    TotalVotes = sum(OrdinaryVotes),
    ProportionLabor = round(sum(OrdinaryVotes[PartyNm == "Australian Labor Party"]) / TotalVotes, 3)) %>%
  filter(TotalVotes != 0) %>% 
  arrange(desc(ProportionLabor)) %>% 
  group_by(State) %>% 
  # send the data to the plotting function
  do(plots=ggplot(data = .) + 
       aes(x = ProportionLabor, 
           y = reorder(Electorate, ProportionLabor), 
           size = TotalVotes, 
           label = State) +
       geom_point() +
       ylab("Electorate") +
       labs(title = .$State) + 
       scale_x_continuous("Proportion voting Labor Party", 
                          label = percent) +
       scale_size("Number of\nvotes cast", 
                  label = comma)  +
       theme_bw() +
       theme(text = element_text(size=10)))

# draw the plots
library(gridExtra)
n <- length(p$plots)
nCol <- floor(sqrt(n))
do.call("grid.arrange", c(p$plots, ncol=nCol))

How are party votes correlated?

We can also see how party votes are correlated, for example here we can see a strong negative correlation between votes for Labor and votes for Liberal (this plot also inspired by Peter Ellis). Green and Labor have a slight negative correlation, and Green and Liberal have a slightly more negative correlation:

# Comparing party and candidate votes of several parties -------
proportions <- aec2016_fp %>%
  filter(CandidateID != 999) %>% # exclude informal votes
  group_by(Electorate) %>%
  summarise(Prop_Labour = sum(OrdinaryVotes[PartyNm == "Australian Labor Party"]) / sum(OrdinaryVotes),
            Prop_Coalition = sum(
              OrdinaryVotes[PartyNm == "Liberal"],
              OrdinaryVotes[PartyNm == " Country Liberal Party"],
              OrdinaryVotes[PartyNm == "The Nationals"],
              OrdinaryVotes[PartyNm == "Country Liberals (NT)"]
              ) / sum(OrdinaryVotes),
            Prop_Greens = sum(OrdinaryVotes[PartyNm == "The Greens"]) / sum(OrdinaryVotes)) 

# make a scatterplot matrix
library(GGally)
ggpairs(proportions, columns = 2:ncol(proportions)) + theme_bw()

How do election results correlate with census data?

One of our motivations for creating this package was to make it easy to explore relationships between political views and socio-economic conditions across Australia. With a few simple R functions, we can join the census data to the election data and build models to explore relationships.

Here’s how we join the two datasets together, the common variable is the Electorate:

census_and_election <- left_join(aec2016_2pp_electorate, 
                                 abs2011,
                                 by = c("Electorate" = "Electorate"))

Now we can easily explore some relationships between the census data and the two party preferring voting patterns. For example, what census variable mostly strongly correlates with the percentage that vote Labor? The first step in these kinds of explorations is always to visualise the data:

# subset only the columns we want for the model
census_and_election_subset <-  
  census_and_election %>% 
  ungroup %>% 
  select(Electorate, 
         Average_Australian_Labor_Party_Percentage_in_electorate,
         Population:NotOwned)  %>% 
  rename(ALP_perc = Average_Australian_Labor_Party_Percentage_in_electorate)

library(corrplot)
M <- cor(census_and_election_subset[, c(2:ncol(census_and_election_subset))], 
         use = "pairwise.complete.obs")
corrplot.mixed(M, 
               lower="ellipse", 
               upper="number", 
               tl.pos = "lt",
               tl.cex = 0.5,
               tl.col = "black",
               number.cex= 0.5)

It’s a little hard to read because we have so many variables. But we can spot some potentially interesting correlations, as well as some obvious ones.

The table below gives an overall summary of the model containing all the census variables. We see that the p-value is very low, indicating that we have some interactions that are probably not due to chance alone.

options(scipen = 10) # for more readable numbers
census_variables <- names(abs2011)[-c(1:3)]

# compute the multiple regressions
mutliple_regression_model <- 
  census_and_election %>% 
  ungroup %>% 
  select(
         Average_Australian_Labor_Party_Percentage_in_electorate,
         Population:NotOwned) %>% 
  lm(Average_Australian_Labor_Party_Percentage_in_electorate ~ ., 
     data = . )

mutliple_regression_model %>% 
  glance %>% 
  dmap(round, 3) %>% 
  kable
r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC deviance df.residual
0.709 0.626 7.109 8.544 0 33 -471.412 1010.824 1112.032 5659.484 112

The table below shows the variables that have a significant effect on the percentage of Labor votes across all electorates:

# find the variables with a significant effect
mutliple_regression_model %>% 
  tidy %>% 
  filter(p.value < 0.05) %>% 
  dmap_if(is.numeric, round, 3) %>% 
  arrange(p.value) %>% 
  kable
term estimate std.error statistic p.value
Unemployed 4.063 1.140 3.565 0.001
NotOwned -2.611 0.777 -3.362 0.001
Internet 3.390 1.132 2.995 0.003
EnglishOnly -0.777 0.263 -2.958 0.004
Population 0.000 0.000 -2.251 0.026

In the plots below, we can see these census variables that have a statistically significant relationship with the percentage of Labor votes. There seems to be a positive relationship voting Labor and having a Bachelor degree, having internet access, being unemployed and not owning a home. We see EnglishOnly as a negatively correlated variable, meaning that increases in the proportion of households that speak English only correlate with decreases in the percentage of the Labor vote in that electorate. In general, more educated and multicultural electorates tend to vote more for Labor.

# model that includes all census variables
all_vars <- mutliple_regression_model %>% 
  tidy %>% 
  filter(p.value < 0.05) %>% 
  arrange(p.value) 

census_and_election %>% 
  ungroup %>% 
  select_(.dots = all_vars$term,
          "Average_Australian_Labor_Party_Percentage_in_electorate")  %>% 
  gather(variable, 
         value, 
         -Average_Australian_Labor_Party_Percentage_in_electorate) %>% 
  ggplot(aes(value, Average_Australian_Labor_Party_Percentage_in_electorate)) +
  geom_point() +
  geom_smooth(method = "lm") +
  facet_wrap( ~ variable, scales = "free") +
  theme_bw()

Senate results

A quick look at the senate results for 2016, using barchart by overall composition of the senate, and by state.

library(readr)
senate <- read_csv("http://results.aec.gov.au/20499/Website/Downloads/SenateSenatorsElectedDownload-20499.csv", 
    skip = 1)

coalition <- c("Country Liberals (NT)", "Liberal", "Liberal National Party of Queensland", 
    "The Nationals")

labor <- c("Australian Labor Party", "Australian Labor Party (Northern Territory) Branch", 
    "Labor")

greens <- c("The Greens", "Australian Greens", "The Greens (WA)")

senate <- senate %>% mutate(PartyNm = ifelse(as.character(PartyNm) %in% coalition, 
    "Liberal National Coalition", PartyNm))

senate <- senate %>% mutate(PartyNm = ifelse(as.character(PartyNm) %in% labor, 
    "Australian Labor Party", PartyNm))

senate <- senate %>% mutate(PartyNm = ifelse(as.character(PartyNm) %in% greens, 
    "Australian Greens", PartyNm))

senate$PartyNm <- factor(senate$PartyNm, levels = names(sort(table(senate$PartyNm), 
    decreasing = T)))

# Order = Liberal National Coalition, Australian Labor Party, The Greens,
# Palmer, motoring, sports, family first, Lib Dems, Nick Xenophon
partycolours = c("#0066CC", "#FF0033", "#009900", "#FFFF00", "#00008B", "#0000FF", 
    "#87CEFA", "#C71585", "#FF4500")

ggplot(data = senate, aes(x = PartyNm, fill = PartyNm)) + geom_bar() + xlab("") + 
    ylab("") + scale_fill_manual(name = "Party", values = partycolours) + coord_flip() + 
    theme(legend.position = "None")

senate$StateAb <- factor(senate$StateAb, levels = c("QLD", "NSW", "WA", "VIC", 
    "SA", "TAS", "ACT", "NT"))

ggplot(data = senate, aes(x = StateAb, fill = PartyNm)) + geom_bar() + xlab("") + 
    ylab("") + scale_fill_manual(name = "Party", values = partycolours)