Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created August 30, 2024 13:38
Show Gist options
  • Save ryanburge/ee35be6570281f59e57c262aaa32c98e to your computer and use it in GitHub Desktop.
Save ryanburge/ee35be6570281f59e57c262aaa32c98e to your computer and use it in GitHub Desktop.
one <- gss %>%
filter(year >= 1990) %>%
filter(year <= 2020) %>%
mutate(decades = frcode(year >= 1970 & year <= 1979 ~ "1970s",
year >= 1980 & year <= 1989 ~ "1980s",
year >= 1990 & year <= 1999 ~ "1990s",
year >= 2000 & year <= 2009 ~ "2000s",
year >= 2010 & year <= 2019 ~ "2010s",
year >= 2020 ~ "2020s")) %>%
mutate(gd = frcode(god == 1 ~ "Don't Believe",
god == 2 ~ "No Way to Find Out",
god == 3 ~ "Some Higher Power",
god == 4 ~ "Believe Sometimes",
god == 5 ~ "Believe But Doubts",
god == 6 ~ "Believe No Doubts")) %>%
mutate(pid7 = partyid + 1) %>%
cces_pid3(pid7) %>%
filter(pid3 == "Republican" | pid3 == "Democrat") %>%
group_by(decades, pid3) %>%
ct(gd, wt = wtssall, show_na = FALSE)
two <- gss %>%
filter(year >= 2020) %>%
mutate(decades = frcode(year >= 1970 & year <= 1979 ~ "1970s",
year >= 1980 & year <= 1989 ~ "1980s",
year >= 1990 & year <= 1999 ~ "1990s",
year >= 2000 & year <= 2009 ~ "2000s",
year >= 2010 & year <= 2019 ~ "2010s",
year >= 2020 ~ "2020s")) %>%
mutate(gd = frcode(god == 1 ~ "Don't Believe",
god == 2 ~ "No Way to Find Out",
god == 3 ~ "Some Higher Power",
god == 4 ~ "Believe Sometimes",
god == 5 ~ "Believe But Doubts",
god == 6 ~ "Believe No Doubts")) %>%
mutate(pid7 = partyid + 1) %>%
cces_pid3(pid7) %>%
filter(pid3 == "Republican" | pid3 == "Democrat") %>%
group_by(decades, pid3) %>%
ct(gd, wt = wtssnrps, show_na = FALSE)
belief <- bind_rows(one, two)
belief %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(pid3), y = pct, fill = fct_rev(gd))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ decades, ncol =1, strip.position = "top") +
theme_rb() +
scale_fill_manual(values = c(moma.colors("Connors", 6))) +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0)) +
guides(fill = guide_legend(reverse=T, nrow = 1)) +
# theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) +
theme(panel.grid.minor.y=element_blank(), panel.grid.major.y=element_blank()) +
geom_text(aes(label = ifelse(gd == "Believe No Doubts" & pct > .05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 9, family = "font", color = "white") +
geom_text(aes(label = ifelse(gd != "Believe No Doubts" & pct > .05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 9, family = "font", color = "black") +
# geom_text(aes(label = ifelse(age2 == "36-44", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
theme(plot.title = element_text(size = 24)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
theme(strip.text.x.top = element_text(size = 20)) +
theme(legend.text = element_text(size = 10)) +
labs(x = "", y = "", title = "What Do You Believe About God", caption = "@ryanburge\nData: General Social Survey, 1990-2022")
save("pid2_belief_god.png", wd = 9, ht = 8)
one <- gss %>%
filter(year <= 2020) %>%
mutate(decades = frcode(year >= 1970 & year <= 1979 ~ "1970s",
year >= 1980 & year <= 1989 ~ "1980s",
year >= 1990 & year <= 1999 ~ "1990s",
year >= 2000 & year <= 2009 ~ "2000s",
year >= 2010 & year <= 2019 ~ "2010s",
year >= 2020 ~ "2020s")) %>%
gss_attend(attend) %>%
mutate(pid7 = partyid + 1) %>%
cces_pid3(pid7) %>%
filter(pid3 == "Republican" | pid3 == "Democrat") %>%
group_by(decades, pid3) %>%
ct(att, wt = wtssall, show_na = FALSE)
two <- gss %>%
filter(year >= 2020) %>%
mutate(decades = frcode(year >= 1970 & year <= 1979 ~ "1970s",
year >= 1980 & year <= 1989 ~ "1980s",
year >= 1990 & year <= 1999 ~ "1990s",
year >= 2000 & year <= 2009 ~ "2000s",
year >= 2010 & year <= 2019 ~ "2010s",
year >= 2020 ~ "2020s")) %>%
gss_attend(attend) %>%
mutate(pid7 = partyid + 1) %>%
cces_pid3(pid7) %>%
filter(pid3 == "Republican" | pid3 == "Democrat") %>%
group_by(decades, pid3) %>%
ct(att, wt = wtssnrps, show_na = FALSE)
attend <- bind_rows(one, two)
attend %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(pid3), y = pct, fill = fct_rev(att))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ decades, ncol =1, strip.position = "top") +
theme_rb() +
scale_fill_manual(values = c(moma.colors("ustwo", 5))) +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0)) +
guides(fill = guide_legend(reverse=T, nrow = 1)) +
# theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) +
theme(panel.grid.minor.y=element_blank(), panel.grid.major.y=element_blank()) +
geom_text(aes(label = ifelse(pct > .05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 9, family = "font", color = "black") +
# geom_text(aes(label = ifelse(gd != "Believe No Doubts" & pct > .05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 9, family = "font", color = "black") +
# geom_text(aes(label = ifelse(age2 == "36-44", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
theme(plot.title = element_text(size = 24)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
theme(strip.text.x.top = element_text(size = 20)) +
theme(legend.text = element_text(size = 10)) +
labs(x = "", y = "", title = "How Often Do You Attend Religious Services?", caption = "@ryanburge\nData: General Social Survey, 1972-2022")
save("pid2_attend_gss.png", wd = 9, ht = 10)
one <- gss %>%
filter(year <= 2020) %>%
mutate(decades = frcode(year >= 1970 & year <= 1979 ~ "1970s",
year >= 1980 & year <= 1989 ~ "1980s",
year >= 1990 & year <= 1999 ~ "1990s",
year >= 2000 & year <= 2009 ~ "2000s",
year >= 2010 & year <= 2019 ~ "2010s",
year >= 2020 ~ "2020s")) %>%
mutate(rel = frcode(relig == 1 ~ "Protestant",
relig == 2 ~ "Catholic",
relig == 4 ~ "No Religion",
TRUE ~ "Something Else")) %>%
mutate(pid7 = partyid + 1) %>%
cces_pid3(pid7) %>%
filter(pid3 == "Republican" | pid3 == "Democrat") %>%
group_by(decades, pid3) %>%
ct(rel, wt = wtssall, show_na = FALSE)
two <- gss %>%
filter(year >= 2020) %>%
mutate(decades = frcode(year >= 1970 & year <= 1979 ~ "1970s",
year >= 1980 & year <= 1989 ~ "1980s",
year >= 1990 & year <= 1999 ~ "1990s",
year >= 2000 & year <= 2009 ~ "2000s",
year >= 2010 & year <= 2019 ~ "2010s",
year >= 2020 ~ "2020s")) %>%
mutate(rel = frcode(relig == 1 ~ "Protestant",
relig == 2 ~ "Catholic",
relig == 4 ~ "No Religion",
TRUE ~ "Something Else")) %>%
mutate(pid7 = partyid + 1) %>%
cces_pid3(pid7) %>%
filter(pid3 == "Republican" | pid3 == "Democrat") %>%
group_by(decades, pid3) %>%
ct(rel, wt = wtssnrps, show_na = FALSE)
belong <- bind_rows(one, two)
belong %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(pid3), y = pct, fill = fct_rev(rel))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ decades, ncol =1, strip.position = "top") +
theme_rb() +
scale_fill_manual(values = c(moma.colors("Budnitz", 4))) +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0)) +
guides(fill = guide_legend(reverse=T, nrow = 1)) +
# theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) +
theme(panel.grid.minor.y=element_blank(), panel.grid.major.y=element_blank()) +
geom_text(aes(label = ifelse(pct > .05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 9, family = "font", color = "black") +
# geom_text(aes(label = ifelse(gd != "Believe No Doubts" & pct > .05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 9, family = "font", color = "black") +
# geom_text(aes(label = ifelse(age2 == "36-44", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
theme(plot.title = element_text(size = 24)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
theme(strip.text.x.top = element_text(size = 20)) +
theme(legend.text = element_text(size = 10)) +
labs(x = "", y = "", title = "What is Your Present Religion", caption = "@ryanburge\nData: General Social Survey, 1972-2022")
save("pid2_belonging_gss.png", wd = 9, ht = 10)
gg1 <- gss %>%
filter(partyid == 0 | partyid == 1 | partyid == 2) %>%
filter(year <= 2018) %>%
mutate(belief = case_when(god == 1 | god == 2 ~ 1,
god == 3 | god == 4 | god == 5 | god == 6 ~ 0)) %>%
group_by(year) %>%
mean_ci(belief, wt = wtssall, ci = .84) %>%
na.omit()
gg2 <- gss %>%
filter(partyid == 0 | partyid == 1 | partyid == 2) %>%
filter(year > 2018) %>%
mutate(belief = case_when(god == 1 | god == 2 ~ 1,
god == 3 | god == 4 | god == 5 | god == 6 ~ 0)) %>%
group_by(year) %>%
mean_ci(belief, wt = wtssnrps, ci = .84) %>%
na.omit()
bel <- bind_rows(gg1, gg2) %>% mutate(type = "Atheist/Agnostic Belief")
gg1 <- gss %>%
filter(partyid == 0 | partyid == 1 | partyid == 2) %>%
filter(year <= 2018) %>%
mutate(never = case_when(attend == 0 ~ 1,
attend <= 8 ~ 0)) %>%
group_by(year) %>%
mean_ci(never, wt = wtssall, ci = .84) %>%
na.omit()
gg2 <- gss %>%
filter(year > 2018) %>%
filter(partyid == 0 | partyid == 1 | partyid == 2) %>%
mutate(never = case_when(attend == 0 ~ 1,
attend <= 8 ~ 0)) %>%
group_by(year) %>%
mean_ci(never, wt = wtssnrps, ci = .84) %>%
na.omit()
att <- bind_rows(gg1, gg2) %>% mutate(type = "Never Attend")
gg1 <- gss %>%
filter(partyid == 0 | partyid == 1 | partyid == 2) %>%
filter(year <= 2018) %>%
mutate(none = case_when(relig == 4 ~ 1,
TRUE ~ 0)) %>%
group_by(year) %>%
mean_ci(none, wt = wtssall, ci = .84) %>%
na.omit()
gg2 <- gss %>%
filter(partyid == 0 | partyid == 1 | partyid == 2) %>%
filter(year > 2018) %>%
mutate(none = case_when(relig == 4 ~ 1,
TRUE ~ 0)) %>%
group_by(year) %>%
mean_ci(none, wt = wtssnrps, ci = .84) %>%
na.omit()
belong <- bind_rows(gg1, gg2)%>% mutate(type = "No Religious Affiliation")
dem <- bind_rows(bel, att, belong) %>%
mutate(pid2 = "Democrats")
gg1 <- gss %>%
filter(partyid == 4 | partyid == 5 | partyid == 6) %>%
filter(year <= 2018) %>%
mutate(belief = case_when(god == 1 | god == 2 ~ 1,
god == 3 | god == 4 | god == 5 | god == 6 ~ 0)) %>%
group_by(year) %>%
mean_ci(belief, wt = wtssall, ci = .84) %>%
na.omit()
gg2 <- gss %>%
filter(partyid == 4 | partyid == 5 | partyid == 6) %>%
filter(year > 2018) %>%
mutate(belief = case_when(god == 1 | god == 2 ~ 1,
god == 3 | god == 4 | god == 5 | god == 6 ~ 0)) %>%
group_by(year) %>%
mean_ci(belief, wt = wtssnrps, ci = .84) %>%
na.omit()
bel <- bind_rows(gg1, gg2) %>% mutate(type = "Atheist/Agnostic Belief")
gg1 <- gss %>%
filter(partyid == 4 | partyid == 5 | partyid == 6) %>%
filter(year <= 2018) %>%
mutate(never = case_when(attend == 0 ~ 1,
attend <= 8 ~ 0)) %>%
group_by(year) %>%
mean_ci(never, wt = wtssall, ci = .84) %>%
na.omit()
gg2 <- gss %>%
filter(year > 2018) %>%
filter(partyid == 4 | partyid == 5 | partyid == 6) %>%
mutate(never = case_when(attend == 0 ~ 1,
attend <= 8 ~ 0)) %>%
group_by(year) %>%
mean_ci(never, wt = wtssnrps, ci = .84) %>%
na.omit()
att <- bind_rows(gg1, gg2) %>% mutate(type = "Never Attend")
gg1 <- gss %>%
filter(partyid == 4 | partyid == 5 | partyid == 6) %>%
filter(year <= 2018) %>%
mutate(none = case_when(relig == 4 ~ 1,
TRUE ~ 0)) %>%
group_by(year) %>%
mean_ci(none, wt = wtssall, ci = .84) %>%
na.omit()
gg2 <- gss %>%
filter(partyid == 4 | partyid == 5 | partyid == 6) %>%
filter(year > 2018) %>%
mutate(none = case_when(relig == 4 ~ 1,
TRUE ~ 0)) %>%
group_by(year) %>%
mean_ci(none, wt = wtssnrps, ci = .84) %>%
na.omit()
belong <- bind_rows(gg1, gg2)%>% mutate(type = "No Religious Affiliation")
rep <- bind_rows(bel, att, belong) %>%
mutate(pid2 = "Republicans")
both <- bind_rows(dem, rep)
both %>%
ggplot(., aes(x = year, y = mean, color = pid2, group = pid2)) +
geom_point(stroke = .25, shape = 21) +
geom_smooth(se = FALSE) +
facet_wrap(~ type) +
scale_color_manual(values = c("dodgerblue3", 'firebrick3')) +
scale_y_continuous(labels = percent, limits = c(0, .35)) +
theme_rb(legend = TRUE) +
theme(strip.text = element_text(size = 20)) +
labs(x = "", y = "", title = "Behavior, Belief, and Belonging in Democrats versus Republicans", caption = "@ryanburge\nData: General Social Survey, 1972-2022")
save("three_b_two_pid_new22.png")
gg1 <- gss %>%
filter(year <= 2018) %>%
filter(year != 1982) %>%
filter(year != 1987) %>%
mutate(pid7 = partyid + 1) %>%
cces_pid3(pid7) %>%
mutate(white = case_when(race == 1 ~ 1, TRUE ~ 0)) %>%
mutate(xtn = case_when(relig == 1 | relig == 2 | relig == 10 | relig == 11 ~ 1,
TRUE ~0)) %>%
mutate(both = xtn + white) %>%
mutate(both = case_when(both == 2 ~ 1,
both <= 1 ~ 0)) %>%
group_by(pid3, year) %>%
mean_ci(both, wt = wtssall)
gg2 <- gss %>%
filter(year == 1982 | year == 1987) %>%
mutate(pid7 = partyid + 1) %>%
cces_pid3(pid7) %>%
mutate(white = case_when(race == 1 ~ 1, TRUE ~ 0)) %>%
mutate(xtn = case_when(relig == 1 | relig == 2 | relig == 10 | relig == 11 ~ 1,
TRUE ~0)) %>%
mutate(both = xtn + white) %>%
mutate(both = case_when(both == 2 ~ 1,
both <= 1 ~ 0)) %>%
group_by(pid3, year) %>%
mean_ci(both, wt = oversamp)
gg3 <- gss %>%
filter(year >= 2020) %>%
mutate(pid7 = partyid + 1) %>%
cces_pid3(pid7) %>%
mutate(white = case_when(race == 1 ~ 1, TRUE ~ 0)) %>%
mutate(xtn = case_when(relig == 1 | relig == 2 | relig == 10 | relig == 11 ~ 1,
TRUE ~0)) %>%
mutate(both = xtn + white) %>%
mutate(both = case_when(both == 2 ~ 1,
both <= 1 ~ 0)) %>%
group_by(pid3, year) %>%
mean_ci(both, wt = wtssnrps)
both <- bind_rows(gg1, gg2, gg3) %>% filter(pid3 != "NA")
both %>%
ggplot(., aes(x = year, y = mean, color = pid3, group = pid3)) +
geom_point(stroke = .5, shape = 21) +
geom_labelsmooth(aes(label = pid3), method = "loess", formula = y ~ x, family = "font", linewidth = 1, text_smoothing = 30, size = 6, linewidth = 1, boxlinewidth = 0.3, hjust = .75) +
pid3_color() +
theme_rb() +
y_pct() +
add_text(x = 1972, y = .935, word = "91%", sz = 6) +
add_text(x = 1972, y = .79, word = "77%", sz = 6) +
add_text(x = 1972, y = .68, word = "71%", sz = 6) +
add_text(x = 2022, y = .31, word = "33%", sz = 6) +
add_text(x = 2022, y = .40, word = "38%", sz = 6) +
add_text(x = 2022, y = .75, word = "73%", sz = 6) +
labs(x = "Year", y = "", title = "Share of Each Party That is White and Christian", caption = "@ryanburge\nData: General Social Survey, 1972-2022")
save("gss_pid3_wxtn22.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment