Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created February 26, 2021 16:33
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/6adb7fefb60d547bf935c1049250ca87 to your computer and use it in GitHub Desktop.
Save ryanburge/6adb7fefb60d547bf935c1049250ca87 to your computer and use it in GitHub Desktop.
ns <- read.fst("D://ns_full.fst") %>% as_tibble()
gg <- ns %>%
mutate(pid2 = frcode(pid3 == 1 ~ "Democrat",
pid3 == 3 ~ "Independent",
pid3 == 2 ~ "Republican")) %>%
mutate(xtn = statements_gender_identity) %>%
mutate(xtn = frcode(xtn == 4 ~ "Strongly Disagree",
xtn == 3 ~ "Somewhat Disagree",
xtn == 2 ~ "Somewhat Agree",
xtn == 1 ~ "Strongly Agree")) %>%
group_by(pid2) %>%
ct(xtn, wt = weight, show_na = FALSE) %>% na.omit()
gg1 <- ns %>%
mutate(xtn = statements_gender_identity) %>%
mutate(xtn = frcode(xtn == 4 ~ "Strongly Disagree",
xtn == 3 ~ "Somewhat Disagree",
xtn == 2 ~ "Somewhat Agree",
xtn == 1 ~ "Strongly Agree")) %>%
ct(xtn, wt = weight, show_na = FALSE) %>% na.omit() %>%
mutate(pid2 = "Entire Sample")
graph <- bind_rows(gg, gg1)
graph$pid2 <- factor(graph$pid2, levels = c("Entire Sample", "Democrat", "Independent", "Republican"))
graph %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(xtn))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ pid2, ncol =1, strip.position = "left") +
scale_fill_manual(values = c("#EE4035", "#F37736", "#7BC043", "#0492CF")) +
theme_rb() +
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 >.08, paste0(pct*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "black") +
labs(x = "", y = "", title = "Agreement with this statement: There are only two genders- male and female", subtitle = "", caption = "@ryanburge\nData: Nationscape (July 2019-June 2020)") +
ggsave("E://two_genders.png", width = 9, height = 3)
graph <- ns %>%
mutate(trans = statements_gender_identity) %>%
mutate(trans = case_when(trans == 1 | trans == 2 ~ 1,
trans == 3 | trans == 4 ~ 0)) %>%
group_by(wave) %>%
mean_ci(trans, wt = weight, ci = .84) %>%
na.omit() %>%
mutate(pid2 = "Entire Sample")
graph1 <- ns %>%
mutate(trans = statements_gender_identity) %>%
mutate(trans = case_when(trans == 1 | trans == 2 ~ 1,
trans == 3 | trans == 4 ~ 0)) %>%
mutate(pid2 = frcode(pid3 == 1 ~ "Democrat",
pid3 == 3 ~ "Independent",
pid3 == 2 ~ "Republican")) %>%
group_by(wave, pid2) %>%
mean_ci(trans, wt = weight, ci = .84) %>%
na.omit()
gg <- bind_rows(graph, graph1)
gg$pid2 <- factor(gg$pid2, levels = c("Entire Sample", "Democrat", "Independent", "Republican"))
every_nth = function(n) {
return(function(x) {x[c(TRUE, rep(FALSE, n - 1))]})
}
gg %>%
ggplot(., aes(x = wave, y = mean, color = pid2, group = pid2)) +
geom_point() +
smooth() +
scale_color_manual(values = c("black", "dodgerblue3", "forestgreen", "firebrick3")) +
theme_rb(legend = TRUE) +
y_pct() +
scale_x_discrete(breaks = every_nth(n = 3)) +
labs(x = "", y = "Share Agreeing", title = "Agreement with this statement: There are only two genders- male and female", caption = "@ryanburge\nData: Nationscape (July 2019-June 2020)") +
ggsave("E://two_gender_long.png", height = 7)
gg <- ns %>%
ns_trad(religion) %>%
group_by(trad) %>%
mutate(xtn = statements_gender_identity) %>%
mutate(xtn = frcode(xtn == 4 ~ "Strongly Disagree",
xtn == 3 ~ "Somewhat Disagree",
xtn == 2 ~ "Somewhat Agree",
xtn == 1 ~ "Strongly Agree")) %>%
ct(xtn, wt = weight, show_na = FALSE) %>% na.omit()
lvl <- gg %>%
filter(xtn == "Strongly Agree") %>%
select(trad, sort = pct)
gg <- left_join(gg, lvl)
gg$trad <- fct_reorder(gg$trad, gg$sort, .desc = TRUE)
gg %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(xtn))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ trad, ncol =1, strip.position = "left") +
scale_fill_manual(values = c("#EE4035", "#F37736", "#7BC043", "#0492CF")) +
theme_rb() +
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 >.08, paste0(pct*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "black") +
labs(x = "", y = "", title = "Agreement with this statement: There are only two genders- male and female", subtitle = "", caption = "@ryanburge\nData: Nationscape (July 2019-June 2020)") +
ggsave("E://two_genders_trad.png", width = 9, height = 6)
reg <- ns %>%
filter(age <= 80) %>%
mutate(gender = frcode(gender == 2 ~ "Men",
gender == 1 ~ "Women")) %>%
mutate(trans = statements_gender_identity) %>%
mutate(trans = case_when(trans == 1 | trans == 2 ~ 1,
trans == 3 | trans == 4 ~ 0)) %>%
mutate(white = case_when(race_ethnicity == 1 & hispanic == 1 ~ 1, TRUE ~ 0)) %>%
mutate(pid2 = frcode(pid3 == 1 ~ "Democrat",
pid3 == 3 ~ "Independent",
pid3 == 2 ~ "Republican")) %>%
mutate(evan = frcode(is_evangelical == 1 ~ "Evangelical",
is_evangelical == 2 ~ "Non-Evangelical")) %>%
select(gender, age, trans, education, income = household_income, pid2, evan, white)
tt <- glm(trans ~ age*pid2*evan + white + gender + education + income, family = "binomial", data = reg)
plot <- interact_plot(tt, pred= age, modx = pid2, mod2 = evan, int.width = .76, interval = TRUE, mod2.labels = c("Evangelical", "Non-Evangelical"))
plot +
theme_rb(legend = TRUE) +
pid3_color() +
pid3_fill() +
y_pct() +
labs(x = "Age", y = "Estimate of Agreement", title = "Agreement with this statement: There are only two genders- male and female", caption = "@ryanburge\nData: Nationscape (July 2019-June 2020)") +
ggsave("E://trans_interact.png", type = "cairo-png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment