-
-
Save ryanburge/119ca8e1ce8ebe48bae96963ea91a031 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df <- import("E://data/politics_first.sav") %>% | |
clean_names() | |
small <- df %>% | |
select(caseid, relig_2, relig_4, weight = weight1_4) %>% | |
mutate(religpew = relig_2) %>% | |
mutate(relig1 = frcode(religpew == 1 ~ "Protestant", | |
religpew == 2 ~ "Catholic", | |
religpew >= 3 & religpew <= 8 ~ "Other\nWorld\nReligions", | |
religpew == 9 ~ "Atheist", | |
religpew == 10 ~ "Agnostic", | |
religpew == 11 ~ "Nothing in Particular", | |
religpew == 12 ~ "Something Else")) %>% | |
mutate(religpew = relig_4) %>% | |
mutate(relig2 = frcode(religpew == 1 ~ "Protestant", | |
religpew == 2 ~ "Catholic", | |
religpew >= 3 & religpew <= 8 ~ "Other\nWorld\nReligions", | |
religpew == 9 ~ "Atheist", | |
religpew == 10 ~ "Agnostic", | |
religpew == 11 ~ "Nothing in Particular", | |
religpew == 12 ~ "Something Else")) %>% | |
select(caseid, relig1, relig2, weight) %>% | |
as_tibble() %>% | |
filter(relig1 != "NA") %>% | |
filter(relig2 != "NA") | |
library(reshape2) | |
test <- small %>% | |
select(-weight) %>% | |
melt(., id = c("caseid")) %>% arrange(caseid) | |
test <- test %>% | |
mutate(year = frcode(variable == "relig1" ~ "Summer 2011", | |
variable == "relig2" ~ "Fall 2012")) | |
library(ggalluvial) | |
test$value <- factor(test$value, levels = c("Protestant", "Catholic", "Other\nWorld\nReligions", "Atheist", "Agnostic", "Nothing in Particular", "Something Else")) | |
test$value <- fct_rev(test$value) | |
test$year <- fct_rev(test$year) | |
ggplot(test, aes(x = year, stratum = value, alluvium = caseid, fill = value, label = value)) + | |
geom_flow(stat = "alluvium", lode.guidance = "leftright") + | |
geom_stratum() + | |
theme_rb() + | |
coord_flip() + | |
geom_label(fill = "white", stat = "stratum", size = 4, colour = "black", family = "font") + | |
scale_fill_manual(values = c("#033f63", "#28666e", "#150023", "#e1e1e1", "#B5B682", "#FFA700", "#5f2680")) + | |
scale_color_manual(values = c("#033f63", "#28666e", "#150023", "#e1e1e1", "#B5B682", "#FFA700", "#5f2680")) + | |
theme(plot.title = element_text(size = 40, margin = margin(t = 0, r = 0, b = 10, l = 0))) + # Adjusted the top margin | |
labs(x = "Year", y = "Number of Respondents", title = "Religious Shifts Over Time", caption = "@ryanburge + @religiondata\nData: Putting Politics First, Panel Data (2010-2012)") | |
save("political_first_alluvial.png", wd = 10) | |
small <- df %>% | |
select(caseid, relig_2, relig_4, weight = weight1_4) %>% | |
mutate(religpew = relig_2) %>% | |
mutate(relig1 = frcode(religpew == 1 ~ "Protestant", | |
religpew == 2 ~ "Catholic", | |
religpew >= 3 & religpew <= 8 ~ "Other World Religions", | |
religpew == 9 ~ "Atheist", | |
religpew == 10 ~ "Agnostic", | |
religpew == 11 ~ "Nothing in Particular", | |
religpew == 12 ~ "Something Else")) %>% | |
mutate(religpew = relig_4) %>% | |
mutate(relig2 = frcode(religpew == 1 ~ "Protestant", | |
religpew == 2 ~ "Catholic", | |
religpew >= 3 & religpew <= 8 ~ "Other World Religions", | |
religpew == 9 ~ "Atheist", | |
religpew == 10 ~ "Agnostic", | |
religpew == 11 ~ "Nothing in Particular", | |
religpew == 12 ~ "Something Else")) %>% | |
select(caseid, relig1, relig2, weight) %>% | |
as_tibble() %>% | |
filter(relig1 != "NA") %>% | |
filter(relig2 != "NA") | |
small <- small %>% | |
mutate(same = frcode(relig1 == relig2 ~ "Same", | |
TRUE ~ "Not the Same")) | |
graph <- small %>% | |
mutate(switch = case_when(relig1 != relig2 ~ 1, | |
TRUE ~ 0)) %>% | |
group_by(relig1) %>% | |
mean_ci(switch, wt = weight) | |
graph %>% | |
mutate(lab = round(mean, 2)) %>% | |
ggplot(., aes(x = reorder(relig1, mean), y = mean, fill = relig1)) + | |
geom_col(color = "black") + | |
coord_flip() + | |
theme_rb() + | |
error_bar() + | |
scale_fill_manual(values = c("#5f2680", "#FFA700", "#B5B682", "#e1e1e1", "#150023", "#28666e", "#033f63")) + | |
lab_bar(top = FALSE, type = lab, pos = .025, sz = 7) + | |
geom_text(aes(y = .025, label = ifelse(relig1 == "Agnostic", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 7, family = "font", color = "white") + | |
geom_text(aes(y = .025, label = ifelse(relig1 == "Protestant", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 7, family = "font", color = "white") + | |
geom_text(aes(y = .025, label = ifelse(relig1 == "Something Else", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 7, family = "font", color = "white") + | |
geom_text(aes(y = .025, label = ifelse(relig1 == "Nothing in Particular", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 7, family = "font", color = "white") + | |
y_pct() + | |
theme(plot.title = element_text(size = 15)) + | |
labs(x = "", y = "", title = "Share Who Left Their Religious Tradition Between Summer of 2011 and Fall of 2012", caption = "@ryanburge + @religiondata\nData: Putting Politics First, Panel Data (2010-2012)") | |
save("politics_first_switching.png", ht = 4) | |
switch <- small %>% | |
filter(same == "Not the Same") | |
graph <- switch %>% | |
mutate(relig1 = frcode(relig1 == "Protestant" | relig1 == "Catholic" ~ "Prot./Cath.", | |
relig1 == "Other World Religions" ~ "Other World Religions", | |
relig1 == "Atheist" | relig1 == "Agnostic" | relig1 == "Nothing in Particular" ~ "No Religion", | |
relig1 == "Something Else" ~ "Something Else")) %>% | |
mutate(relig2 = frcode(relig2 == "Protestant" | relig2 == "Catholic" ~ "Prot./Cath.", | |
relig2 == "Other World Religions" ~ "Other World Religions", | |
relig2 == "Atheist" | relig2 == "Agnostic" | relig2 == "Nothing in Particular" ~ "No Religion", | |
relig2 == "Something Else" ~ "Something Else")) %>% | |
group_by(relig1) %>% | |
ct(relig2, wt = weight) %>% | |
filter(relig1 != "Other World Religions") | |
graph %>% | |
mutate(lab = round(pct, 2)) %>% | |
ggplot(., aes(x = 1, y = pct, fill = fct_rev(relig2))) + | |
geom_col(color = "black") + | |
coord_flip() + | |
facet_wrap(~ relig1, ncol =1, strip.position = "top") + | |
theme_rb() + | |
scale_fill_manual(values = c("#5f2680", "#FFA700", "#B5B682", "#28666e")) + | |
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(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 9, family = "font", color = "black") + | |
geom_text(aes(label = ifelse(relig2 == "Something Else", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 9, family = "font", color = "white") + | |
geom_text(aes(label = ifelse(relig2 == "Prot./Cath.", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 9, family = "font", color = "white") + | |
theme(plot.title = element_text(size = 16)) + | |
theme(strip.text.x.top = element_text(size = 20)) + | |
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) + | |
labs(x = "", y = "", title = "Among Switchers, Where Do They End Up?", caption = "@ryanburge + @religiondata\nData: Putting Politics First, Panel Data (2010-2012)") | |
save("switchers_politics_first.png", wd = 9, ht = 4.5) | |
regg <- df %>% | |
select(caseid, relig_2, relig_4, weight = weight1_4, educ = ppeduc_2, income = ppincimp_2, gender = ppgender_2, race = ppethm_2, age = i_age, attend = relattend_2, pid3 = partyid1_2) %>% | |
mutate(white = case_when(race == 1 ~ 1, | |
TRUE ~ 0)) %>% | |
mutate(male = case_when(gender == 1 ~ 1, | |
TRUE ~ 0)) %>% | |
mutate(rep = case_when(pid3 == 1 ~ 1, | |
TRUE ~ 0)) %>% | |
mutate(attend = 10 - attend) %>% | |
mutate(religpew = relig_2) %>% | |
mutate(relig1 = frcode(religpew == 1 ~ "Protestant", | |
religpew == 2 ~ "Catholic", | |
religpew >= 3 & religpew <= 8 ~ "Other World Religions", | |
religpew == 9 ~ "Atheist", | |
religpew == 10 ~ "Agnostic", | |
religpew == 11 ~ "Nothing in Particular", | |
religpew == 12 ~ "Something Else")) %>% | |
mutate(religpew = relig_4) %>% | |
mutate(relig2 = frcode(religpew == 1 ~ "Protestant", | |
religpew == 2 ~ "Catholic", | |
religpew >= 3 & religpew <= 8 ~ "Other World Religions", | |
religpew == 9 ~ "Atheist", | |
religpew == 10 ~ "Agnostic", | |
religpew == 11 ~ "Nothing in Particular", | |
religpew == 12 ~ "Something Else")) %>% | |
as_tibble() %>% | |
filter(relig1 != "NA") %>% | |
filter(relig2 != "NA") %>% | |
mutate(switch = case_when(relig1 != relig2 ~ 1, | |
TRUE ~ 0)) %>% | |
select(-relig_2, -relig_4, -religpew, -relig1, -relig2) | |
out <- glm(switch ~ educ + income + male + white + attend + age + rep, family = "binomial", data = regg) | |
library(jtools) | |
coef_names <- c("Age" = "age", | |
"Male" = "male", | |
"White" = "white", | |
"Income" = "income", | |
"Republican" = "rep", | |
"Attendance" = "attend", | |
"Education" = "educ", | |
"No Religion" = "none") | |
gg1 <- plot_summs(out, robust = "HC3", scale = TRUE, coefs = coef_names) | |
gg1 + | |
theme_rb() + | |
add_text(x = -.55, y = 6.5, word = "Less Likely to Switch", sz = 8) + | |
labs(x = "", y = "", title = "What Factors Predict Someone Switching Religions?", caption = "@ryanburge + @religiondata\nData: Putting Politics First, Panel Data (2010-2012)") | |
save("reg_predict_switching.png") | |
graph <- df %>% | |
select(caseid, relig_2, relig_4, weight = weight1_4, educ = ppeduc_2) %>% | |
mutate(religpew = relig_2) %>% | |
mutate(relig1 = frcode(religpew == 1 ~ "Protestant", | |
religpew == 2 ~ "Catholic", | |
religpew >= 3 & religpew <= 8 ~ "Other World Religions", | |
religpew == 9 ~ "Atheist", | |
religpew == 10 ~ "Agnostic", | |
religpew == 11 ~ "Nothing in Particular", | |
religpew == 12 ~ "Something Else")) %>% | |
mutate(religpew = relig_4) %>% | |
mutate(relig2 = frcode(religpew == 1 ~ "Protestant", | |
religpew == 2 ~ "Catholic", | |
religpew >= 3 & religpew <= 8 ~ "Other World Religions", | |
religpew == 9 ~ "Atheist", | |
religpew == 10 ~ "Agnostic", | |
religpew == 11 ~ "Nothing in Particular", | |
religpew == 12 ~ "Something Else")) %>% | |
select(caseid, relig1, relig2, weight, educ) %>% | |
as_tibble() %>% | |
filter(relig1 != "NA") %>% | |
filter(relig2 != "NA") %>% | |
mutate(switch = case_when(relig1 != relig2 ~ 1, | |
TRUE ~ 0)) %>% | |
mutate(educ = frcode(educ <= 9 ~ "HS or Less", | |
educ == 10 | educ == 11 ~ "Some College", | |
educ == 12 ~ "4 Year", | |
educ == 13 | educ == 14 ~ "Graduate School")) %>% | |
group_by(educ) %>% | |
mean_ci(switch, wt = weight, ci = .84) | |
heat <- df %>% | |
select(caseid, attend1 = relattend_2, attend2 = relattend_4, weight = weight1_4) %>% | |
mutate(attend1 = frcode(attend1 == 9 ~ "Never", | |
attend1 == 8 | attend1 == 7 | attend1 == 6 ~ "Yearly", | |
attend1 == 5 | attend1 == 4 ~ "Monthly", | |
attend1 == 3 | attend1 == 2 | attend1 == 1 ~ "Weekly")) %>% | |
mutate(attend2 = frcode(attend2 == 9 ~ "Never", | |
attend2 == 8 | attend2 == 7 | attend2 == 6 ~ "Yearly", | |
attend2 == 5 | attend2 == 4 ~ "Monthly", | |
attend2 == 3 | attend2 == 2 | attend2 == 1 ~ "Weekly")) %>% | |
group_by(attend1) %>% | |
ct(attend2, wt = weight, show_na = FALSE) %>% | |
filter(attend1 != "NA") | |
heat <- heat %>% | |
mutate(bins = frcode(pct < .10 ~ "a", | |
pct > .10 & pct <= .31 ~ "b", | |
pct > .32 ~ "c")) | |
heat %>% | |
mutate(lab = round(pct, 2)) %>% | |
ggplot(., aes(x= attend2, y = attend1)) + | |
geom_tile(aes(fill = bins), color = "black") + | |
scale_fill_manual(values = c("#ADDAB6", "#688081", "#013749")) + | |
theme_rb() + | |
# theme(plot.subtitle = element_text(size = 24)) + | |
geom_text(aes(x= attend2, y = attend1, label = paste0(lab*100, '%')), size = 9, family = "font") + | |
geom_text(aes(x= attend2, y = attend1, label = ifelse(bins == "c", paste0(lab*100, '%'), "")), size = 9, family = "font", color = "white") + | |
theme(strip.text = element_text(size = 20)) + | |
labs(x= "Attendance in 2012", y = "Attendance in 2011", title = "How Does Religious Attendance Change?", | |
caption = "@ryanburge + @religiondata\nData: Putting Politics First, Panel Data (2010-2012)") | |
save('politics_first_att_heat.png', wd = 6, ht = 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment