Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created February 20, 2019 00:36
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/d92001f4c3137402364b78d94c32ea40 to your computer and use it in GitHub Desktop.
Save ryanburge/d92001f4c3137402364b78d94c32ea40 to your computer and use it in GitHub Desktop.
United Methodist SSM
## Single Bar Graph ####
graph <- cces16 %>%
filter(religpew_methodist ==1) %>%
filter(CC16_335 != 8) %>%
ct(CC16_335, wt = commonweight_vv) %>%
mutate(gaym = frcode(CC16_335 == 1 ~ "Favor",
CC16_335 == 2 ~ "Oppose"))
graph %>%
ggplot(., aes(x =1, y = pct, fill = gaym)) +
geom_col(color = "black") +
coord_flip() +
scale_y_continuous(labels = percent) +
theme_gg("Overpass") +
scale_x_continuous(breaks = NULL) +
theme(axis.title.y=element_blank(), axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
labs(x = "", y = "", title = "Support for Same Sex Marriage", subtitle = "Among United Methodists", caption = "Data: CCES 2016") +
scale_fill_manual(values = c("#7a2426", "#8e9300")) +
geom_text(aes(label = paste0(pct*100, '%')), position = position_stack(vjust = 0.5), size = 6, family = "font") +
annotate("text", x= .75, y = .203, label = "Oppose", size = 4, family = "font") +
annotate("text", x= .75, y = .703, label = "Favor", size = 4, family = "font") +
ggsave("D://cces/images/umc_gay_onebar.png", height =2)
## Bar Graph by Age ####
graph <- cces16 %>%
filter(religpew_methodist ==1) %>%
mutate(age = 2016 - birthyr) %>%
mutate(age2 = frcode(age >= 18 & age <= 35 ~ "18 to 35",
age >= 36 & age <= 49 ~ "36 to 49",
age >= 50 & age <= 59 ~ "50 to 59",
age >= 60 & age <= 75 ~ "60 to 75",
age >= 76 & age <= 100 ~ "76 and Over")) %>%
mutate(gaym = car::recode(CC16_335, "1=1; 2=0; else = NA")) %>%
group_by(age2) %>%
mean_ci(gaym, ci = .84)
graph %>%
ggplot(., aes(x = age2, y = mean, fill = mean)) +
geom_col(color = "black") +
scale_y_continuous(labels = percent) +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.2, position=position_dodge(.9)) +
theme_gg("Overpass") +
scale_fill_gradient(low = "#8e9300", high = "#7a2426") +
geom_text(aes(y = .25, label = paste0(mean*100, '%')), position = position_stack(vjust = 0.5), size = 6, family = "font") +
theme(plot.title = element_text(size = 16)) +
labs(x = "", y = "", title = "Support for Gay Marriage among United Methodists", caption = "Data: CCES 2016") +
ggsave("D://cces/images/umc_age2_gaym.png")
## Doing the margins plots ed*att by region ####
cces16 <- cces16 %>% mutate(region=car::Recode(inputstate, "1=3;
2=4;
4=4;
5=3;
6=4;
8=4;
9=1;
10=1;
11=1;
12=3;
13=3;
15=4;
16=4;
17=2;
18=2;
19=2;
20=2;
21=3;
22=3;
23=1;
24=1;
25=1;
26=2;
27=2;
28=3;
29=2;
30=4;
31=2;
32=4;
33=1;
34=1;
35=4;
36=1;
37=3;
38=2;
39=2;
40=3;
41=4;
42=2;
44=1;
45=3;
46=2;
47=3;
48=3;
49=4;
50=1;
51=3;
53=4;
54=3;
55=2;
56=4"))
reg <- cces16 %>%
filter(religpew_methodist ==1) %>%
mutate(gaym = car::recode(CC16_335, "1=1; 2=0; else = NA")) %>%
mutate(att = car::recode(pew_churatd, "1=6; 2=5; 3=4; 4=3; 5=2; 6=1; else= NA")) %>%
mutate(college = car::recode(educ, "5:6=1; 1:4=0; else = NA"))
gg <- glm(gaym ~ att*college*region, family = "binomial", data = reg)
gg2 <- interact_plot(gg, pred= att, modx = college, mod2 = region, int.width = .76, interval = TRUE, mod2.values = c(1,2,3,4), modx.labels = c("No College Degree", "College Degree"), mod2.labels = c("New England", "Midwest", "South", "West"))
gg2 +
labs(x = "Church Attendance", y = "Support for Gay Marriage", title = "Interaction of Education and Attendance on Support for SSM", subtitle = "Among United Methodists") +
theme_gg("Overpass") +
scale_y_continuous(labels = percent, limits = c(0,1.05)) +
scale_color_manual(values = c("#e4002b", "#b5b7b4", "#51626f")) +
scale_fill_manual(values = c("#e4002b", "#b5b7b4", "#51626f")) +
theme(legend.position = c(.75,.05)) +
scale_x_continuous(limits = c(1,6), breaks = c(1,2,3,4,5,6), labels = c("Never", "Seldom", "Yearly", "Monthly", "Weekly", "Weekly+")) +
theme(plot.title = element_text(size = 12)) +
theme(plot.subtitle = element_text(size = 10)) +
ggsave("D://cces/images/jtools_umc_ne1.png", width = 10, height = 10)
## Issue Importance ####
compare <- function(df, var, ques){
var <- enquo(var)
ques <- enquo(ques)
df %>%
filter(religpew_methodist ==1) %>%
mutate(newvar = recode(!! var, "1=5; 2=4; 3=3; 4=2; 5=1; else =99")) %>%
mutate(att = car::recode(pew_churatd, "1=6; 2=5; 3=4; 4=3; 5=2; 6=1; else= NA")) %>%
mutate(att3 = frcode(att == 1 | att == 2 ~ "Never",
att == 3 | att == 4 ~ "Seldom",
att == 5 | att == 6 ~ "Frequent")) %>%
filter(newvar != 99) %>%
group_by(att3) %>%
mean_ci(newvar) %>%
mutate(topic = !! ques)
}
ee1 <- cces16 %>% compare(CC16_301a, "Gun Control")
ee2 <- cces16 %>% compare(CC16_301b, "Abortion")
ee3 <- cces16 %>% compare(CC16_301c, "Taxes")
ee4 <- cces16 %>% compare(CC16_301d, "Immigration")
ee5 <- cces16 %>% compare(CC16_301e, "Budget Deficit")
ee6 <- cces16 %>% compare(CC16_301f, "Defense Spending")
ee7 <- cces16 %>% compare(CC16_301g, "Social Security")
ee8 <- cces16 %>% compare(CC16_301h, "Environment")
ee9 <- cces16 %>% compare(CC16_301i, "Jobs")
ee10 <- cces16 %>% compare(CC16_301j, "Crime")
ee11 <- cces16 %>% compare(CC16_301k, "National Security")
ee12 <- cces16 %>% compare(CC16_301l, "Race Relations")
ee13 <- cces16 %>% compare(CC16_301m, "Health Care")
ee14 <- cces16 %>% compare(CC16_301n, "Gay Marriage")
ee15 <- cces16 %>% compare(CC16_301o, "Govt. Corruption")
graph <- bind_df("ee") %>% na.omit()
graph %>%
ggplot(., aes(y=mean, x= fct_reorder(topic, mean), color = att3)) +
geom_point(position=position_dodge(width=0.5), size =4) +
geom_errorbar(aes(ymin = lower, ymax=upper), position=position_dodge(0.5), size = 1) +
coord_flip() +
theme_gg("Overpass") +
labs(title = "Issue Importance Among United Methodists", x = "Issue Area", y = "Level of Importance", subtitle = "Broken Down by Church Attendance", caption = "Data: CCES 2016") +
scale_y_continuous(limits = c(0.85,5.05), breaks = c(1,2,3,4,5), labels = c("No Importance", "Very Low", "Somewhat Low", "Somewhat High", "Very High")) +
scale_color_manual(values = c("#e4002b", "#b5b7b4", "#51626f")) +
theme(legend.position = "bottom") +
theme(legend.title=element_blank()) +
theme(text=element_text(size=18, family="font")) +
geom_vline(xintercept = 1.5, linetype = "dotdash") +
geom_vline(xintercept = 2.5, linetype = "dotdash") +
geom_vline(xintercept = 3.5, linetype = "dotdash") +
geom_vline(xintercept = 4.5, linetype = "dotdash") +
geom_vline(xintercept = 5.5, linetype = "dotdash") +
geom_vline(xintercept = 6.5, linetype = "dotdash") +
geom_vline(xintercept = 7.5, linetype = "dotdash") +
geom_vline(xintercept = 8.5, linetype = "dotdash") +
geom_vline(xintercept = 9.5, linetype = "dotdash") +
geom_vline(xintercept = 10.5, linetype = "dotdash") +
geom_vline(xintercept = 11.5, linetype = "dotdash") +
geom_vline(xintercept = 12.5, linetype = "dotdash") +
geom_vline(xintercept = 13.5, linetype = "dotdash") +
geom_vline(xintercept = 14.5, linetype = "dotdash") +
ggsave("D://cces/images/umc_issue_import.png", width = 14, height = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment