Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created December 8, 2024 22:53
Show Gist options
  • Select an option

  • Save ryanburge/2b923f9712082b2b7c4908523db14245 to your computer and use it in GitHub Desktop.

Select an option

Save ryanburge/2b923f9712082b2b7c4908523db14245 to your computer and use it in GitHub Desktop.
fun <- function(df, var, type){
df %>%
mutate(con = frcode({{var}} == 1 ~ "Great Deal",
{{var}} == 2 ~ "Only Some",
{{var}} == 3 ~ "Hardly Any")) %>%
group_by(year) %>%
ct(con, wt = wtssall,show_na = FALSE) %>%
mutate(name = type) %>%
na.omit()
}
yyy3 <- gss %>% fun(conclerg, "Organized Religion")
yyy4 <- gss %>%
filter(year > 2018) %>%
mutate(con = frcode(conclerg == 1 ~ "Great Deal",
conclerg == 2 ~ "Only Some",
conclerg == 3 ~ "Hardly Any")) %>%
group_by(year) %>%
ct(con, wt = wtssnrps, show_na = FALSE) %>%
mutate(name = "Organized Religion") %>%
na.omit()
all <- bind_rows(yyy3, yyy4)
all %>%
ggplot(., aes(x = year, y = pct, color = con, group = con)) +
geom_point(stroke = 1, shape = 21, fill = "white") +
scale_y_continuous(labels = percent, limits = c(0, .60)) +
scale_color_fivethirtyeight() +
geom_labelsmooth(aes(label = con), method = "loess", formula = y ~ x, family = "font", linewidth = 1, text_smoothing = 30, size = 6, linewidth = 1, boxlinewidth = 0.3, hjust = .20) +
theme_rb() +
labs(x = "", y = "", title = "How Much Confidence Do You Have in Organized Religion?",
caption = "@ryanburge + @religiondata\nData: General Social Survey, 1972-2022")
save("conf_relig_gss22.png")
gg1 <- gss %>%
filter(year <= 2018) %>%
mutate(birthyr = year - age) %>%
mutate(cohorts = frcode(birthyr >= 1940 & birthyr <= 1944 ~ "1940-1944",
birthyr >= 1945 & birthyr <= 1949 ~ "1945-1949",
birthyr >= 1950 & birthyr <= 1954 ~ "1950-1954",
birthyr >= 1955 & birthyr <= 1959 ~ "1955-1959",
birthyr >= 1960 & birthyr <= 1964 ~ "1960-1964",
birthyr >= 1965 & birthyr <= 1969 ~ "1965-1969",
birthyr >= 1970 & birthyr <= 1974 ~ "1970-1974",
birthyr >= 1975 & birthyr <= 1979 ~ "1975-1979",
birthyr >= 1980 & birthyr <= 1984 ~ "1980-1984",
birthyr >= 1985 & birthyr <= 1989 ~ "1985-1989",
birthyr >= 1990 & birthyr <= 1994 ~ "1990-1994",
birthyr >= 1995 & birthyr <= 2000 ~ "1995-2000")) %>%
mutate(con = frcode(conclerg == 1 ~ "Great Deal",
conclerg == 2 ~ "Only Some",
conclerg == 3 ~ "Hardly Any")) %>%
group_by(cohorts, year) %>%
ct(con, wt = wtssall, show_na = FALSE)
gg2 <- gss %>%
filter(year > 2018) %>%
mutate(birthyr = year - age) %>%
mutate(cohorts = frcode(birthyr >= 1940 & birthyr <= 1944 ~ "1940-1944",
birthyr >= 1945 & birthyr <= 1949 ~ "1945-1949",
birthyr >= 1950 & birthyr <= 1954 ~ "1950-1954",
birthyr >= 1955 & birthyr <= 1959 ~ "1955-1959",
birthyr >= 1960 & birthyr <= 1964 ~ "1960-1964",
birthyr >= 1965 & birthyr <= 1969 ~ "1965-1969",
birthyr >= 1970 & birthyr <= 1974 ~ "1970-1974",
birthyr >= 1975 & birthyr <= 1979 ~ "1975-1979",
birthyr >= 1980 & birthyr <= 1984 ~ "1980-1984",
birthyr >= 1985 & birthyr <= 1989 ~ "1985-1989",
birthyr >= 1990 & birthyr <= 1994 ~ "1990-1994",
birthyr >= 1995 & birthyr <= 2000 ~ "1995-2000")) %>%
mutate(con = frcode(conclerg == 1 ~ "Great Deal",
conclerg == 2 ~ "Only Some",
conclerg == 3 ~ "Hardly Any")) %>%
group_by(cohorts, year) %>%
ct(con, wt = wtssnrps, show_na = FALSE)
both <- bind_rows(gg1, gg2)
both %>%
filter(cohorts != "NA") %>%
ggplot(., aes(x = year, y = pct, color = con, group = con)) +
geom_point(stroke = .15, shape = 21, alpha = 1) +
geom_smooth(se = FALSE) +
facet_wrap(~ cohorts) +
scale_color_fivethirtyeight() +
theme_rb(legend = TRUE) +
y_pct() +
labs(x = "", y = "", title = "How Much Confidence Do You Have in Organized Religion?",
caption = "@ryanburge + @religiondata\nData: General Social Survey, 1972-2022")
save("conf_relig_cohorts22.png", ht = 9)
gg1 <- gss %>%
gss_reltrad6(reltrad) %>%
mutate(con = frcode(conclerg == 1 ~ "Great Deal",
conclerg == 2 ~ "Only Some",
conclerg == 3 ~ "Hardly Any")) %>%
group_by(year, reltrad) %>%
ct(con, wt = wtssall, show_na = FALSE)
gg2 <- gss %>%
gss_reltrad6(reltrad) %>%
mutate(con = frcode(conclerg == 1 ~ "Great Deal",
conclerg == 2 ~ "Only Some",
conclerg == 3 ~ "Hardly Any")) %>%
group_by(year, reltrad) %>%
ct(con, wt = wtssnrps, show_na = FALSE)
all <- bind_rows(gg1, gg2)
all %>%
filter(reltrad != "NA") %>%
ggplot(., aes(x = year, y = pct, color = con, group = con)) +
geom_point(stroke = .15, shape = 21, alpha = 1) +
geom_smooth(se = FALSE) +
facet_wrap(~ reltrad) +
scale_color_fivethirtyeight() +
theme_rb(legend = TRUE) +
y_pct() +
theme(strip.text = element_text(size = 17)) +
labs(x = "", y = "", title = "How Much Confidence Do You Have in Organized Religion?",
caption = "@ryanburge + @religiondata\nData: General Social Survey, 1972-2022")
save("conf_relig_reltrad6_22.png", ht = 7)
gg <- all %>%
filter(year == 2022) %>%
filter(n > 0) %>%
filter(con == "Great Deal") %>%
na.omit()
gg %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = reorder(reltrad, pct), y = pct, fill = reltrad)) +
geom_col(color = "black") +
coord_flip() +
theme_rb() +
scale_fill_manual(values = c("#1b9e77", "#d95f02", "#7570b3", "#e7298a", "#66a61e", "#e6ab02")) +
geom_text(aes(y = pct + .02, label = ifelse(pct > .10, paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 8, family = "font") +
geom_text(aes(y = pct + .015, label = ifelse(pct < .10, paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 8, family = "font") +
scale_y_continuous(labels = percent) +
labs(x = "", y = "", title = "Share of Each Tradition with A Great Deal of Trust in Organized Religion in 2022",
caption = "@ryanburge + @religiondata\nData: General Social Survey, 2022")
save("great_deal_conf_reltrad.png", ht = 4)
fun <- function(df, var, type){
aa1 <- df %>%
filter(year <= 2018) %>%
mutate(con = case_when({{var}} == 1 ~ 1,
{{var}} == 2 | {{var}} == 3 ~ 0)) %>%
group_by(year) %>%
mean_ci(con, wt = wtssall, ci = .84) %>%
mutate(name = type) %>%
na.omit()
aa2 <- df %>%
filter(year == 2021 | year == 2022) %>%
mutate(con = case_when({{var}} == 1 ~ 1,
{{var}} == 2 | {{var}} == 3 ~ 0)) %>%
group_by(year) %>%
mean_ci(con, wt = wtssnrps, ci = .84) %>%
mutate(name = type) %>%
na.omit()
bind_rows(aa1, aa2)
}
yyy1 <- gss %>% fun(confinan, "Banks")
yyy2 <- gss %>% fun(conbus, "Major Companies")
yyy4 <- gss %>% fun(coneduc, "Education")
yyy5 <- gss %>% fun(confed, "Executive Branch")
yyy6 <- gss %>% fun(conlabor, "Organized Labor")
yyy7 <- gss %>% fun(conpress, "Press")
yyy8 <- gss %>% fun(conmedic, "Medicine")
yyy9 <- gss %>% fun(conjudge, "SCOTUS")
yyy10 <- gss %>% fun(contv, "Television")
yyy11 <- gss %>% fun(consci, "Scientific Community")
yyy12 <- gss %>% fun(conlegis, "Congress")
yyy13 <- gss %>% fun(conarmy, "Military")
all_data <- bind_rows(yyy1, yyy2, yyy4, yyy5, yyy6, yyy7, yyy8, yyy9, yyy10, yyy11, yyy12, yyy13)
# Calculate the average institutional trust score over time
all_trust <- all_data %>%
group_by(year) %>%
summarise(
trust = mean(mean, na.rm = TRUE),
lower = mean(lower, na.rm = TRUE), # Average of lower confidence intervals
upper = mean(upper, na.rm = TRUE) # Average of upper confidence intervals
) %>%
mutate(type = "All Other Institutions")
rel <- gss %>% fun(conclerg, "Organized Religion") %>%
select(year, trust = mean, lower, upper, type = name)
both <- bind_rows(all_trust, rel)
both %>%
ggplot(., aes(x = year, y = trust, color = type, group = type)) +
geom_line() +
geom_point(stroke = .75, shape = 21, fill = "white") +
theme_rb() +
scale_y_continuous(labels = percent, limits = c(0, .47)) +
scale_color_calc() +
theme(legend.position = c(.75, .75)) +
theme(legend.text = element_text(size = 18)) +
labs(x = "", y = "% With Great Deal of Confidence", title = "Do People Distrust Organized Religion More Than Other Institutions?",
caption = "@ryanburge + @religiondata\nData: General Socail Survey, 1972-2022")
save("rel_trust_compare_to_all22.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment