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.
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//RtmpTB6zDE/downloaded_packages
install.packages("data.table")
##
## The downloaded binary packages are in
## /var/folders/9h/nkdjn38s7gn3bb051r2lwzdm0000gn/T//RtmpTB6zDE/downloaded_packages
install.packages("ggplot2")
##
## The downloaded binary packages are in
## /var/folders/9h/nkdjn38s7gn3bb051r2lwzdm0000gn/T//RtmpTB6zDE/downloaded_packages
install.packages("scales")
##
## The downloaded binary packages are in
## /var/folders/9h/nkdjn38s7gn3bb051r2lwzdm0000gn/T//RtmpTB6zDE/downloaded_packages
install.packages("RColorBrewer")
##
## The downloaded binary packages are in
## /var/folders/9h/nkdjn38s7gn3bb051r2lwzdm0000gn/T//RtmpTB6zDE/downloaded_packages
install.packages("tidytuesdayR")
##
## The downloaded binary packages are in
## /var/folders/9h/nkdjn38s7gn3bb051r2lwzdm0000gn/T//RtmpTB6zDE/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"),]
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))
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"),]
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))
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'
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.
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)]
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"))
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.
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"))
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)]
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))
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)]
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.
install.packages(c("ggplot2", "sf", "dplyr", "maps", "usmap", "ggtext"))
##
## The downloaded binary packages are in
## /var/folders/9h/nkdjn38s7gn3bb051r2lwzdm0000gn/T//RtmpTB6zDE/downloaded_packages
library(ggplot2)
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(dplyr)
library(maps)
##
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
##
## map
library(usmap)
library(ggtext)
usa_map <- usmap::us_map(regions = "states")
average_votes_before_2012_filtered <- average_votes_before_2012 %>%
filter(party %in% c("REPUBLICAN", "DEMOCRAT")) %>%
mutate(state = tolower(state))
party_with_most_votes_before_2012 <- average_votes_before_2012_filtered %>%
group_by(state) %>%
filter(average_votes == max(average_votes)) %>%
ungroup()
usa_map$full <- tolower(usa_map$full)
merged_data_before_2012 <- left_join(usa_map, party_with_most_votes_before_2012, by = c("full" = "state"))
ggplot() +
geom_sf(data = merged_data_before_2012, aes(fill = party)) +
geom_sf_text(data = merged_data_before_2012, aes(label = abbr), size = 3, color = "black",
hjust = 0.5, vjust = 0.5, fontface = "bold", family = "serif") +
scale_fill_manual(values = c("blue", "red"), name = "Party", labels = c("Democrat", "Republican")) +
labs(title = "Parties with most average votes before 2012",
subtitle = "The two major parties with the highest average votes in each state before 2012.") +
theme_void() +
theme(legend.position = "bottom")
average_votes_after_2012_filtered <- average_votes_after_2012 %>%
filter(party %in% c("REPUBLICAN", "DEMOCRAT")) %>%
mutate(state = tolower(state))
party_with_most_votes_after_2012 <- average_votes_after_2012_filtered %>%
group_by(state) %>%
filter(average_votes == max(average_votes)) %>%
ungroup()
usa_map$full <- tolower(usa_map$full)
merged_data <- left_join(usa_map, party_with_most_votes_after_2012, by = c("full" = "state"))
ggplot() +
geom_sf(data = merged_data, aes(fill = party)) +
geom_sf_text(data = merged_data, aes(label = abbr), size = 3, color = "black",
hjust = 0.5, vjust = 0.5, fontface = "bold", family = "serif") +
scale_fill_manual(values = c("blue", "red"), name = "Party", labels = c("Democrat", "Republican")) +
labs(title = "Party with Most Average Votes After 2012",
subtitle = "The two major parties with the highest average votes in each state after 2012.") +
theme_void() +
theme(legend.position = "bottom")
## Interpretation of the map After 2012 we can see that
Indiana, Pennsylvania, North Dakota, South Dakota, Wisconsin,
Mississippi, North Carolina, West Virginia became more Republican from
Democrat. Meanwhile, only Nevada and New Hampshire changed their
affiliation from Republican to Democrat. So as a conclusion it can be
stated Republicans are outperforming Democrats in the US House elections
since the widespread of social media tools.
install.packages("gganimate")
##
## The downloaded binary packages are in
## /var/folders/9h/nkdjn38s7gn3bb051r2lwzdm0000gn/T//RtmpTB6zDE/downloaded_packages
library(gganimate)
p <- ggplot(filtered_party_avg_total, aes(x = as.factor(year), y = average_votes, fill = party)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Average Votes for Democrats and Republicans during House elections (1976-2022)",
x = "Election Year",
y = "Average Votes",
fill = "Party") +
theme_bw() +
theme(axis.text.x = element_text(angle = 90, 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")) +
geom_text(aes(label = ""), position = position_dodge(width = 0.9),
vjust = -0.5, size = 3) +
facet_wrap(~ state, scales = "free_y")
p_animated <- p + transition_states(year, transition_length = 2, state_length = 1)
animate(p_animated, fps = 10, width = 1980, height = 1080)
sum_filtered_total_avg_votes <- filtered_party_avg_total %>%
group_by(year, party) %>%
summarise(total_average_votes = sum(average_votes)) %>%
ungroup()
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
ggplot(sum_filtered_total_avg_votes, aes(x = year, y = total_average_votes, fill = party)) +
geom_area() +
labs(title = "Total average votes for Democrats and Republicans throughout 1976-2022",
x = "Year",
y = "Total average votes (millions)") +
scale_y_continuous(labels = scales::label_number(scale = 1e-6, suffix = "M")) +
scale_fill_manual(values = c(alpha("royalblue", 1), alpha("red3", 1)), labels = c("Democrat", "Republican")) +
theme_bw()
## Interpretation of stacked area chart It’s clearly
visible on the stacked area chart that presidential election years
always bring higher voting turnout in the US house elections.
install.packages("plotly")
##
## The downloaded binary packages are in
## /var/folders/9h/nkdjn38s7gn3bb051r2lwzdm0000gn/T//RtmpTB6zDE/downloaded_packages
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
color_data <- rbind(average_votes_before_2012, average_votes_after_2012)
party_colors <- c("DEMOCRAT" = "royalblue", "REPUBLICAN" = "red3")
plot_before_2012 <- average_votes_before_2012 %>%
filter(party %in% c("DEMOCRAT", "REPUBLICAN")) %>%
plot_ly(x = ~year, y = ~average_votes, color = ~party, colors = party_colors,
type = 'scatter', mode = 'markers', text = ~paste("State: ", state, "<br>", "Year: ", year, "<br>", "Average Votes: ", round(average_votes)),
hoverinfo = 'text') %>%
layout(title = "Average Votes Before 2012",
xaxis = list(title = "Year"),
yaxis = list(title = "Average Votes"),
showlegend = TRUE,
updatemenus = list(
list(
buttons = list(
list(method = "restyle",
args = list("visible", list(TRUE, FALSE)),
label = "Democrats"),
list(method = "restyle",
args = list("visible", list(FALSE, TRUE)),
label = "Republicans"),
list(method = "restyle",
args = list("visible", list(TRUE, TRUE)),
label = "Both")
),
x = 1.02,
xanchor = "left",
y = 1,
yanchor = "top"
)
)) %>%
add_markers(size = ~average_votes * 0.1, opacity = 0.7) %>%
style(marker = list(opacity = 0.7))
## Create interactive scatter plot for average votes before 2012 with filter
plot_after_2012 <- average_votes_after_2012 %>%
filter(party %in% c("DEMOCRAT", "REPUBLICAN")) %>%
plot_ly(x = ~year, y = ~average_votes, color = ~party, colors = party_colors,
type = 'scatter', mode = 'markers', text = ~paste("State: ", state, "<br>", "Year: ", year, "<br>", "Average Votes: ", round(average_votes)),
hoverinfo = 'text') %>%
layout(title = "Average Votes Before 2012",
xaxis = list(title = "Year"),
yaxis = list(title = "Average Votes"),
showlegend = TRUE,
updatemenus = list(
list(
buttons = list(
list(method = "restyle",
args = list("visible", list(TRUE, FALSE)),
label = "Democrats"),
list(method = "restyle",
args = list("visible", list(FALSE, TRUE)),
label = "Republicans"),
list(method = "restyle",
args = list("visible", list(TRUE, TRUE)),
label = "Both")
),
x = 1.02,
xanchor = "left",
y = 1,
yanchor = "top"
)
)) %>%
add_markers(size = ~average_votes * 0.1, opacity = 0.7) %>%
style(marker = list(opacity = 0.7))
subplot(plot_before_2012)
subplot(plot_after_2012)
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. With the aditional visualization this project became more punctual and gave a detailful insight of the trends in US politics. One of the most interesting finsing of this project was, while the mainstream press often promotes Democrats as more popular and better in case of using social media tools, the data from this particular dataset shows it differently. Since 2012 Republicans could outperform Democrats in the single voting constituency candidate run for the US House of Representatives.