Project Goals

# load libraries
library(rvest)
library(emo)
library(readr)
library(dplyr)
library(forcats)
library(ggplot2)
library(wesanderson)
library(stringr)
library(dumas) # my personal pacakge on GitHub

Parsed Data Overview:

potus_election <- read_csv("https://raw.githubusercontent.com/jakekara/ct-election-2016-certified-data/master/output/merged_state_votes_pretty.csv")

head(potus_election) 

Exploratory Data Analysis

# define percentage function
percentage <- function(n) {
  return(as.numeric(sprintf("%.2f", n /sum(n) * 100)))
}

# how many differnt parties represented = 10
potus_election %>% group_by(party_name) %>% 
  dplyr::filter(!is.na(party_name)) %>% 
  summarise(count = n()) %>% 
  mutate(percentage = percentage(count)) %>% 
  ggplot(., aes(x = fct_reorder(party_name, percentage), y = percentage)) +
    geom_col(fill = wes_palette("Moonrise1", 1)) +
    coord_flip() +
    labs(title = "Candidate Political Party Affiliation", 
         subtitle = "Most candidates run as Republican, Democrat or Independent",
         x = "", y = "Percentage (%)") +
    theme_minimal()

potus_election %>% group_by(party_name) %>% 
  dplyr::filter(party_name %in% c("Democratic Party", "Republican Party")) %>% 
  summarise(count = sum(vote_count)) %>% 
  ggplot(., aes(x = party_name, y = count, fill = count)) +
    geom_col(fill = wes_palette("Moonrise2", 1)) +
    labs(title = "Count of Votes Recieved by Party", subtitle = "Democratic candidates recieved more total votes aggregated by all races",
         x = "", y = "Count") +
    theme_minimal()

# remove numbers from office name
potus_election$race <- str_replace_all(potus_election$office, "[0-9]", "")
# vote participation by office type?
potus_election %>% group_by(race) %>% 
  summarise(count = sum(vote_count)) %>% 
  arrange(desc(count)) %>% 
  mutate(rank = 1:nrow(.), 
         percentage = percentage(count)) %>% 
  dplyr::filter(rank < 6) %>% 
  ggplot(., aes(x = fct_reorder(race, percentage), y = percentage)) +
    geom_col(fill = wes_palette("Moonrise3", 1)) +
    coord_flip() +
    labs(title = "% of Votes Recieved by Office", subtitle = "Aggregated by all candidates",
         x = "", y = "Percentage (%)") +
    theme_minimal()

Summary

  • More Republican affiliated candidates ran for office in 2016 in Connecticut than any other race, however Democrats received more share of the votes aggregated across all races.

  • The less “prestigious” office, the fewer amount of voters participate in. This could be due to the voting ballot listing the candidates in office order from left to right, potentially fatiguing voters to vote for less known candidates in races they are unclear about.

Example West Hartford Ballot

Example West Hartford Ballot


fin.