Skip to content

Instantly share code, notes, and snippets.

@ryanburge

ryanburge/071818.R

Last active Jul 15, 2018
Embed
What would you like to do?
Tweet about generational differences in PID
library(building)
library(ggsci)
two_sample <- function(df, low, high, age) {
low <- enquo(low)
high <- enquo(high)
age <- enquo(age)
a1 <- df %>%
mutate(age = 2016 - birthyr) %>%
filter(age >= !!low & age <= !!high) %>%
filter(race ==1) %>%
filter(educ <= 4) %>%
filter(evangelical ==1) %>%
mutate(group = "Evangelical")
a2 <- df %>%
mutate(age = 2016 - birthyr) %>%
filter(age >= !!low & age <= !!high) %>%
filter(race ==1) %>%
filter(educ <= 4) %>%
mutate(group = "Entire Sample ")
full <- bind_rows(a1, a2)
result <- full %>%
filter(pid7 <=7) %>%
group_by(group) %>%
mean_ci(pid7) %>%
mutate(age = !! age)
}
first <- two_sample(cces16, 18, 35, "18-35")
second <- two_sample(cces16, 36, 44, "36-44")
third <- two_sample(cces16, 45, 54, "45-54")
four <- two_sample(cces16, 55, 64, "55-64")
five <- two_sample(cces16, 65, 100, "65-100")
graph <- bind_rows(first, second, third, four, five)
font_add_google("Oswald", "font")
showtext_auto()
graph %>%
ggplot(., aes(x=mean, y=age)) +
geom_point(shape = 21, aes(color=group, fill = group), size=5, stroke=.5) +
geom_errorbarh(aes(xmin = lower, xmax=upper, color = group), size = 1.25, height = .25) +
theme_minimal() +
theme(text=element_text(size=64, family="font")) +
theme(legend.position = "bottom") +
scale_fill_aaas() +
scale_color_aaas() +
labs(x = "Mean Party Identification", y = "Age Group", title = "Young Evangelicals Are Further Away From Their Counterparts", caption = "Data: CCES 2016", subtitle = "Includes only White Respondents with at least a 2 Year College Degree") +
scale_x_continuous(limits = c(3.5,5.5), breaks = c(1,2,3,4,5,6,7), labels = c("Strong Democrat", "Not Strong Democrat", "Lean Democrat", "Independent", "Lean Republican", "Not Strong Republican", "Strong Republican")) +
theme(plot.title = element_text(face = "bold")) +
theme(legend.title=element_blank())
ggsave("age_diff_pid.png", type = "cairo-png", width = 12, height = 8)
library(building)
library(ggsci)
two_sample <- function(df, low, high, age) {
low <- enquo(low)
high <- enquo(high)
age <- enquo(age)
a1 <- df %>%
mutate(age = 2016 - birthyr) %>%
filter(age >= !!low & age <= !!high) %>%
filter(race ==1) %>%
filter(educ <= 4) %>%
filter(evangelical ==1) %>%
mutate(group = "Evangelical")
a2 <- df %>%
mutate(age = 2016 - birthyr) %>%
filter(age >= !!low & age <= !!high) %>%
filter(race ==1) %>%
filter(educ <= 4) %>%
mutate(group = "Entire Sample ")
full <- bind_rows(a1, a2)
result <- full %>%
filter(pid7 <=7) %>%
group_by(group) %>%
filter(CC16_410a <=2) %>%
ct(CC16_410a, wt = commonweight_vv_post) %>%
mutate(age = !! age)
}
first <- two_sample(cces16, 18, 35, "18-35")
second <- two_sample(cces16, 36, 44, "36-44")
third <- two_sample(cces16, 45, 54, "45-54")
four <- two_sample(cces16, 55, 64, "55-64")
five <- two_sample(cces16, 65, 100, "65-100")
graph <- bind_rows(first, second, third, four, five)
font_add_google("Oswald", "font")
showtext_auto()
graph %>%
filter(CC16_410a ==1) %>%
ggplot(., aes(x= group, y = pct)) +
geom_col(aes(fill = as.factor(group)), color = "black") +
theme_minimal() +
theme(text=element_text(size=64, family="font")) +
scale_y_continuous(labels = scales::percent) + theme(legend.position="none") +
facet_grid(.~ age) +
scale_fill_aaas() +
geom_text(aes(y = pct + .025, label = paste0(pct*100, '%')), position = position_dodge(width = .9), size = 16, family = "font")+
labs(x = "", y = "Share of Two Party Vote for Trump", title = "Young Evangelicals Were + 30% for Trump", caption = "Data: CCES 2016", subtitle = "Includes only White Respondents with at least a 2 Year College Degree") +
ggsave("age_diff_vote.png", type = "cairo-png", width = 15, height = 8)
ab <- cces16 %>%
filter(evangelical ==1) %>%
mutate(abany = recode(CC16_332a, "1=1; 2=0; else =99")) %>%
mutate(age = 2016 - birthyr) %>%
group_by(age) %>%
ct(abany, commonweight_vv_post) %>%
filter(abany == 1) %>%
mutate(issue = "Abortion")
gay <- cces16 %>%
# filter(evangelical ==1) %>%
mutate(abany = recode(CC16_335, "1=1; 2=0; else =99")) %>%
# mutate(age = 2016 - birthyr) %>%
# group_by(age) %>%
ct(abany, commonweight_vv_post) %>%
filter(abany == 1) %>%
mutate(issue = "Gay Marriage")
graph <- bind_rows(ab, gay)
font_add_google("Oswald", "font")
showtext_auto()
ggplot(graph, aes(x=age, y = pct, group = issue, color = as.factor(issue))) +
geom_point() +
geom_smooth() +
theme_minimal() +
scale_x_continuous(limits = c(18,75)) +
theme(text=element_text(size=64, family="font")) +
scale_y_continuous(limits = c(0, .60), labels = scales::percent) + theme(legend.position="none") +
labs(x = "Age", y = "Percent in Favor", title = "Social Issue Support Among Evangelicals", caption = "Data: CCES 2016") +
annotate("text", x= 66, y = .22, label = "Gay Marriage", size = 24, family = "font") +
annotate("text", x= 68, y = .38, label = "Abortion", size = 24, family = "font") +
scale_color_d3()
ggsave("age_gay_aby.png", type = "cairo-png", width = 9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment