Skip to content

Instantly share code, notes, and snippets.

@USMortality
Created November 8, 2024 16:04
Show Gist options
  • Save USMortality/88bd7c05f34853dd28a6c892123072d4 to your computer and use it in GitHub Desktop.
Save USMortality/88bd7c05f34853dd28a6c892123072d4 to your computer and use it in GitHub Desktop.
Electoral College Votes by Presidential Election [USA]
library(ggplot2)
library(dplyr)
library(readr)
sf <- 2
width <- 600 * sf
height <- 335 * sf
options(vsc.dev.args = list(width = width, height = height, res = 72 * sf))
# Electoral College data (1976-2020)
ec_data <- data.frame(
year = c(
1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2024
),
dem_ec = c(
297, 49, 13, 111,
370, 379, 266, 251,
365, 332, 227, 306, 226
),
rep_ec = c(
240, 489, 525, 426,
168, 159, 271, 286,
173, 206, 304, 232, 312
)
)
# Plot Electoral College votes
chart <- ggplot(ec_data, aes(x = year)) +
geom_line(aes(y = dem_ec, color = "Democratic"), size = 1.2) +
geom_line(aes(y = rep_ec, color = "Republican"), size = 1.2) +
geom_point(aes(y = dem_ec, color = "Democratic"), size = 3) +
geom_point(aes(y = rep_ec, color = "Republican"), size = 3) +
geom_hline(yintercept = 270, linetype = "dashed", color = "black", size = 1) +
labs(
title = "Electoral College Votes by Presidential Election [USA]",
subtitle = "Source: DDHQ, Wikipedia · @USMortality",
x = "Year", y = "Electoral College Votes",
color = "Party"
) +
scale_color_manual(values = c("Democratic" = "blue", "Republican" = "red")) +
scale_y_continuous(labels = scales::comma, limits = c(0, 550)) +
theme_bw()
ggsave(
filename = "chart1.png", plot = chart, width = width, height = height,
units = "px", dpi = 72 * sf, device = grDevices::png, type = "cairo"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment