Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created May 29, 2020 03:46
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/098b355ec5a242d94ae2004e19a8d08f to your computer and use it in GitHub Desktop.
Save ryanburge/098b355ec5a242d94ae2004e19a8d08f to your computer and use it in GitHub Desktop.
gg <- cces18 %>%
mutate(trad2 = frcode(evangelical == 1 & race == 1 ~ "White Evangelical",
evangelical == 1 & race != 1 ~ "Non-White Evangelical",
mainline == 1 ~ "Mainline",
religpew == 1 & race == 2 ~ "Black Protestant",
catholic == 1 & race == 1 ~ "White Catholic",
catholic == 1 & race != 1 ~ "Non-White Catholic",
religpew == 3 ~ "Mormon",
religpew == 4 ~ "Orthodox",
religpew == 5 ~ "Jewish",
religpew == 6 ~ "Muslim",
religpew == 7 ~ "Buddhist",
religpew == 8 ~ "Hindu",
religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular",
TRUE ~ "Unclassified")) %>%
mutate(rac = case_when(CC18_422b == 1 | CC18_422b == 2 ~ 1,
CC18_321f == 3 | CC18_422b == 4 | CC18_422b == 5 ~ 0)) %>%
group_by(trad2) %>%
mean_ci(rac, ci = .84, wt = commonweight)
gg %>%
ggplot(., aes(x = reorder(trad2, mean), y = mean, fill = mean)) +
geom_col(color = "black") +
coord_flip() +
theme_gg("Abel") +
scale_fill_gradient(low = "#2E1437", high = "#948E99") +
y_pct() +
lab_bar(top = FALSE, type = mean, pos = .02, sz = 4) +
error_bar() +
labs(x = "", y = "Percent Who Agree", title = "Racial problems in the U.S. are rare, isolated situations", caption = "@ryanburge\nData: CCES 2018") +
ggsave("E://race_problems.png", type = "cairo-png", width = 7)
gg <- cces18 %>%
mutate(trad2 = frcode(evangelical == 1 & race == 1 ~ "White Evangelical",
evangelical == 1 & race != 1 ~ "Non-White Evangelical",
mainline == 1 ~ "Mainline",
religpew == 1 & race == 2 ~ "Black Protestant",
catholic == 1 & race == 1 ~ "White Catholic",
catholic == 1 & race != 1 ~ "Non-White Catholic",
religpew == 3 ~ "Mormon",
religpew == 4 ~ "Orthodox",
religpew == 5 ~ "Jewish",
religpew == 6 ~ "Muslim",
religpew == 7 ~ "Buddhist",
religpew == 8 ~ "Hindu",
religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular",
TRUE ~ "Unclassified")) %>%
mutate(rac1 = case_when(CC18_422e == 1 | CC18_422e == 2 ~ 1,
CC18_422e == 3 | CC18_422e == 4 | CC18_422e == 5 ~ 0)) %>%
mutate(rac2 = case_when(CC18_422f == 4 | CC18_422f == 5 ~ 1,
CC18_422f == 1 | CC18_422f == 2 | CC18_422f == 3 ~ 0)) %>%
mutate(rac3 = case_when(CC18_422g == 4 | CC18_422g == 5 ~ 1,
CC18_422g == 1 | CC18_422g == 2 | CC18_422g == 3 ~ 0)) %>%
mutate(rac4 = case_when(CC18_422h == 1 | CC18_422h == 2 ~ 1,
CC18_422h == 3 | CC18_422h == 4 | CC18_422h == 5 ~ 0)) %>%
mutate(resent = rac1 + rac2 + rac3 + rac4) %>%
group_by(trad2) %>%
ct(resent, show_na = FALSE, wt = commonweight) %>%
mutate(resent = frcode(resent == 0 ~ "Zero",
resent == 1 ~ "One",
resent == 2 ~ "Two",
resent == 3 ~ "Three",
resent == 4 ~ "Four"))
lvl <- gg %>%
filter(resent == "Four") %>%
select(trad2, sort = pct)
gg <- left_join(gg, lvl)
gg$trad2 <- fct_reorder(gg$trad2, gg$sort, .desc = TRUE)
gg %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(resent))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ trad2, ncol =1, strip.position = "left") +
fill5_scale() +
theme_gg("Abel") +
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(pct*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "black") +
labs(x = "", y = "", title = "Distribution of Racial Resentment Scores", subtitle = "Higher Numbers = More Racial Resentment", caption = "@ryanburge\nData: CCES 2018") +
ggsave("E://trad2_racial_resentment.png", width = 9, height = 6)
# https://git.io/Jfo5K
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment