-
-
Save ryanburge/271fc524d32d9edf54f5f4c985baf683 to your computer and use it in GitHub Desktop.
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
| cces_educ <- function(df, var){ | |
| df %>% | |
| mutate(educ = frcode({{var}} == 1 ~ "No HS", | |
| {{var}} == 2 ~ "HS\nGrad", | |
| {{var}} == 3 ~ "Some\nColl.", | |
| {{var}} == 4 ~ "2 Yr.", | |
| {{var}} == 5 ~ "4 Yr.", | |
| {{var}} == 6 ~ "Post-\nGrad")) | |
| } | |
| graph <- cces %>% | |
| filter(year >= 2008) %>% | |
| cces_educ(educ) %>% | |
| mutate(none = case_when(religion == 9 | religion == 10 | religion == 11 ~ 1, | |
| TRUE ~ 0)) %>% | |
| group_by(educ, year) %>% | |
| mean_ci(none, wt = weight, ci = .84) | |
| graph %>% | |
| mutate(lab = round(mean, 2)) %>% | |
| ggplot(., aes(x = educ, y = mean, group = factor(year), fill = factor(year))) + | |
| geom_col(color = "black") + | |
| facet_wrap(~ year) + | |
| theme_rb() + | |
| geom_smooth(se = FALSE, method = lm, color = "black", linetype = "twodash", linewidth = .5) + | |
| scale_fill_manual(values = c(met.brewer("Cross", 15))) + | |
| lab_bar(top = FALSE, type = lab, pos = .053, sz = 4) + | |
| geom_text(aes(y = .053, label = ifelse(year >= 2020, paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 4, family = "font", color = "white") + | |
| y_pct() + | |
| error_bar() + | |
| labs(x = "Highest Level of Education", y = "", title = "Share Who Identify as Atheist, Agnostic, or Nothing in Particular", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022") | |
| save("educ_nones_ces22.png", ht = 9) | |
| gg <- ns %>% | |
| mutate(educ = frcode(education <= 3 ~ "Some HS", | |
| education == 4 ~ "HS Grad", | |
| education == 5 ~ "Trade\nSchool", | |
| education == 6 ~ "Some\nColl.", | |
| education == 7 ~ "2 Yr.", | |
| education == 8 ~ "4 Yr.", | |
| education == 9 ~ "Some\nGrad.", | |
| education == 10 ~ "Masters", | |
| education == 11 ~ "Doctorate")) %>% | |
| mutate(none = case_when(religion == 10 | religion == 11 | religion == 12 ~ 1, TRUE ~ 0)) %>% | |
| group_by(educ) %>% | |
| mean_ci(none, wt = weight, ci = .84) | |
| gg %>% | |
| mutate(lab = round(mean, 2)) %>% | |
| ggplot(., aes(x = educ, y = mean, fill = educ)) + | |
| geom_col(color = 'black') + | |
| theme_rb() + | |
| y_pct() + | |
| lab_bar(top = FALSE, type = lab, pos= .015, sz = 10) + | |
| geom_text(aes(y = .015, label = ifelse(educ == "Doctorate" | educ == "Masters", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 10, family = "font", color = "white") + | |
| error_bar() + | |
| scale_fill_manual(values=met.brewer("Cross", 9)) + | |
| theme(axis.text = element_text(size = 17)) + | |
| labs(x = "Education Level", y = "", title = "Share Who Are Nones by Educational Level", caption = "@ryanburge\nData: Nationscape 2019-2021") | |
| save("ns_ed_nones23.png") | |
| cces_educ <- function(df, var){ | |
| df %>% | |
| mutate(educ = frcode({{var}} == 1 ~ "No HS", | |
| {{var}} == 2 ~ "HS\nGrad", | |
| {{var}} == 3 ~ "Some\nColl.", | |
| {{var}} == 4 ~ "2 Yr.", | |
| {{var}} == 5 ~ "4 Yr.", | |
| {{var}} == 6 ~ "Post-\nGrad")) | |
| } | |
| graph <- cces %>% | |
| filter(year >= 2008) %>% | |
| cces_educ(educ) %>% | |
| mutate(never = case_when(pew_attendance == 1 | pew_attendance == 2 ~ 1, | |
| TRUE ~ 0)) %>% | |
| group_by(educ, year) %>% | |
| mean_ci(never, wt = weight, ci = .84) | |
| graph %>% | |
| mutate(lab = round(mean, 2)) %>% | |
| ggplot(., aes(x = educ, y = mean, group = factor(year), fill = factor(year))) + | |
| geom_col(color = "black") + | |
| facet_wrap(~ year) + | |
| theme_rb() + | |
| geom_smooth(se = FALSE, method = lm, color = "black", linetype = "twodash", linewidth = .5) + | |
| scale_fill_manual(values = c(met.brewer("Homer1", 15))) + | |
| lab_bar(top = FALSE, type = lab, pos = .053, sz = 4) + | |
| geom_text(aes(y = .053, label = ifelse(year <= 2010, paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 4, family = "font", color = "white") + | |
| y_pct() + | |
| error_bar() + | |
| labs(x = "Highest Level of Education", y = "", title = "Share Who Attend Religious Services At Least Once a Week", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022") | |
| save("educ_nevers_ces22.png", ht = 9) | |
| cces_inc <- function(df, var){ | |
| df %>% | |
| mutate(inc = frcode({{var}} == 1 ~ "<$10k", | |
| {{var}} == 2 ~ "$10k-\n$20k", | |
| {{var}} == 3 ~ "$20k-\n$30k", | |
| {{var}} == 4 ~ "$30k-\n$40k", | |
| {{var}} == 5 ~ "$40k-\n$50k", | |
| {{var}} == 6 ~ "$50k-\n$60k", | |
| {{var}} == 7 ~ "$60k-\n$70k", | |
| {{var}} == 8 ~ "$70k-\n$80k", | |
| {{var}} == 9 ~ "$80k-\n$100k", | |
| {{var}} == 10 ~ "$100k-\n$120k", | |
| {{var}} == 11 ~ "$120k-\n$150k", | |
| {{var}} == 12 ~ "$150k-\n$200k", | |
| {{var}} == 13 | {{var}} == 14 | {{var}} == 15 ~ "$200k+")) | |
| } | |
| graph <- cces %>% | |
| cces_inc(income) %>% | |
| filter(year == 2008 | year == 2012 | year == 2016 | year == 2020 | year == 2022) %>% | |
| filter(inc != "NA") %>% | |
| mutate(ed2 = frcode(educ == 1 | educ == 2 ~ "HS or Less", | |
| educ == 5 | educ == 6 ~ "College Degree")) %>% | |
| filter(ed2 != "NA") %>% | |
| mutate(never = case_when(pew_attendance == 1 | pew_attendance == 2 ~ 1, | |
| TRUE ~ 0)) %>% | |
| group_by(inc, ed2, year) %>% | |
| mean_ci(never, wt = weight, ci = .84) %>% filter(n > 100) | |
| every_nth = function(n) { | |
| return(function(x) {x[c(TRUE, rep(FALSE, n - 1))]}) | |
| } | |
| graph %>% | |
| ggplot(., aes(x = inc, y = mean, color = ed2, group = ed2)) + | |
| geom_point(stroke = .5, shape = 21, alpha = .45, show.legend = FALSE) + | |
| geom_smooth(se = FALSE) + | |
| facet_wrap(~ year) + | |
| y_pct() + | |
| theme_rb() + | |
| scale_color_calc() + | |
| scale_x_discrete(breaks = every_nth(n = 3)) + | |
| guides(color = guide_legend(reverse = TRUE)) + | |
| theme(legend.position = c(.85, .15)) + | |
| labs(x= "Household Income", y = "", title = "Share Attending Weekly by Education and Income Level", caption = "@ryanburge\nData: Cooperative Election Study 2008-2022") | |
| save("inc_educ_wk_att.png") | |
| graph <- cces %>% | |
| filter(year >= 2020) %>% | |
| mutate(mar = frcode(marital_status == 1 ~ "Married", | |
| marital_status == 2 | marital_status == 3 ~ "Separated/Divorced", | |
| marital_status == 5 ~ "Never Married")) %>% | |
| group_by(age, mar) %>% | |
| mutate(never = case_when(pew_attendance == 1 | pew_attendance == 2 ~ 1, | |
| TRUE ~ 0)) %>% | |
| mean_ci(never, wt = weight) %>% | |
| filter(mar != 'NA') | |
| graph %>% | |
| filter(mean <= .50) %>% | |
| filter(age <= 70) %>% | |
| ggplot(., aes(x = age, y= mean, color = mar, group = mar)) + | |
| geom_point(stroke = .5, shape = 21, alpha = .45, show.legend = FALSE) + | |
| geom_smooth(se = FALSE) + | |
| scale_color_calc() + | |
| y_pct() + | |
| theme_rb(legend = TRUE) + | |
| labs(x = "Age", y = "", title = "Share Attending Religious Services Weekly Based on Marital Status", caption = "@ryanburge\nData: Cooperative Election Study 2020-2022") | |
| save("mar_status_wk.png") | |
| graph <- cces %>% | |
| # filter(age >= 30 & age <= 50) %>% | |
| filter(year >= 2020) %>% | |
| mutate(mar = frcode(marital_status == 1 ~ "Married", | |
| marital_status == 2 | marital_status == 3 | marital_status == 5 ~ "Not Married")) %>% | |
| mutate(kids = frcode(havekids == 1 ~ "Parent", | |
| TRUE ~ "Not a Parent")) %>% | |
| mutate(never = case_when(pew_attendance == 1 | pew_attendance == 2 ~ 1, | |
| TRUE ~ 0)) %>% | |
| group_by(mar, kids, age) %>% | |
| mean_ci(never, wt = weight) %>% filter(mar != "NA") | |
| graph <- graph %>% | |
| mutate(comb = paste(mar, kids, sep = "+")) | |
| graph %>% | |
| filter(age <= 50) %>% | |
| ggplot(., aes(x = age, y= mean, color = comb, group = comb)) + | |
| geom_point(stroke = .5, shape = 21, alpha = .45, show.legend = FALSE) + | |
| geom_smooth(se = FALSE) + | |
| scale_color_calc() + | |
| y_pct() + | |
| theme_rb(legend = TRUE) + | |
| labs(x = "Age", y = "", title = "Share Attending Religious Services Weekly Based on Marital and Parental Status", caption = "@ryanburge\nData: Cooperative Election Study 2020-2022") | |
| save("mar_status_parents_wk.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment