Skip to content

Instantly share code, notes, and snippets.

@steveharoz
Created October 21, 2021 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveharoz/404f119c6b6c00c5cdab4ee2e1de6721 to your computer and use it in GitHub Desktop.
Save steveharoz/404f119c6b6c00c5cdab4ee2e1de6721 to your computer and use it in GitHub Desktop.
Texas congressional district simulation
library(tidyverse)
# arbitrary number
district_count = 38
# population from stephanie's figure
# https://twitter.com/evergreendata/status/1450862060972216320
population = c(
rep("White", 40),
rep("Latino", 39),
rep("Black", 12),
rep("Asian", 5),
rep("Other", 4)
)
population = rep(population, each=district_count)
# what is the majority in a given district
# if no ethnicity is >50%, return "no majority"
get_majority = function(ethnicity) {
majority = (table(ethnicity) %>% sort(TRUE))[1]
ifelse(majority / length(ethnicity) <= 0.5,
"no majority",
majority %>% as.list() %>% names())
}
simulate = function() {
tibble(
ethnicity = sample(population, length(population), replace=FALSE),
district = 0:(length(ethnicity)-1) %/% district_count
) %>%
group_by(district) %>%
summarize(majority = get_majority(ethnicity)) %>%
ungroup() %>%
count(majority)
}
simulated_results = lapply(1:1000, function(x) simulate() %>% mutate(iteration=x))
simulated_results = simulated_results %>% bind_rows()
simulated_results = simulated_results %>% mutate(majority = factor(majority, c("White", "Latino", "Black", "Asian", "no majority")))
ggplot(simulated_results) +
aes(x=n, y=fct_rev(majority), fill=majority) +
ggdist::stat_histinterval() +
scale_fill_manual(values = c(
"White" = "#1B9E77",
"Latino" = "#D95F02",
"no majority" = "gray"
)) +
guides(fill = "none") +
labs(x = NULL, y=NULL,
title = "If everyone in Texas lived in a random location and there were no gerrymandering,\nwhat would the majority racial group (>50%) be for the districts?",
#subtitle = "(a simulation)",
caption = "data source: @evergreendata\nby Steve Haroz") +
theme_minimal(18)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment