Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created January 28, 2025 14:17
Show Gist options
  • Save ryanburge/3ea74fee7adc0b7f40da193c10ac02f6 to your computer and use it in GitHub Desktop.
Save ryanburge/3ea74fee7adc0b7f40da193c10ac02f6 to your computer and use it in GitHub Desktop.
gg <- cces %>%
filter(year >= 2021) %>%
group_by(year) %>%
mutate(gender = frcode(gender == 1 ~ "Male",
gender == 2 ~ "Female",
gender == 3 ~ "Non-\nbinary",
gender == 4 ~ "Other")) %>%
ct(gender, wt = weight, show_na = FALSE) %>%
filter(gender == "Non-\nbinary" | gender == "Other")
gg %>%
ggplot(., aes(x = gender, y = pct, fill = gender)) +
geom_col(color = "black") +
theme_rb() +
facet_wrap(~ year) +
geom_text(aes(y = pct + .0003, label = paste0(pct*100, '%')), position = position_dodge(width = .9), size = 6, family = "font") +
scale_fill_calc() +
scale_y_continuous(labels = percent) +
labs(x = "", y = "", title = "Share of the Sample That Does Not Identify\nas Male or Female",
caption = "@ryanburge\nData: Cooperative Election Study, 2021-2023")
save("non_bin_share_ces.png", wd = 5, ht = 8)
gg <- cces %>%
filter(year >= 2021) %>%
mutate(trans = case_when(gender == 3 | gender == 4 ~ 1,
gender == 1 | gender == 2 ~ 0)) %>%
group_by(birthyr) %>%
mean_ci(trans, wt = weight, ci = .84)
gg %>%
ggplot(., aes(x = birthyr, y = mean)) +
geom_point(stroke = 1, shape = 21, fill = "white", color = "azure4") +
geom_smooth(se = FALSE, color = "red") +
theme_rb() +
scale_y_continuous(labels = percent) +
labs(x = "Birth Year of Respondent", y = "", title = "Share Who Identify as Non-binary or Other by Birth Year",
caption = "@ryanburge\nData: Cooperative Election Study, 2021-2023")
save("trans_birthyear.png")
gg <- cces %>%
filter(year >= 2021) %>%
mutate(trans = case_when(gender == 3 | gender == 4 ~ 1,
gender == 1 | gender == 2 ~ 0)) %>%
mutate(gen = frcode(birthyr>= 1925 & birthyr <= 1945 ~ 'Silent',
birthyr>= 1946 & birthyr <= 1964 ~ 'Boomers',
birthyr>= 1965 & birthyr <= 1979 ~ 'Gen X',
birthyr>= 1980 & birthyr <= 1995 ~ 'Millennials',
birthyr>= 1996 & birthyr <= 2019 ~ 'Gen Z')) %>%
group_by(gen) %>%
mean_ci(trans, wt = weight)
gg %>%
ggplot(., aes(x = gen, y = mean, fill = gen)) +
geom_col(color = "black") +
theme_rb() +
geom_text(aes(y = mean + .0015, label = paste0(mean*100, '%')), position = position_dodge(width = .9), size = 9, family = "font") +
scale_fill_manual(values = met.brewer("Signac", 5)) +
scale_y_continuous(labels = percent) +
theme(axis.text.x = element_text(size = 20)) +
labs(x = "", y = "", title = "Share Who Identify as Non-binary or Other by Generation",
caption = "@ryanburge\nData: Cooperative Election Study, 2021-2023")
save("trans_by_gen.png", wd = 6)
gg1 <- cces %>%
filter(year >= 2021) %>%
filter(gender == 1 | gender == 2) %>%
mutate(relig = frcode(religion == 1 ~ "Protestant",
religion == 2 ~ "Catholic",
religion >= 3 & religion <= 8 ~ "Other World Religion",
religion == 9 ~ "Atheist",
religion == 10 ~ "Agnostic",
religion == 11 ~ "Nothing in Particular",
religion == 12 ~ "Something Else")) %>%
ct(relig, show_na = FALSE, wt = weight) %>%
mutate(type = "Male/\nFemale")
gg2 <- cces %>%
filter(year >= 2021) %>%
filter(gender == 3 | gender == 4) %>%
mutate(relig = frcode(religion == 1 ~ "Protestant",
religion == 2 ~ "Catholic",
religion >= 3 & religion <= 8 ~ "Other World Religion",
religion == 9 ~ "Atheist",
religion == 10 ~ "Agnostic",
religion == 11 ~ "Nothing in Particular",
religion == 12 ~ "Something Else")) %>%
ct(relig, show_na = FALSE, wt = weight) %>%
mutate(type = "Non-binary/\nOther")
both <- bind_rows(gg1, gg2)
both %>%
ggplot(., aes(x = type, y = pct, fill = type)) +
geom_col(color = "black") +
facet_wrap(~ relig) +
theme_rb() +
lab_bar(type = pct, above = TRUE, pos = .045, sz = 9) +
scale_fill_manual(values = c("#E93332", "#303180")) +
scale_y_continuous(labels = percent, limits = c(0, .39)) +
labs(x = "", y = "", title = "Religious Composition by Gender Identity",
caption = "@ryanburge\nData: Cooperative Election Study, 2021-2023")
save("gender_relig_comp.png", wd = 6, ht = 8)
gg1 <- cces %>%
filter(year >= 2021) %>%
filter(gender == 1 | gender == 2) %>%
cces_attend(pew_attendance) %>%
ct(att, show_na = FALSE, wt = weight) %>%
mutate(type = "Male/Female")
gg2 <- cces %>%
filter(year >= 2021) %>%
filter(gender == 3 | gender == 4) %>%
cces_attend(pew_attendance) %>%
ct(att, show_na = FALSE, wt = weight) %>%
mutate(type = "Non-binary/Other")
both <- bind_rows(gg1, gg2)
both %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(att))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ type, ncol =1, strip.position = "left") +
theme_rb() +
scale_fill_manual(values = c(met.brewer("Egypt", 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(pct >.05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 10, family = "font", color = "white") +
geom_text(aes(label = ifelse(pct >.05 & att == "Never", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 10, family = "font", color = "black") +
geom_text(aes(label = ifelse(pct >.05 & att == "Seldom", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 10, family = "font", color = "black") +
theme(plot.title = element_text(size = 16)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
theme(legend.text = element_text(size = 20)) +
labs(x = "", y = "", title = "Religious Attendance by Gender Identity", caption = "@ryanburge\nData: Cooperative Election Study, 2021-2023")
save("gender_id_relattend.png", wd = 10, ht = 3)
scale_01 <- function(df, cols = NULL) {
# If no specific columns are provided, scale all numeric columns
cols <- cols %||% names(df)[sapply(df, is.numeric)]
# Scale each specified column to 0-1
df <- df %>%
mutate(across(all_of(cols), ~ (. - min(.)) / (max(.) - min(.)), .names = "scaled_{.col}"))
return(df)
}
regg <- cces %>%
filter(age <= 60) %>%
filter(year >= 2021) %>%
mutate(trans = case_when(gender == 3 | gender == 4 ~ 1,
gender == 1 | gender == 2 ~ 0)) %>%
mutate(att = 7 - pew_attendance) %>%
mutate(none = case_when(religion == 9 | religion == 10 | religion == 11 ~ 1,
TRUE ~ 0)) %>%
mutate(lib = case_when(ideo5 == 1 | ideo5 == 2 ~ 1,
ideo5 <= 5 ~ 0)) %>%
mutate(id3 = frcode(ideo5 == 1 | ideo5 == 2 ~ "Liberal",
ideo5 == 3 ~ "Moderate",
ideo5 == 4 | ideo5 == 5 ~ "Conservative")) %>%
select(age, educ, income, att, none, lib, trans, id3)
out <- glm(trans ~ ., data = regg)
library(jtools)
summ(out)
out <- glm(trans ~ I(age^2) * id3 + ., data = regg)
library(interactions)
pp <- interact_plot(out, pred= age, modx = id3, int.width = .76, interval = TRUE)
pp +
theme_rb(legend = TRUE) +
pid3_color() +
pid3_fill() +
scale_y_continuous(labels = percent) +
theme(plot.title = element_text(size = 26)) +
labs(x = "Age of Respondent", y = "", title = "Likelihood of Identifying as Non-binary/Other",
caption = "@ryanburge\nData: Cooperative Election Study, 2021-2023")
save("trans_interact_plot.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment