Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created April 12, 2024 13:08
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 ryanburge/959727b3468bc7a81331ddd739107e6b to your computer and use it in GitHub Desktop.
Save ryanburge/959727b3468bc7a81331ddd739107e6b to your computer and use it in GitHub Desktop.
gg <- cces %>%
mutate(nip = case_when(religion == 11 ~ 1,
TRUE ~ 0)) %>%
group_by(year) %>%
mean_ci(nip, wt = weight, ci = .84)
gg %>%
filter(year >= 2008) %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = factor(year), y = mean, fill = mean)) +
geom_col(color = "black") +
theme_rb() +
y_pct() +
scale_fill_gradient2(low = "#c6fff9", mid = "#00a4a4", high = "#003034", midpoint = .20) +
geom_text(aes(y = mean + .008, label = paste0(lab*100, '%')), position = position_dodge(width = .9), size = 7, family = "font") +
labs(x = "", y = "", title = "Share of Americans Who Describe Their Present Religion as Nothing in Particular", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022")
save("nip_ces22.png")
gg <- cces %>%
filter(year == 2022) %>%
mutate(religpew = religion) %>%
mutate(trad = frcode(religpew == 1 ~ "Protestant",
religpew == 2 ~ "Catholic",
religpew == 3 ~ "Mormon",
religpew == 4 ~ "Orthodox",
religpew == 5 ~ "Jewish",
religpew == 6 ~ "Muslim",
religpew == 7 ~ "Buddhist",
religpew == 8 ~ "Hindu",
religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular",
religpew == 12 ~ "Something Else")) %>%
mutate(ind = case_when(pid7 == 4 | pid7 == 8 ~ 1,
TRUE ~ 0)) %>%
mutate(other = case_when(ideo5 == 6 ~ 1,
TRUE ~ 0)) %>%
mutate(both = ind + other) %>%
mutate(both = case_when(both == 2 ~ 1, TRUE ~ 0)) %>%
group_by(trad) %>%
mean_ci(both, wt = weight, ci = .84) %>%
filter(trad != "NA")
gg %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = reorder(trad, mean), y = mean, fill = trad)) +
geom_col(color = "black") +
coord_flip() +
y_pct() +
theme_rb() +
scale_fill_manual(values = c(moma.colors("Warhol", 12))) +
geom_text(aes(y = mean + .0075, label = ifelse(mean > .09, paste0(lab*100, '%'),"")), position = position_dodge(width = .9), size = 7, family = "font") +
geom_text(aes(y = mean + .005, label = ifelse(mean <= .087, paste0(lab*100, '%'),"")), position = position_dodge(width = .9), size = 7, family = "font") +
labs(x = "", y = "", title = "Share Describing Their Partisanship as Independent/Other and Ideology as Not Sure", caption = "@ryanburge\nData: Cooperative Election Study, 2022")
save("id_pid_other.png")
gg1 <- cces %>%
mutate(ind = case_when(pid7 == 4 | pid7 == 8 ~ 1,
TRUE ~ 0)) %>%
mutate(other = case_when(ideo5 == 6 ~ 1,
TRUE ~ 0)) %>%
mutate(both = ind + other) %>%
mutate(both = case_when(both == 2 ~ 1, TRUE ~ 0)) %>%
group_by(year) %>%
mean_ci(both, wt = weight, ci = .84) %>%
mutate(grp = "Entire Sample")
gg2 <- cces %>%
filter(religion == 11) %>%
mutate(ind = case_when(pid7 == 4 | pid7 == 8 ~ 1,
TRUE ~ 0)) %>%
mutate(other = case_when(ideo5 == 6 ~ 1,
TRUE ~ 0)) %>%
mutate(both = ind + other) %>%
mutate(both = case_when(both == 2 ~ 1, TRUE ~ 0)) %>%
group_by(year) %>%
mean_ci(both, wt = weight, ci = .84) %>%
mutate(grp = "Nothing in Particular")
all <- bind_rows(gg1, gg2) %>% filter(year >= 2012)
all %>%
ggplot(., aes(x = factor(year), y = mean, color = grp, group = grp)) +
geom_point(stroke = 1, shape = 21, fill = "white") +
geom_labelsmooth(aes(label = grp), method = "loess", formula = y ~ x, family = "font", linewidth = 1, text_smoothing = 30, size = 7, linewidth = 1, boxlinewidth = 0.3) +
theme_rb() +
scale_color_calc() +
scale_y_continuous(labels = percent, limits = c(0, .18)) +
labs(x = "Year", y = "", title = "Share Describing Their Partisanship as Independent/Other and Ideology as Not Sure", caption = "@ryanburge\nData: Cooperative Election Study, 2012-2022")
save("dones_nip_all_trendlines.png")
gg1 <- cces %>%
filter(year == 2008 | year == 2022) %>%
mutate(religpew = religion) %>%
mutate(trad = frcode(religpew == 1 ~ "Protestant",
religpew == 2 ~ "Catholic",
religpew == 3 ~ "Mormon",
religpew == 4 ~ "Orthodox",
religpew == 5 ~ "Jewish",
religpew == 6 ~ "Muslim",
religpew == 7 ~ "Buddhist",
religpew == 8 ~ "Hindu",
religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
mutate(college = case_when(educ == 5 | educ == 6 ~1, TRUE ~ 0)) %>%
group_by(trad, year) %>%
mean_ci(college, wt = weight, ci = .84) %>%
na.omit()
gg2 <- cces %>%
filter(year == 2008 | year == 2022) %>%
mutate(college = case_when(educ == 5 | educ == 6 ~1, TRUE ~ 0)) %>%
group_by(year) %>%
mean_ci(college, wt = weight, ci = .84) %>%
mutate(trad = "Entire Sample")
gg <- bind_rows(gg1, gg2)
gg$diff <- c("+5", "+5", "+11", "+11", "+8", "+8", "-7", "-7", "+9", "+9", "+13", "+13", "+2", "+2", "0", "0", "+18", "+18", "+11", "+11", "+7", "+7", "+10", "+10")
gg <- gg %>%
filter(trad == "Protestant" | trad == "Catholic" | trad == "Entire Sample" | trad == "Atheist" | trad == "Agnostic" | trad == "Nothing in Particular")
lvl <- gg %>%
group_by(trad) %>%
summarise(sum = sum(n))
gg <- left_join(gg, lvl)
gg$trad <- fct_reorder(gg$trad, gg$sum, .desc = TRUE)
library(ggpubr)
gg %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = year, y = mean, fill = factor(year))) +
geom_col(color = "black") +
facet_wrap(~ trad, nrow = 2) +
geom_bracket(data = gg, aes(label = diff), xmin = 2008, xmax = 2022, y.position = .57, label = "test", family = "font", label.size = 8, size = 1) +
theme_rb() +
scale_x_continuous(labels = c("2008", "2022"), breaks = c(2008, 2022)) +
scale_y_continuous(labels = percent, limits = c(0, .60), breaks = c(0, .25, .50, .75)) +
scale_fill_manual(values = c("#ed3049", "#26b5f8")) +
theme(panel.grid.major.x = element_blank()) +
theme(strip.text = element_text(size = 14)) +
geom_text(aes(y = mean + .025, label = paste0(lab*100, '%')), position = position_dodge(width = .9), size = 8, family = "font") +
labs(x = "", y = "", title = "Share with a Bachelor's Degree in 2008 vs 2022", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022")
save("educ_change_2008_2022_nn.png", wd = 6, ht = 10)
yyy1 <- cces16 %>%
mutate(trad = frcode(religpew == 1 ~ "Protestant",
religpew == 2 ~ "Catholic",
religpew == 3 ~ "Mormon",
religpew == 4 ~ "Orthodox",
religpew == 5 ~ "Jewish",
religpew == 6 ~ "Muslim",
religpew == 7 ~ "Buddhist",
religpew == 8 ~ "Hindu",
religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
mutate(meet = CC16_417a_1) %>%
mutate(sign = CC16_417a_2) %>%
mutate(work = CC16_417a_3) %>%
mutate(donate = CC16_417a_4) %>%
mutate(meet = car::recode(meet, "1=1; 2=0")) %>%
mutate(sign = car::recode(sign, "1=1; 2=0")) %>%
mutate(donate = car::recode(donate, "1=1; 2=0")) %>%
mutate(work = car::recode(work, "1=1; 2=0")) %>%
mutate(acts = meet + sign + donate + work) %>%
group_by(trad) %>%
mutate(none = case_when(acts == 0 ~ 1,
acts >= 1 ~ 0)) %>%
socsci::mean_ci(none, wt = commonweight, ci = .84) %>%
mutate(year = "2016") %>%
na.omit()
yyy2 <- cces18 %>%
mutate(trad = frcode(religpew == 1 ~ "Protestant",
religpew == 2 ~ "Catholic",
religpew == 3 ~ "Mormon",
religpew == 4 ~ "Orthodox",
religpew == 5 ~ "Jewish",
religpew == 6 ~ "Muslim",
religpew == 7 ~ "Buddhist",
religpew == 8 ~ "Hindu",
religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
mutate(meet = CC18_417a_1) %>%
mutate(sign = CC18_417a_2) %>%
mutate(work = CC18_417a_3) %>%
mutate(protest = CC18_417a_4) %>%
mutate(contact = CC18_417a_5) %>%
mutate(donate = CC18_417a_6) %>%
mutate(meet = car::recode(meet, "1=1; 2=0")) %>%
mutate(sign = car::recode(sign, "1=1; 2=0")) %>%
mutate(donate = car::recode(donate, "1=1; 2=0")) %>%
mutate(work = car::recode(work, "1=1; 2=0")) %>%
mutate(contact = car::recode(contact, "1=1; 2=0")) %>%
mutate(protest = car::recode(protest, "1=1; 2=0")) %>%
mutate(acts = meet + sign + donate + work + contact + protest) %>%
mutate(none = case_when(acts == 0 ~ 1,
acts >= 1 ~ 0)) %>%
group_by(trad) %>%
socsci::mean_ci(none, wt = commonweight, ci = .84) %>%
mutate(year = "2018") %>%
na.omit()
yyy3 <- cces20 %>%
mutate(trad = frcode(religpew == 1 ~ "Protestant",
religpew == 2 ~ "Catholic",
religpew == 3 ~ "Mormon",
religpew == 4 ~ "Orthodox",
religpew == 5 ~ "Jewish",
religpew == 6 ~ "Muslim",
religpew == 7 ~ "Buddhist",
religpew == 8 ~ "Hindu",
religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
mutate(meet = CC20_430a_1) %>%
mutate(sign = CC20_430a_2) %>%
mutate(work = CC20_430a_3) %>%
mutate(protest = CC20_430a_4) %>%
mutate(contact = CC20_430a_5) %>%
mutate(donate = CC20_430a_6) %>%
mutate(meet = car::recode(meet, "1=1; 2=0")) %>%
mutate(sign = car::recode(sign, "1=1; 2=0")) %>%
mutate(work = car::recode(work, "1=1; 2=0")) %>%
mutate(protest = car::recode(protest, "1=1; 2=0")) %>%
mutate(contact = car::recode(contact, "1=1; 2=0")) %>%
mutate(donate = car::recode(donate, "1=1; 2=0")) %>%
mutate(acts = meet + sign + donate + work + protest + contact) %>%
mutate(none = case_when(acts == 0 ~ 1,
acts >= 1 ~ 0)) %>%
group_by(trad) %>%
socsci::mean_ci(none, wt = commonweight, ci = .84) %>%
mutate(year = "2020") %>%
na.omit()
yyy4 <- cces22 %>%
mutate(trad = frcode(religpew == 1 ~ "Protestant",
religpew == 2 ~ "Catholic",
religpew == 3 ~ "Mormon",
religpew == 4 ~ "Orthodox",
religpew == 5 ~ "Jewish",
religpew == 6 ~ "Muslim",
religpew == 7 ~ "Buddhist",
religpew == 8 ~ "Hindu",
religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
mutate(meet = CC22_430a_1) %>%
mutate(sign = CC22_430a_2) %>%
mutate(work = CC22_430a_3) %>%
mutate(protest = CC22_430a_4) %>%
mutate(contact = CC22_430a_5) %>%
mutate(donate = CC22_430a_6) %>%
mutate(meet = car::recode(meet, "1=1; 2=0")) %>%
mutate(sign = car::recode(sign, "1=1; 2=0")) %>%
mutate(work = car::recode(work, "1=1; 2=0")) %>%
mutate(protest = car::recode(protest, "1=1; 2=0")) %>%
mutate(contact = car::recode(contact, "1=1; 2=0")) %>%
mutate(donate = car::recode(donate, "1=1; 2=0")) %>%
mutate(acts = meet + sign + donate + work + protest + contact) %>%
mutate(none = case_when(acts == 0 ~ 1,
acts >= 1 ~ 0)) %>%
group_by(trad) %>%
socsci::mean_ci(none, wt = commonweight, ci = .84) %>%
mutate(year = "2022") %>%
na.omit()
graph <- bind_rows(yyy1, yyy2, yyy3, yyy4)
graph %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = reorder(trad, mean), y = mean, fill = trad)) +
geom_col(color = "black") +
facet_wrap(~ year) +
coord_flip() +
theme_rb() +
error_bar() +
lab_bar(top = FALSE, type = lab, pos = .055, sz = 5) +
geom_text(aes(y = .055, label = ifelse(trad == "Nothing in Particular" | trad == "Protestant" | trad == "Orthodox" | trad == "Muslim" | trad == "Catholic", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 5, family = "font", color = "white") +
scale_fill_manual(values = c(moma.colors("Warhol", 12))) +
y_pct() +
theme(strip.text = element_text(size = 20)) +
labs(x = "", y = "", title = "Share Engaging in Zero Political Acts in the Prior Year", caption = "@ryanburge\nData: Cooperative Election Study, 2016-2022")
save("no_polacts.png", ht = 8)
graph1 <- cces %>%
filter(year >= 2020) %>%
filter(religion == 11) %>%
mutate(single = case_when(marital_status == 5 ~ 1,
TRUE ~ 0)) %>%
group_by(age) %>%
socsci::mean_ci(single, wt = weight) %>%
mutate(type = "Nothing in Particular")
graph2 <- cces %>%
filter(year >= 2020) %>%
mutate(single = case_when(marital_status == 5 ~ 1,
TRUE ~ 0)) %>%
group_by(age) %>%
socsci::mean_ci(single, wt = weight) %>%
mutate(type = "Entire Sample")
both <- bind_rows(graph1, graph2)
both %>%
filter(age <= 75) %>%
ggplot(., aes(x = age, y = mean, color = type, group = type)) +
geom_point(stroke = .5, shape = 21, alpha = .45) +
geom_labelsmooth(aes(label = type), method = "loess", formula = y ~ x, family = "font",
linewidth = 1, text_smoothing = 30, size = 6, linewidth = 1, boxlinewidth = 0.3, hjust = .75) +
scale_color_calc() +
theme_rb() +
y_pct() +
labs(x = "Age", y = "", title = "Share Reporting That They Have Never Been Married by Age",
caption = "@ryanburge\nData: Cooperative Election 2020-2022")
save("age_marital_nip.png", wd = 8, ht = 6)
regg <- cces %>%
filter(year >= 2020) %>%
filter(age >= 35 & age <= 45) %>%
mutate(nokids = case_when(havekids == 2 ~ 1,
havekids == 1 ~ 0)) %>%
mutate(single = case_when(marital_status == 5 ~ 1,
TRUE ~ 0)) %>%
mutate(both = nokids + single) %>%
mutate(both = case_when(both == 2 ~ 1,
TRUE ~ 0)) %>%
mutate(religpew = religion) %>%
mutate(trad = frcode(religpew == 1 ~ "Protestant",
religpew == 2 ~ "Catholic",
religpew == 3 ~ "Mormon",
religpew == 4 ~ "Orthodox",
religpew == 5 ~ "Jewish",
religpew == 6 ~ "Muslim",
religpew == 7 ~ "Buddhist",
religpew == 8 ~ "Hindu",
religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
mutate(male = case_when(gender == 1 ~ 1,
gender == 2 ~ 0)) %>%
mutate(white = case_when(race == 1 ~ 1,
TRUE ~ 0)) %>%
mutate(lib = case_when(ideo5 == 1 | ideo5 == 2 ~ 1,
ideo5 <= 5 ~ 0)) %>%
select(age, income, educ, male, white, lib, single, nokids, both, trad)
one <- glm(nokids ~ trad + male + white + age + income + educ + lib, family = "binomial", data = regg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment