Last active
March 29, 2025 14:49
-
-
Save tkeskinturk/9ea393712e2ad645e6504b224ac51507 to your computer and use it in GitHub Desktop.
Code for processing preferences on defunding the police
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # --- package | |
| pacman::p_load( | |
| gssr, | |
| hrbrthemes, | |
| tidyverse, | |
| patchwork, | |
| marginaleffects) | |
| # --- helpers | |
| my_oka <- | |
| c("#82B2C0", | |
| "#F6C7B3", | |
| "#eebd64") | |
| us_oka <- | |
| c("#013364", | |
| "#cbcaca", | |
| "#cc0000") | |
| theme_set(theme_ipsum_rc( | |
| grid = "Y", | |
| axis_title_size = 12, | |
| plot_margin = | |
| margin(10, 10, 10, 10) | |
| )) | |
| # ---------------------------------------------------------------------------- # | |
| # PART 1: GSS DATA PREPS ----------------------------------------------------- # | |
| # ---------------------------------------------------------------------------- # | |
| # --- data | |
| d <- gssr::gss_get_yr(year = 2021) | |
| # --- wrangle | |
| d <- d |> | |
| select( | |
| id, | |
| wt = wtssnrps, | |
| age, | |
| partyid, | |
| defund | |
| ) |> | |
| haven::zap_labels() |> | |
| drop_na() |> | |
| mutate(defund = if_else(defund == 1, 1, 0), | |
| partyid = case_when( | |
| partyid %in% c(0, 1, 2) ~ "Democrat", | |
| partyid %in% c(3, 7) ~ "Independent", | |
| partyid %in% c(4, 5, 6) ~ "Republican" | |
| )) |> | |
| ## bounding | |
| mutate( | |
| age = if_else(age < 20, 20, age), | |
| age = if_else(age > 85, 85, age) | |
| ) | |
| # --- summing | |
| s <- d |> | |
| mutate(agegr = floor(age / 5) * 5) |> | |
| summarize( | |
| ymean = weighted.mean(defund, | |
| w = wt), | |
| .by = c("agegr") | |
| ) |> | |
| arrange(agegr) | |
| # --- summing, party ID | |
| p <- d |> | |
| mutate(agegr = floor(age / 5) * 5) |> | |
| summarize( | |
| ymean = weighted.mean(defund, | |
| w = wt), | |
| .by = c("agegr", "partyid") | |
| ) |> | |
| arrange(agegr) | |
| # ---------------------------------------------------------------------------- # | |
| # PART 2: DRAW --------------------------------------------------------------- # | |
| # ---------------------------------------------------------------------------- # | |
| # --- plot 1 | |
| png( | |
| "gss-01.png", | |
| w = 8, | |
| h = 6, | |
| units = "in", | |
| res = 500 | |
| ) | |
| s |> | |
| ggplot(aes(x = agegr, y = ymean)) + | |
| scale_x_continuous( | |
| limits = c(20, 85), breaks = seq(20, 85, by = 5)) + | |
| scale_y_continuous( | |
| limits = c(0.1, 0.7), | |
| breaks = seq(0.1, 0.7, by = 0.1), | |
| labels = scales::label_percent() | |
| ) + | |
| geom_point(shape = 1, col = my_oka[1], size = 2.5) + | |
| geom_smooth( | |
| se = FALSE, | |
| linewidth = 0.5, | |
| method = "lm", | |
| formula = y ~ x, | |
| color = "black" | |
| ) + | |
| labs(x = "Age", | |
| y = "Percent Favoring Police Defunding", | |
| title = "Age/Cohort Differences on Defunding the Police", | |
| subtitle = "General Social Survey, 2021, N = 2,215.", | |
| caption = "@tkeskinturk.bsky.social") | |
| dev.off() | |
| # --- plot 2 | |
| png( | |
| "gss-02.png", | |
| w = 12, | |
| h = 5, | |
| units = "in", | |
| res = 500 | |
| ) | |
| p |> | |
| ggplot(aes(x = agegr, y = ymean, col = partyid)) + | |
| scale_x_continuous( | |
| limits = c(20, 85), breaks = seq(20, 85, by = 5)) + | |
| facet_wrap(~partyid) + | |
| scale_y_continuous( | |
| limits = c(0, 1), | |
| breaks = seq(0, 1, by = 0.1), | |
| labels = scales::label_percent() | |
| ) + | |
| geom_point(shape = 1, size = 2.5) + | |
| geom_smooth( | |
| se = FALSE, | |
| linewidth = 0.5, | |
| method = "lm", | |
| formula = y ~ x + I(x^2) | |
| ) + | |
| labs(x = "Age", | |
| y = "Percent Favoring Police Defunding", | |
| title = "Age/Cohort Differences on Defunding the Police", | |
| subtitle = "General Social Survey, 2021, N = 2,215.", | |
| caption = "@tkeskinturk.bsky.social") + | |
| theme(legend.position = "none") + | |
| scale_color_manual(values = us_oka) | |
| dev.off() | |
| # ---------------------------------------------------------------------------- # |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment