United States Capitol
United States Capitol

Introduction of the project

The following project is going to examine the United States House of Representatives election results between 1976 and 2022. Furthermore, the project will also shortly show how the spread of social media tools in campaigning has boosted voting participation.

Set CRAN mirror

options(repos = c(CRAN = "https://cloud.r-project.org"))

Install and load necessary libraries

install.packages("tidyverse")
## 
## The downloaded binary packages are in
##  /var/folders/9h/nkdjn38s7gn3bb051r2lwzdm0000gn/T//RtmpUs76wp/downloaded_packages
install.packages("data.table")
## 
## The downloaded binary packages are in
##  /var/folders/9h/nkdjn38s7gn3bb051r2lwzdm0000gn/T//RtmpUs76wp/downloaded_packages
install.packages("ggplot2")
## 
## The downloaded binary packages are in
##  /var/folders/9h/nkdjn38s7gn3bb051r2lwzdm0000gn/T//RtmpUs76wp/downloaded_packages
install.packages("scales")
## 
## The downloaded binary packages are in
##  /var/folders/9h/nkdjn38s7gn3bb051r2lwzdm0000gn/T//RtmpUs76wp/downloaded_packages
install.packages("RColorBrewer")
## 
## The downloaded binary packages are in
##  /var/folders/9h/nkdjn38s7gn3bb051r2lwzdm0000gn/T//RtmpUs76wp/downloaded_packages
install.packages("tidytuesdayR")
## 
## The downloaded binary packages are in
##  /var/folders/9h/nkdjn38s7gn3bb051r2lwzdm0000gn/T//RtmpUs76wp/downloaded_packages
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(data.table)
## 
## Attaching package: 'data.table'
## 
## The following objects are masked from 'package:lubridate':
## 
##     hour, isoweek, mday, minute, month, quarter, second, wday, week,
##     yday, year
## 
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
## 
## The following object is masked from 'package:purrr':
## 
##     transpose
library(ggplot2)
library(scales)
## 
## Attaching package: 'scales'
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## The following object is masked from 'package:readr':
## 
##     col_factor
library(RColorBrewer)

Read in the dataset

tuesdata <- tidytuesdayR::tt_load(2023, week = 45)
## --- Compiling #TidyTuesday Information for 2023-11-07 ----
## --- There is 1 file available ---
## --- Starting Download ---
## 
##  Downloading file 1 of 1: `house.csv`
## --- Download complete ---
house <- tuesdata$house

Setting the threshold for social media

The first widespread occurrence of social media was in 2008 by Barack Obama (Hendircks et al., 2010) during the presidential race against John McCain. This study supposes that politicians, after 2008, more frequently used social media, and by 2012, it could be considered widespread.

Filter rows where the year is greater than or equal to 2012

house <- as.data.table(house)
house <- house[!(state == "MAINE" & year == 2022)] ## Excluding one heavy outlier 
result_after_2012 <- house[year >= 2012]

Calculating average casted votes in the US house elections for Republicans and Democrats after 2012

average_votes_after_2012 <- result_after_2012[!is.na(party), .(average_votes = mean(candidatevotes, na.rm = TRUE)), by = .(party)]

Calculating average casted votes in the US house elections for Republicans and Democrats before 2012

average_votes_before_2012 <- house[year < 2012 & !is.na(party), .(average_votes = mean(candidatevotes, na.rm = TRUE)), by = .(party)]

Filter data for Democrats and Republicans only before 2012

filtered_data_before_2012 <- average_votes_before_2012[party %in% c("DEMOCRAT", "REPUBLICAN"),]

Create a bar plot before 2012

ggplot(filtered_data_before_2012, aes(x = party, y = average_votes, fill = party, label = round(average_votes))) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(position = position_dodge(width = 0.9),
            vjust = -0.5, 
            size = 4, 
            color = "black") +
  labs(title = "Average Votes Casted Before 2012",
       x = "Two major parties",
       y = "Number of average votes") +
  scale_fill_manual(values = c(alpha("blue", 0.85), alpha("red", 0.85)), labels = c("Democrat", "Republican")) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Interpretation

Between 1976 and 2012 Democrats are tightly leading in terms of average votes in all the states.

Filter data for Democrats and Republicans only after 2012

filtered_data_after_2012 <- average_votes_after_2012[party %in% c("DEMOCRAT", "REPUBLICAN"),]

Create a bar plot after 2012

ggplot(filtered_data_after_2012, aes(x = party, y = average_votes, fill = party, label = round(average_votes))) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(position = position_dodge(width = 0.9),
            vjust = -0.5, 
            size = 4, 
            color = "black") +
  labs(title = "**Average Votes Casted After 2012**",
       x = "Two major parties",
       y = "Number of average votes") +
  scale_fill_manual(values = c(alpha("blue", 0.85), alpha("red", 0.85)), labels = c("Democrat", "Republican"))+
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Interpretation

However, after 2012, Republicans could improve their performance. This simple fact could show that Republican candidates were able to use social media tools better, even if Democrats are traditionally more popular among young people.

Recalculating the average votes with parties and states included before 2012

average_votes_before_2012 <- house[year < 2012 & !is.na(party), .(average_votes = mean(candidatevotes, na.rm = TRUE)), by = .(party, state, year)]

Recalculating the average votes with parties and states included after 2012

average_votes_after_2012 <- house[year >= 2012 & !is.na(party), .(average_votes = mean(candidatevotes, na.rm = TRUE)), by = .(party, state, year)]

Calculate grand average for the better visualization

grand_average <- mean(filtered_data_after_2012$average_votes, na.rm = TRUE)

Calculate average votes for Democrats and Republicans across all states for each year

party_avg_after_2012 <- average_votes_after_2012[, .(average_votes = mean(average_votes, na.rm = TRUE)), by = .(year, party)]

Filter data for Democrats and Republicans only in party average

filtered_party_avg_after_2012 <- party_avg_after_2012[party %in% c("DEMOCRAT", "REPUBLICAN"), .(year, party, average_votes)]

##Showing the average results of the democrats and republicans after 2012

ggplot(filtered_party_avg_after_2012, aes(x = as.factor(year), y = average_votes, fill = party)) +
  geom_bar(stat = "identity", position = "dodge", width = 0.7) +
  geom_text(aes(label = round(average_votes)), position = position_dodge(width = 0.7), vjust = -0.5, size = 3) +
  geom_smooth(aes(group = party, color = party), method = "loess", se = FALSE, size = 0.8, span = 1) +
  labs(title = "Average Votes for Democrats and Republicans After 2012",
       x = "Election Year",
       y = "Average Votes",
       fill = "Party") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  scale_y_continuous(labels = scales::number_format(scale = 1)) + 
  scale_fill_manual(values = c(alpha("royalblue", 1), alpha("red3", 1)), labels = c("Democrat", "Republican")) +
  scale_color_manual(values = c("lawngreen", "yellow"), labels = c("Democrat", "Republican")) +
  geom_text(aes(label = round(grand_average)), x = length(unique(party_avg_after_2012$year)) + 1)
## `geom_smooth()` using formula = 'y ~ x'

Interpretation

Looking more deeply into the data after 2012, it is very visible how presidential election years (2012, 2016, 2020) are influencing voter participation in a positive way. It is also clear that Republicans were able to raise their total average votes after 2012, and in most of the years, they received more votes than their Democratic counterparts.

Creating a density for each states’ average between 1976-2022

Making a total average for the plotting

average_votes_total <- house[!is.na(party), .(average_votes = mean(candidatevotes, na.rm = TRUE)), by = .(party, state, year)]

filtered_party_avg_total <- average_votes_total[party %in% c("DEMOCRAT", "REPUBLICAN"), .(year, party, state, average_votes)]

Plotting the density for each states between 1976-2022

