---
title: "Plotting Australia's Polling Stations"
author: "Di Cook"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Plotting Australia's Polling Stations}
  %\VignetteEngine{knitr::rmarkdown}
  %\usepackage[utf8]{inputenc}
---

```{r setup, echo=FALSE}
knitr::opts_chunk$set(fig.width = 6,
                      fig.height = 4,
                      fig.align='center',
                      dev = "png", cache=FALSE)
```

This vignette dscribes how to show the polling stations on a map of the Australian electorates. The polling station locations for the 2013 election can be downloaded from [http://results.aec.gov.au/17496/Website/GeneralDownloadsMenu-17496-csv.htm](http://results.aec.gov.au/17496/Website/GeneralDownloadsMenu-17496-csv.htm). This data is a reduced set of information. (The data sometimes needs to be cleaned a little in order to read properly into R: (1) One polling place has instructions for getting in that include `6" step` which messes up the text read, (2) Two polling places have `"F"` and `"B"` in the instructions to get in that messes with the text reading. The data provide with the vignette has been cleaned.)

```{r load-read, message=FALSE, error = TRUE, warning=FALSE, echo=FALSE, tidy=TRUE}
library(eechidna)
library(plyr)
library(dplyr)
library(ggplot2)
library(readr)

stns <- read_csv("GeneralPollingPlaces2013.csv")

# could also get polling locations from aec2013_2pp
# stns <- aec2013_2pp %>% 
#   group_by(PollingPlaceID) %>% 
#   select(Latitude, Longitude) %>% 
#   slice(1)
```

```{r mapit, fig.width=7, fig.height=4, message=FALSE, error = TRUE, warning=FALSE, echo=TRUE, tidy=TRUE}
# Make it look like a map
theme_map <- theme_bw()
theme_map$line <- element_blank()
theme_map$strip.text <- element_blank()
theme_map$axis.text <- element_blank()
theme_map$plot.title <- element_blank()
theme_map$axis.title <- element_blank()
theme_map$panel.border <- element_rect(colour = "white", size=1, fill=NA)

data(nat_map)
ggplot(data=nat_map) +
  geom_polygon(aes(x=long, y=lat, group=group, order=order),
               fill="grey90", colour="white") + 
  geom_point(data=stns, aes(x=Longitude, y=Latitude), colour="red", size=1, alpha=0.3) +
  xlim(c(112,157)) + ylim(c(-44,-11)) +
  theme_map + coord_equal() 
```

## Incorporating other information

Election results are provided at the resolution of polling place. We can use this information to color the points. The two files need to be merged. Both have id's for the polling place, that can be used to match the records. The two party preferred votes are given in `aec2013_2pp` data, only for the Australian Labor Party and the Liberal/National Coalition. From these columns the winner needs to be calculated based on the higher percentage. This is used to colour the points on the polling places. 

```{r addresults, fig.width=7, fig.height=5, message=FALSE, error = TRUE, warning=FALSE, echo=FALSE, tidy=TRUE}
data(aec2013_2pp)
all <- merge(stns, aec2013_2pp, by.x="PollingPlaceID", by.y="PollingPlaceID")
# Find winner
all$winner <- apply(all[,c(42,44)], 1, which.max)
all$winner <- ifelse(all$winner==1, "ALP", "LNC")
ggplot(data=nat_map) +
  geom_polygon(aes(x=long, y=lat, group=group, order=order),
               fill="grey90", colour="white") + 
  geom_point(data=all, aes(x=Longitude.x, y=Latitude.x, colour=winner), size=1, alpha=0.3) +
  scale_color_manual("Party", values=c("ALP"="#FF0033", "LNC"="#0066CC")) + 
  xlim(c(112,157)) + ylim(c(-44,-11)) +
  theme_map + coord_equal() + theme(legend.position="bottom")
```

This gives a richer look at the party preferences across the country. You can see that although the big rural electorates vote the LNC overall some polling places would elect the ALP, e.g. western NSW around Broken Hill. This data would look far more interesting if the data also contained the minority parties, because there must be some polling places where the majority vote would be for a minor party, since there are some minor partt representatives in the House.  