Skip to content
Toronto voting results as an R package
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
R
data-raw
data
inst/extdata
man
.Rbuildignore
.gitignore
DESCRIPTION
LICENSE
NAMESPACE
README.Rmd
toVotes.Rproj

README.Rmd

toVotes
=======

## Municipal

This package contains cleaned and collated poll by poll results from the 2003, 2006, 2010, and 2014 mayoral elections in Toronto. The original data is from [Toronto Open Data](http://www1.toronto.ca/wps/portal/contentonly?vgnextoid=834689fe9c18b210VgnVCM1000003dd60f89RCRD)

The `toVotes` dataframe contains the:

* year of the election as a factor
* name of the candidate as last name first name
* number of votes received by the candidate
* ward for the polling station
* area for the polling station
* type of election, currently just Mayor

The `toTurnout` dataframe contains the:

* year of the election as a factor
* ward for the polling station
* area for the polling station
* the proportion of eligible voters that voted
* type of election, currently just Mayor

Also included is the `toPollGeo` dataframe which contains geocode data for the polling stations.

## Federal

`toFederalVotes` contains the results of the 2006, 2008, and 2011 Federal elections for Electoral Districts in Toronto, taken from [Elections Canada](http://www.elections.ca/). The dataframe contains:

* year of the election as a factor
* name of the candidate as last name first name
* number of votes received by the candidate
* electoral district for the polling station
* poll number for the polling station
* type of election as federal
* the party affiliation of the candidate
* a logical value indicating if the candidate was the incumbent

## Installation

    library(devtools)
    devtools::install_github("Psepho/toVotes")
    library(toVotes)
    data(toVotes)
    
## Using geocoded data

To join `toPollGeo` with `toVotes`, one approach is to use `dplyr`:

```{r}
library(dplyr)
vote_geo <- left_join(toVotes, toPollGeo)
```

Then you could map the number of votes in each area as follows:

```{r}
votes_by_area <- toVotes %>%
  group_by(year, ward, area) %>%
  summarize(votes = sum(votes))
votes_by_area <- left_join(votes_by_area, toPollGeo)
library(ggmap)
library(mapproj)
toronto_map <- qmap("queens park,toronto", zoom = 11, maptype = 'terrain')
toronto_map +
  geom_polygon(aes(x=long, y=lat, group=group, fill=votes), alpha = 5/6, data=votes_by_area) + 
  scale_colour_brewer("Votes") + 
  facet_wrap(~year)
```
    
## Helper function

The package also contains a helper function `importElectionWorksheets` which was used to convert the source Excel files into dataframes. For example, importing the 2010 election results could proceed as:

```{r}
processFiles <- function(pattern) { # Takes a filename pattern and returns a dataframe
  lfiles <- list.files(pattern = fpattern) # Find all files that match the pattern
  results <- do.call("rbind", lapply(lfiles, importWorksheets)) # Collapse all down to a single dataframe
  row.names(results) <- NULL # Clear the rownames
  }
fyear <- "2010"
fpattern <- paste(fyear,".*_Mayor.xls?",sep="") # Import several files at once
election_results <- processFiles(fpattern)
```
You can’t perform that action at this time.