ggplot(filtered_party_avg_total, aes(x = average_votes, fill = party)) +
  geom_density(alpha = 0.6, aes(y = ..scaled..)) + 
  facet_wrap(~state) +
  labs(title = "Density of average votes for Democrats and Republicans between 1976-2022",
       x = "Average Votes",
       y = "Density (Normalized)",
       fill = "Party") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  scale_x_continuous(labels = scales::number_format(scale = 1)) +
  scale_fill_manual(values = c("royalblue", "red3"))

Interpretation

Through the years, it has been visible that in most of the states, the Democrats and Republicans performances are quite equal.However, in some states, such as Vermont, Maine, Massachusetts, Hawaii, Rhode Island, and Maryland, Democrats are dominant. On the other hand, in the states of Alabama, Alaska, Idaho, Kentucky, Montana, Nebraska, Utah, and Wyoming, Republicans can be considered dominant parties.

Plotting with box plots for each states between 1976-2022

ggplot(filtered_party_avg_total, aes(x = factor(party), y = average_votes, fill = factor(party))) +
  geom_boxplot(alpha = 0.7) +
  facet_wrap(~state, scales = "free_y") +
  labs(title = "Box Plots of Average Votes for Democrats and Republicans between 1976-2022",
       x = "Party",
       y = "Average Votes") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  scale_x_discrete() +
  scale_fill_manual(values = c("royalblue", "red3"))

Interpretation

On the boxplots, the same conclusions can be drawn as from the density map. It is clearly visible which states are more likely to vote for Democratic or Republican candidates in the House of Representatives elections.

##Creating a heatmap for Democrats in each states between 1976-2022

Filtering for Democrats

filtered_dem_avg_total <- average_votes_total[party %in% c("DEMOCRAT"), .(year, party, state, average_votes)]

Plotting the heatmap for Democrats in each states between 1976-2022

filtered_dem_avg_total %>%
  ggplot(aes(x = year, y = state, fill = average_votes)) +
  geom_tile() +
  scale_fill_distiller(name = "Average votes", palette = "Spectral", 
                       breaks = scales::pretty_breaks(n = 9),
                       labels = scales::label_number(scale = 1e-3, suffix = "K")) +
  labs(title = "Heatmap of average votes for Democrats in each state between 1976-2022",
       x = "Year",
       y = "State") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Interpretation

Diving more detail into the data with a heatmap between 1976 and 2022 for Democrats. It is possible to see how Democrats improved their average performance in some states and how they weakened in others.

##Creating a heatmap for Republicans in each states between 1976-2022

Filtering for Republicans

filtered_rep_avg_total <- average_votes_total[party %in% c("REPUBLICAN"), .(year, party, state, average_votes)]

Plotting the heatmap for Republicans in each states between 1976-2022

filtered_rep_avg_total %>%
  ggplot(aes(x = year, y = state, fill = average_votes)) +
  geom_tile() +
  scale_fill_distiller(name = "Average votes", palette = "Spectral", 
                       breaks = scales::pretty_breaks(n = 9),
                       labels = scales::label_number(scale = 1e-3, suffix = "K")) +
  labs(title = "Heatmap of average votes for Republicans in each state between 1976-2022",
       x = "Year",
       y = "State") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Interpretation

Diving more detail into the data with a heatmap between 1976 and 2022 for Republicans. It is possible to see how Republicans improved their average performance in some states and how they weakened in others.

Conclusions

This project from the United States House of Representatives gives a short insight into how the two major parties have changed throughout the years. While some states have switched to other parties, in the majority of them, the race was very tight, as could be seen from the average performances. On the other hand, while the mainstream press often promotes Democrats as more popular, the data from this particular dataset shows it differently, at least for the candidates in a single voting constituency for the US House of Representatives.

Citing the author of the dataset

MIT Election Data and Science Lab. (2017). U.S. House 1976–2022. Harvard Dataverse. doi: 10.7910/DVN/IG0UN2. }