Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Last active April 25, 2019 14:11
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/f4aef554c5b6ba40ad5acd36cd901586 to your computer and use it in GitHub Desktop.
Save ryanburge/f4aef554c5b6ba40ad5acd36cd901586 to your computer and use it in GitHub Desktop.
Religious Composition
gss <- gss %>%
mutate(reltrad = car::recode(reltrad, "NA = 0; 5 =0; 6=0"))
hhh1 <- gss %>%
filter(year == 1978) %>%
filter(partyid == 0 | partyid == 1 | partyid == 2) %>%
ct(reltrad, wt = wtssall) %>%
mutate(year = 1978) %>%
mutate(party = "Democrat")
hhh2 <- gss %>%
filter(year == 1988) %>%
filter(partyid == 0 | partyid == 1 | partyid == 2) %>%
ct(reltrad, wt = wtssall) %>%
mutate(year = 1988) %>%
mutate(party = "Democrat")
hhh3 <- gss %>%
filter(year == 1998) %>%
filter(partyid == 0 | partyid == 1 | partyid == 2) %>%
ct(reltrad, wt = wtssall) %>%
mutate(year = 1998) %>%
mutate(party = "Democrat")
hhh4 <- gss %>%
filter(year == 2008) %>%
filter(partyid == 0 | partyid == 1 | partyid == 2) %>%
ct(reltrad, wt = wtssall) %>%
mutate(year = 2008) %>%
mutate(party = "Democrat")
hhh5 <- gss %>%
filter(year == 2018) %>%
filter(partyid == 0 | partyid == 1 | partyid == 2) %>%
ct(reltrad, wt = wtssall) %>%
mutate(year = 2018) %>%
mutate(party = "Democrat")
dem <- bind_df("hhh") %>%
mutate(reltrad = frcode(reltrad == 1 ~ " Evangelical ",
reltrad == 2 ~ " Mainline ",
reltrad == 3 ~ " Black Prot. ",
reltrad == 4 ~ " Catholic ",
reltrad == 7 ~ " No Religion ",
reltrad == 0 ~ " Other Faith "))
dem %>%
ggplot(., aes(x=1, y = pct, fill = fct_rev(reltrad))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ year, ncol =1) +
theme_gg("Abel") +
theme(legend.position = "bottom") +
scale_fill_paletteer_d(awtools, mpalette) +
scale_y_continuous(labels = percent) +
guides(fill = guide_legend(reverse=T, nrow =1)) +
theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) +
geom_text(aes(label = paste0(pct*100, '%')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
theme(panel.grid.major.y = element_blank()) +
theme(panel.grid.minor.y = element_blank()) +
theme(plot.title = element_text(size = 16)) +
labs(x = "", y = "", title = "How has the Religious Composition of Democrats Changed Over Time?", caption = "@ryanburge\nData: GSS 1978-2018") +
ggsave("E://dem_gss_time.png", type = "cairo-png")
hhh1 <- gss %>%
filter(year == 1978) %>%
filter(partyid == 4 | partyid == 5 | partyid == 6) %>%
ct(reltrad, wt = wtssall) %>%
mutate(year = 1978) %>%
mutate(party = "Republican")
hhh2 <- gss %>%
filter(year == 1988) %>%
filter(partyid == 4 | partyid == 5 | partyid == 6) %>%
ct(reltrad, wt = wtssall) %>%
mutate(year = 1988) %>%
mutate(party = "Republican")
hhh3 <- gss %>%
filter(year == 1998) %>%
filter(partyid == 4 | partyid == 5 | partyid == 6) %>%
ct(reltrad, wt = wtssall) %>%
mutate(year = 1998) %>%
mutate(party = "Republican")
hhh4 <- gss %>%
filter(year == 2008) %>%
filter(partyid == 4 | partyid == 5 | partyid == 6) %>%
ct(reltrad, wt = wtssall) %>%
mutate(year = 2008) %>%
mutate(party = "Republican")
hhh5 <- gss %>%
filter(year == 2018) %>%
filter(partyid == 4 | partyid == 5 | partyid == 6) %>%
ct(reltrad, wt = wtssall) %>%
mutate(year = 2018) %>%
mutate(party = "Republican")
dem <- bind_df("hhh") %>%
mutate(reltrad = frcode(reltrad == 1 ~ " Evangelical ",
reltrad == 2 ~ " Mainline ",
reltrad == 3 ~ " Black Prot. ",
reltrad == 4 ~ " Catholic ",
reltrad == 7 ~ " No Religion ",
reltrad == 0 ~ " Other Faith "))
dem %>%
ggplot(., aes(x=1, y = pct, fill = fct_rev(reltrad))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ year, ncol =1) +
theme_gg("Abel") +
theme(legend.position = "bottom") +
scale_fill_paletteer_d(awtools, mpalette) +
scale_y_continuous(labels = percent) +
guides(fill = guide_legend(reverse=T, nrow =1)) +
theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) +
geom_text(aes(label = paste0(pct*100, '%')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
theme(panel.grid.major.y = element_blank()) +
theme(panel.grid.minor.y = element_blank()) +
theme(plot.title = element_text(size = 15)) +
labs(x = "", y = "", title = "How has the Religious Composition of Republicans Changed Over Time?", caption = "@ryanburge\nData: GSS 1978-2018") +
ggsave("E://gop_gss_time.png", type = "cairo-png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment