Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Last active Jan 15, 2021
Embed
What would you like to do?
Polarization
fun <- function(df, var1, var2, weight, yr) {
aaa1 <- df %>%
filter({{var1}} <= 7) %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
rename(id := {{var1}}) %>%
ct(id, wt = {{weight}}) %>%
mutate(year = yr) %>%
mutate(group = "Democrats")
aaa2 <- df %>%
filter({{var2}} <= 7) %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
rename(id := {{var2}}) %>%
ct(id, wt = {{weight}}) %>%
mutate(year = yr) %>%
mutate(group = "Republicans")
bind_rows(aaa1, aaa2)
}
yyy1 <- cces12 %>% fun(CC334E, CC334F, weight_vv, yr = 2012)
yyy2 <- cces14 %>% fun(CC334K, CC334L, weight, yr = 2014)
yyy3 <- cces16 %>% fun(CC16_340g, CC16_340h, commonweight, yr = 2016)
yyy4 <- cces17 %>% fun(CC17_350d, CC17_350e, weights_common, yr = 2017)
yyy5 <- cces18 %>% fun(CC18_334D, CC18_334E, commonweight, yr = 2018)
yyy6 <- cces19 %>% fun(CC19_334d, CC19_334e, commonweight, yr = 2019)
graph <- bind_df("yyy")
gg1 <- graph %>%
filter(id == 1 & group == "Democrats") %>%
mutate(type = "Very Liberal Democrats") %>%
select(trad2, pct, year, type)
gg2 <- graph %>%
filter(id == 7 & group == "Republicans") %>%
mutate(type = "Very Conservative Republicans") %>%
select(trad2, pct, year, type)
graph <- bind_rows(gg1, gg2)
graph %>%
ggplot(., aes(x = year, y = pct, color = type, group = type)) +
geom_point(size=3, color="white") +
geom_point(size=2, shape=1) +
geom_point(size=1, shape=19) +
geom_line() +
facet_wrap(~ trad2) +
theme_rb(legend = TRUE) +
scale_color_manual(values = c("firebrick3", "dodgerblue3")) +
y_pct() +
labs(x = "", y = "", title = "How Would You Describe the Ideology of Democrats and Republicans?", caption = "@ryanburge\nData: CCES 2012-2019") +
ggsave("E://parties_extreme.png", type = "cairo-png", height = 10)
fun <- function(df, var1, var2, weight, yr) {
aaa1 <- df %>%
filter({{var1}} <= 7) %>%
filter({{var2}} <= 7) %>%
cces_trad(religpew) %>%
rename(dem := {{var1}}) %>%
rename(rep := {{var2}}) %>%
rename(wt := {{weight}}) %>%
select(trad2, dem, rep, wt) %>%
mutate(year = yr)
}
yyy1 <- cces12 %>% fun(CC334E, CC334F, weight_vv, yr = 2012)
yyy2 <- cces14 %>% fun(CC334K, CC334L, weight, yr = 2014)
yyy3 <- cces16 %>% fun(CC16_340g, CC16_340h, commonweight, yr = 2016)
yyy4 <- cces17 %>% fun(CC17_350d, CC17_350e, weights_common, yr = 2017)
yyy5 <- cces18 %>% fun(CC18_334D, CC18_334E, commonweight, yr = 2018)
yyy6 <- cces19 %>% fun(CC19_334d, CC19_334e, commonweight, yr = 2019)
gg <- bind_df("yyy")
gg <- gg %>%
mutate(ex = frcode(rep == 7 & dem == 1 ~ "Both",
rep == 7 ~ "Republicans",
dem == 1 ~ "Democrats",
TRUE ~ "Neither")) %>%
as_tibble()
graph <- gg %>%
group_by(year, trad2) %>%
ct(ex, wt = wt, show_na = FALSE)
graph %>%
mutate(pct = round(pct,2)) %>%
ggplot(., aes(x = factor(year), y = pct, fill= ex)) +
geom_col(color = "black") +
facet_wrap(~ trad2) +
scale_fill_manual(values = c("darkorchid", "firebrick3", "dodgerblue", "azure3")) +
theme_rb(legend = TRUE) +
y_pct() +
labs(x = "", y = "", title = "Share Seeing Each Political Party as the Most Extreme",
subtitle = "Very Conservative for Republicans/Very Liberal for Democrats",
caption = "@ryanburge\nData: CCES 2012-2019") +
geom_text(aes(label = ifelse(pct >.10, paste0(pct*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "black") +
ggsave("E://polarized_views.png", type = "cairo-png", height = 8, width = 10)
fun19 <- function(df, var1, var2, var3, weight, yr) {
aaa1 <- df %>%
filter({{var1}} <= 7) %>%
filter({{var2}} <= 7) %>%
filter({{var3}} <= 7) %>%
cces_trad(religpew) %>%
rename(dem := {{var1}}) %>%
rename(rep := {{var2}}) %>%
rename(self := {{var3}}) %>%
rename(wt := {{weight}}) %>%
mutate(age = 2019 - birthyr) %>%
select(trad2, dem, rep, self, wt, educ, faminc_new, newsint, age, gender, pew_churatd, race) %>%
mutate(year = yr)
}
ttt16 <- cces19 %>% fun19(CC19_334d, CC19_334e, CC19_334a, commonweight, yr = 2019)
ttt16 <- ttt16 %>%
mutate(ex = frcode(rep == 7 & dem == 1 ~ "Both",
rep == 7 ~ "Republicans",
dem == 1 ~ "Democrats",
TRUE ~ "Neither")) %>%
as_tibble() %>%
mutate(self = frcode(self == 1 ~ "Very\nLiberal",
self == 2 ~ "Liberal",
self == 3 ~ "Somewhat\nLiberal",
self == 4 ~ "Middle of\nthe Road",
self == 5 ~ "Somewhat\nConservative",
self == 6 ~ "Conservative",
self == 7 ~ "Very\nConservative"))
gg <- ttt16 %>%
group_by(self) %>%
ct(ex, wt = wt, show_na = FALSE)
gg %>%
mutate(pct = round(pct,2)) %>%
ggplot(., aes(x = self, y = pct, fill= ex)) +
geom_col(color = "black") +
scale_fill_manual(values = c("darkorchid", "firebrick3", "dodgerblue", "azure3")) +
theme_rb(legend = TRUE) +
y_pct() +
labs(x = "Respondent's Personal Ideology", y = "", title = "Share Seeing Each Political Party as the Most Extreme",
subtitle = "Very Conservative for Republicans/Very Liberal for Democrats",
caption = "@ryanburge\nData: CCES 2019") +
geom_text(aes(label = ifelse(pct >.05, paste0(pct*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "black") +
ggsave("E://polarized_views_2019.png", type = "cairo-png", height = 7, width = 7)
ttt16 <- ttt16 %>%
filter(faminc_new <= 20) %>%
mutate(ex = frcode(rep == 7 & dem == 1 ~ "Both",
rep == 7 ~ "Republicans",
dem == 1 ~ "Democrats",
TRUE ~ "Neither")) %>%
mutate(ex = case_when(ex == "Neither" ~ 0,
TRUE ~ 1)) %>%
mutate(male = case_when(gender == 1 ~ 1, gender == 2 ~ 0)) %>%
mutate(att = car::recode(pew_churatd, "6=1; 5=2; 4=3; 3=4; 2=5; 1=6; else = NA")) %>%
mutate(news = case_when(newsint == 4 ~ 1,
newsint == 3 ~ 2,
newsint == 2 ~ 3,
newsint == 1 ~ 4)) %>%
mutate(pid3 = frcode(self == 1 | self == 2 | self == 3 ~ "Liberal",
self == 4 ~ "Moderate",
self == 5 | self == 6 | self == 7 ~ "Conservative")) %>%
mutate(rac2 = frcode(race == 1 ~ "White", TRUE ~ "Non-White"))
reg1 <- glm(ex ~ pid3*att*rac2 + male + age + faminc_new + educ + news, family = "binomial", data = ttt16 )
int <- interact_plot(reg1, pred= att, modx = pid3, mod2 = rac2, int.width = .76, interval = TRUE, mod2.labels = c("White", "Non-White"))
int +
theme_rb(legend = TRUE) +
pid3_color() +
pid3_fill() +
y_pct() +
scale_x_continuous(breaks = c(1,2,3,4,5,6), labels = c("Never", "Seldom", "Yearly", "Monthly", "Weekly", "Weekly+")) +
labs(x = "Church Attendance", y = "Estimated Likelihood of Seeing\nAt Least One Party as Extreme",
title = "Church Attendance Does Little to Reduce Polarized Views among Conservatives",
caption = "@ryanburge\nData: CCES 2019") +
ggsave("E://interact_extreme_views.png", type = "cairo-png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment