Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Last active June 8, 2020 20:02
Show Gist options
  • Save ryanburge/d700c7d09bc170e6c70cf020e29fea47 to your computer and use it in GitHub Desktop.
Save ryanburge/d700c7d09bc170e6c70cf020e29fea47 to your computer and use it in GitHub Desktop.
Jews, Episcopalians, and Puerto Ricans
cc16 <- cces16 %>%
mutate(group = frcode(religpew_protestant == 7 ~ "Episcopalian",
religpew == 5 ~ "Jewish",
Hispanic_origin_4 == 1~ "Puerto Rican")) %>%
mutate(age = 2016 - birthyr) %>%
select(group, pew_churatd, educ, age, gender, pew_religimp, pew_bornagain, vote = CC16_410a, race, weight = commonweight_vv, income = faminc) %>%
mutate(year = 2016)
cc18 <- cces18 %>%
mutate(group = frcode(religpew_protestant == 7 ~ "Episcopalian",
religpew == 5 ~ "Jewish",
CC18_354a_4 == 1~ "Puerto Rican")) %>%
mutate(age = 2018 - birthyr) %>%
select(group, pew_churatd, educ, age, gender, pew_religimp, pew_bornagain, vote = CC18_317, race, weight = commonpostweight, income = faminc_new) %>%
mutate(year = 2018)
cc <- bind_rows(cc16, cc18) %>%
as_tibble()
cc <- cc %>%
mutate(trump = case_when(vote == 1 ~ 1,
vote == 2 ~ 0)) %>%
mutate(male = case_when(gender == 1 ~ 1,
gender == 2 ~ 0)) %>%
mutate(white = case_when(race == 1 ~ 1,
race != 1 ~ 0))
graph <- cc %>%
ct(group, wt = weight)
graph %>%
mutate(n = round(n, 0)) %>%
filter(group != "NA") %>%
ggplot(., aes(x = group, y = n, fill = group)) +
geom_col(color = "black") +
fill4_3() +
theme_gg("Jost") +
geom_text(aes(y = n + 75, label = n), position = position_dodge(width = .9), size = 5.5, family = "font") +
labs(x = "", y = "", title = "Sample Size of the Three Groups") +
ggsave("E://jews_pr_epis.png", type = "cairo-png", width = 5)
graph <- cc %>%
group_by(group, year) %>%
mutate(vote = frcode(vote == 1 ~ "Trump",
vote == 2 ~ "Clinton",
vote == 3 | vote == 4 | vote == 5 | vote == 8 ~ "Someone Else")) %>%
ct(vote, wt = weight, show_na = FALSE)
one <- graph %>%
filter(group == "Episcopalian") %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(vote))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ year, ncol =1, strip.position = "left") +
scale_fill_manual(values = c("forestgreen","dodgerblue3", "firebrick3")) +
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 = "Vote Choice for President in 2016", subtitle = "Episcopalians", caption = "") +
ggsave("E://epis_vote.png", width = 5, height = 3)
two <- graph %>%
filter(group == "Jewish") %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(vote))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ year, ncol =1, strip.position = "left") +
scale_fill_manual(values = c("forestgreen","dodgerblue3", "firebrick3")) +
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 = "", subtitle = "Jewish", caption = "") +
ggsave("E://jewish_vote.png", width = 5, height = 3)
three <- graph %>%
filter(group == "Puerto Rican") %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(vote))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ year, ncol =1, strip.position = "left") +
scale_fill_manual(values = c("forestgreen","dodgerblue3", "firebrick3")) +
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 = "", subtitle = "Puerto Rican", caption = "") +
ggsave("E://pr_vote.png", width = 5, height = 3)
library(patchwork)
all <- one / two / three
ggsave("E://vote16_pr_ep_jew.png", type = "cairo-png", height = 7, width = 7, all)
graph <- cc %>%
filter(income <= 16) %>%
group_by(group) %>%
ct(income, show_na = FALSE, wt = weight)
graph %>%
filter(group != "NA") %>%
ggplot(., aes(x = income, y = pct, fill = group)) +
geom_col(color = "black") +
facet_wrap(~ group, ncol = 1) +
fill4_3() +
theme_gg("Jost") +
scale_x_continuous(breaks = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16), labels = c("<$10k", "$10k", "$20k", "$30k", "$40k",
"$50k", "$60k", "$70k", "$80k", "$100k",
"$120k", "$150k", "$200k", "$250k", "$350k", ">$500k")) +
y_pct() +
lab_bar(top = TRUE, type = pct, pos = .01, sz = 3) +
labs(x = "", y = "", title = "Income Distributions of Each Group", caption = "") +
ggsave("E://income_dist_pr_jew_ep.png", width = 9, type = "cairo-png")
graph <- cc %>%
group_by(group) %>%
mutate(inc = frcode(income <= 5 ~ "<$50k",
income >= 6 & income <= 9 ~ "$50k-$100k",
income >= 10 & income <= 16 ~ ">$100k")) %>%
ct(inc, wt = weight, show_na = FALSE) %>%
na.omit()
graph %>%
ggplot(., aes(x = inc, y = pct, fill = group)) +
geom_col(color = "black") +
facet_wrap(~ group, ncol = 3) +
fill4_3() +
theme_gg("Jost") +
y_pct() +
lab_bar(type = pct, pos = .02, sz = 5.5) +
labs(x = "", y = "", title = "Income Distribution Among the Three Groups", caption = "") +
ggsave("E://income_dist_pr.png", dpi = 300, type = "cairo-png", width = 8)
cc <- cc %>%
mutate(trump = case_when(vote == 1 ~ 1,
vote == 2 ~ 0)) %>%
mutate(male = case_when(gender == 1 ~ 1,
gender == 2 ~ 0)) %>%
mutate(att = case_when(pew_churatd == 6 ~ 1,
pew_churatd == 5 ~ 2,
pew_churatd == 4 ~ 3,
pew_churatd == 3 ~ 4,
pew_churatd == 2 ~ 5,
pew_churatd == 1 ~ 6)) %>%
mutate(income = car::recode(income, "20:99 = NA")) %>%
mutate(white = case_when(race == 1 ~ 1,
TRUE ~ 0))
reg1 <- glm(trump ~ income*group + age + educ + white + male + att, family = "binomial", data = cc)
gg <- interact_plot(reg1, pred= income, modx = group, int.width = .76, interval = TRUE)
gg +
fill4_3() +
color4_3() +
y_pct() +
theme_gg("Jost") +
theme(legend.position = c(.2, .8)) +
scale_x_continuous(breaks = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16), labels = c("<$10k", "$10k", "$20k", "$30k", "$40k",
"$50k", "$60k", "$70k", "$80k", "$100k",
"$120k", "$150k", "$200k", "$250k", "$350k", ">$500k")) +
labs(x = "", y = "Pr(Trump Vote)", title = "Impact of Income on Likelihood of a Trump Vote") +
ggsave("E://interact_pr_jews.png", type = "cairo-png", width = 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment