Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created June 12, 2019 18:42
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/17a4d939aaef826b2515f5ff34300401 to your computer and use it in GitHub Desktop.
Save ryanburge/17a4d939aaef826b2515f5ff34300401 to your computer and use it in GitHub Desktop.
2018 Vote Choice
library(socsci)
library(car)
library(labelled)
library(haven)
source("D://theme.R")
cces18 <- read_dta("D://cces18.dta")
fun <- function(df, relig, fam){
relig <- enquo(relig)
df %>%
group_by(!! relig) %>%
mutate(CC18_412 = as.numeric(CC18_412)) %>%
mutate(house = car::recode(CC18_412, "1 = 'Democrat'; 2 = 'Republican'; 3:90 = 'All Others'; else = NA")) %>%
filter(house != "NA") %>%
ct(house, wt = commonpostweight) %>%
mutate(relig = to_factor(!! relig)) %>%
filter(relig != "Skipped") %>%
mutate(family = fam)
}
aa <- fun(cces18, religpew_methodist, "Methodist")
aa1 <- fun(cces18, religpew_baptist, "Baptist")
aa2 <- fun(cces18, religpew_nondenom, "Non-Denom")
aa3 <- fun(cces18, religpew_lutheran, "Lutheran")
aa4 <- fun(cces18, religpew_presby, "Presbyterian")
aa5 <- fun(cces18, religpew_pentecost, "Pentecostal")
aa6 <- fun(cces18, religpew_episcop, "Episcopal")
aa7 <- fun(cces18, religpew_congreg, "Congregational")
aa8 <- fun(cces18, religpew_holiness, "Holiness")
aa9 <- fun(cces18, religpew_reformed, "Reformed")
aa10 <- cces18 %>%
group_by(religpew) %>%
mutate(CC18_412 = as.numeric(CC18_412)) %>%
mutate(house = car::recode(CC18_412, "1 = 'Democrat'; 2 = 'Republican'; 3:90 = 'All Others'; else = NA")) %>%
filter(house != "NA") %>%
ct(house, wt = commonpostweight) %>%
mutate(relig = to_factor(religpew)) %>%
filter(relig != "Protestant") %>%
select(-religpew)
graph <- bind_df("aa") %>%
select(relig, house, pct, n, family )
fun <- function(df, relig, fam){
relig <- enquo(relig)
df %>%
group_by(!! relig) %>%
count(wt = commonpostweight) %>%
mutate(relig = to_factor(!! relig)) %>%
filter(relig != "Skipped") %>%
mutate(family = fam)
}
aa <- fun(cces18, religpew_methodist, "Methodist")
aa1 <- fun(cces18, religpew_baptist, "Baptist")
aa2 <- fun(cces18, religpew_nondenom, "Non-Denom")
aa3 <- fun(cces18, religpew_lutheran, "Lutheran")
aa4 <- fun(cces18, religpew_presby, "Presbyterian")
aa5 <- fun(cces18, religpew_pentecost, "Pentecostal")
aa6 <- fun(cces18, religpew_episcop, "Episcopal")
aa7 <- fun(cces18, religpew_congreg, "Congregational")
aa8 <- fun(cces18, religpew_holiness, "Holiness")
aa9 <- fun(cces18, religpew_reformed, "Reformed")
aa10 <- cces18 %>%
group_by(religpew) %>%
count(wt = commonpostweight) %>%
mutate(relig = to_factor(religpew)) %>%
filter(relig != "Protestant") %>%
select(-religpew)
size <- bind_df("aa") %>%
select(relig, n) %>%
filter(n > 150)
final <- semi_join(graph, size, by = "relig") %>%
ungroup(religpew_methodist) %>%
select(relig, house, pct, n, family)
aa10 <- cces18 %>%
group_by(religpew) %>%
mutate(CC18_412 = as.numeric(CC18_412)) %>%
mutate(house = car::recode(CC18_412, "1 = 'Democrat'; 2 = 'Republican'; 3:90 = 'All Others'; else = NA")) %>%
filter(house != "NA") %>%
ct(house, wt = commonpostweight) %>%
mutate(relig = to_factor(religpew)) %>%
ungroup(religpew) %>%
select(-religpew)
aa10$relig <- factor(aa10$relig, levels = c("Mormon", "Protestant", "Eastern or Greek Orthodox", "Roman Catholic", "Something else", "Nothing in particular", "NA", "Jewish", "Agnostic", "Hindu", "Buddhist", "Muslim", "Atheist"))
aa10 %>%
filter(relig != "NA") %>%
ggplot(., aes(x = 1, y = pct, fill = house)) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ relig, ncol =1, strip.position = "left") +
scale_fill_manual(values = c('Republican' = "firebrick3", 'Democrat' = "dodgerblue3", 'All Others' = "azure3")) +
theme_gg("Abel") +
theme(legend.position = "none") +
scale_y_continuous(labels = percent) +
theme(strip.text.y = element_text(angle = 180)) +
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 = paste0(pct*100, '%')), position = position_stack(vjust = 0.5), size = 4, family = "font") +
labs(x = "", y = "", title = "2018 House Vote Choice", subtitle = "", caption = "@ryanburge\nData: CCES 2018") +
ggsave("E://cces18_house.png")
bapt <- final %>%
filter(family == "Baptist")
bapt$relig <- factor(bapt$relig, levels = c("Independent Baptist", "Southern Baptist Convention", "General Association of Regular Baptists", "Other Baptist", "Free Will Baptist", "American Baptist Churches in USA", "Baptist Missionary Association", "National Baptist Convention"))
bapt %>%
ggplot(., aes(x = 1, y = pct, fill = house)) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ relig, ncol =1, strip.position = "left") +
scale_fill_manual(values = c('Republican' = "firebrick3", 'Democrat' = "dodgerblue3", 'All Others' = "azure3")) +
theme_gg("Abel") +
theme(legend.position = "none") +
scale_y_continuous(labels = percent) +
theme(strip.text.y = element_text(angle = 180)) +
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 = paste0(pct*100, '%')), position = position_stack(vjust = 0.5), size = 6, family = "font") +
labs(x = "", y = "", title = "2018 House Vote Choice", subtitle = "", caption = "@ryanburge\nData: CCES 2018") +
ggsave("E://cces18_house_baptist.png", height = 5, width = 9)
other <- final %>%
filter(family != "Baptist")
nn <- other %>%
filter(house == "Republican") %>%
arrange(-pct) %>%
mutate(relig = as.factor(relig))
other$relig <- factor(other$relig, levels = nn$relig)
other %>%
ggplot(., aes(x = 1, y = pct, fill = house)) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ relig, ncol =1, strip.position = "left") +
scale_fill_manual(values = c('Republican' = "firebrick3", 'Democrat' = "dodgerblue3", 'All Others' = "azure3")) +
theme_gg("Abel") +
theme(legend.position = "none") +
scale_y_continuous(labels = percent) +
theme(strip.text.y = element_text(angle = 180)) +
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 = paste0(pct*100, '%')), position = position_stack(vjust = 0.5), size = 5, family = "font") +
labs(x = "", y = "", title = "2018 House Vote Choice", subtitle = "", caption = "@ryanburge\nData: CCES 2018") +
ggsave("E://cces18_house_nonbaptist.png", height = 8, width = 7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment