Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Last active December 19, 2018 16:17
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/1d9f8c80b31a589dc05a03113caac8c0 to your computer and use it in GitHub Desktop.
Save ryanburge/1d9f8c80b31a589dc05a03113caac8c0 to your computer and use it in GitHub Desktop.
Mormons and the Environment
## Intro Stuff ####
## Load packages
library(ggsci)
library(socsci)
library(showtext)
library(car)
library(haven)
cces16 <- read_dta("D://cces/data/cces16.dta")
## This a quick template function I use for my plots
theme_gg <- function(fff, base_size = 44, base_family = "font")
{
font_add_google(fff, "font")
showtext_auto()
theme_minimal() +
theme(legend.position = "none") +
theme(legend.title = element_blank()) +
theme(text=element_text(size=44, family="font"))
}
## Load your CCES Data call it cces16
## Then run the evangelical RELTRAD coding that can be found here: https://github.com/ryanburge/reltrad/blob/master/CCES/reltrad16.R
## Concern for the Environment Bar Graph ####
aa1 <- cces16 %>%
filter(religpew == 3) %>%
filter(CC16_301h < 6) %>%
ct(CC16_301h, wt = commonweight_vv_post) %>%
mutate(group = "Mormons")
aa2 <- cces16 %>%
filter(evangelical ==1) %>%
filter(CC16_301h < 6) %>%
ct(CC16_301h, wt = commonweight_vv_post) %>%
mutate(group = "Evangelicals")
aa3 <- cces16 %>%
filter(pid7 == 5 | pid7 == 6 | pid7 ==7) %>%
filter(CC16_301h < 6) %>%
ct(CC16_301h, wt = commonweight_vv_post) %>%
mutate(group = "Republicans")
graph <- bind_df("aa")
graph <- graph %>%
mutate(env = frcode(CC16_301h == 5 ~ "No Importance",
CC16_301h == 4 ~ "Very Low",
CC16_301h == 3 ~ "Somewhat Low",
CC16_301h == 2 ~ "Somewhat High",
CC16_301h == 1 ~ "Very High",
TRUE ~ "REMOVE"))
graph %>%
ggplot(., aes(x= env, y = pct, fill = group)) +
geom_col(color = "black") +
theme_gg("Abel") +
facet_grid(~ group) +
scale_fill_jama() +
scale_y_continuous(labels = percent) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
geom_text(aes(y = pct + .015, label = paste0(pct*100, '%')), position = position_dodge(width = .9), size = 10, family = "font") +
labs(x = "", y = "", title = "How Important is the Environment to You?" , caption = "Data: CCES 2016") +
ggsave("D://mormons_imp_env.png", width = 7)
## I just wanted to check the means for all three
aa1 <- cces16 %>%
filter(religpew == 3) %>%
filter(CC16_301h < 6) %>%
mean_ci(CC16_301h, wt = commonweight_vv_post) %>%
mutate(group = "Mormons")
aa2 <- cces16 %>%
filter(evangelical ==1) %>%
filter(CC16_301h < 6) %>%
mean_ci(CC16_301h, wt = commonweight_vv_post) %>%
mutate(group = "Evangelicals")
aa3 <- cces16 %>%
filter(pid7 == 5 | pid7 == 6 | pid7 ==7) %>%
filter(CC16_301h < 6) %>%
mean_ci(CC16_301h, wt = commonweight_vv_post) %>%
mutate(group = "Republicans")
bind_df("aa")
## 1 is Very Concerned, 5 is No Concern at All
#
# mean sd n se lower upper group
# 1 2.569200 1.255490 182 0.09306311 2.385572 2.752828 Mormons
# 2 2.606419 1.214000 2797 0.02295475 2.561409 2.651428 Evangelicals
# 3 2.939406 1.173163 4756 0.01701130 2.906056 2.972756 Republicans
### Making the Env Protections Dots Plots ####
## Making a quick Republican dichotomous variable
cces16 <- cces16 %>%
mutate(repub = recode(pid7, "5:7=1; else =0"))
## Need to make a function to do this analysis quickly.
compare <- function(df, var1, des){
var1 <- enquo(var1)
df1 <- df %>%
mutate(env = recode(!! var1, "1=1; 2=0; else = NA")) %>%
filter(religpew ==3) %>%
mean_ci(env) %>%
mutate(group = "Mormons")
df2 <- df %>%
mutate(env = recode(!! var1, "1=1; 2=0; else = NA")) %>%
filter(evangelical ==1) %>%
mean_ci(env) %>%
mutate(group = "Evangelicals")
df3 <- df %>%
mutate(env = recode(!! var1, "1=1; 2=0; else = NA")) %>%
filter(repub ==1) %>%
mean_ci(env) %>%
mutate(group = "Republicans")
df4 <- bind_rows(df1, df2, df3) %>%
mutate(ques = des)
}
## Running the function for the four questions
bb1 <- cces16 %>% compare(CC16_333a, "Give EPA Power over CO2")
bb2 <- cces16 %>% compare(CC16_333b, "Raise Fuel Efficiency")
bb3 <- cces16 %>% compare(CC16_333c, "Require Green Fuels")
bb4 <- cces16 %>% compare(CC16_333d, "Strengthen Clear Air/Water Acts")
## Binding
graph <- bind_df("bb") %>% as.tibble()
## Graphing
graph %>%
ggplot(., aes(y=mean, x= fct_reorder(ques, mean), color = group)) +
geom_point(position=position_dodge(width=0.5), size =3) +
geom_errorbar(aes(ymin = lower, ymax=upper), position=position_dodge(0.5), size = 1) +
coord_flip() +
theme_gg("Abel") +
labs(title = "Mormons' Views on Environmental Issues", x = "Issue Area", y = "Percent in Favor", caption = "Data: CCES 2016") +
scale_y_continuous(labels = percent, limits = c(.25, .7)) +
scale_color_jama() +
theme(legend.position = "bottom") +
theme(legend.title=element_blank()) +
ggsave("D://mormon_env_dots.png", width = 8, height = 4)
## Comparing Mormons to Everyone Else ####
## Writing a function to compute the mean for all four questions
fun <- function(df, relig, fam){
relig <- enquo(relig)
fam <- enquo(fam)
df %>%
group_by(!! relig) %>%
mutate(env1 = recode(CC16_333a, "1=1; 2=0; else = NA")) %>%
mutate(env2 = recode(CC16_333b, "1=1; 2=0; else = NA")) %>%
mutate(env3 = recode(CC16_333c, "1=1; 2=0; else = NA")) %>%
mutate(env4 = recode(CC16_333a, "1=1; 2=0; else = NA")) %>%
mutate(envir = env1 + env2 + env3 + env4) %>%
mean_ci(envir) %>%
filter(n > 100) %>%
mutate(relig = to_factor(!! relig)) %>%
filter(relig != "Skipped") %>%
select(relig, mean, sd, n, level, se, lower, upper) %>%
mutate(family = !! fam)
}
## Running this for all the Protestant Groups
aa <- fun(cces16, religpew_methodist, "Methodist")
aa1 <- fun(cces16, religpew_baptist, "Baptist")
aa2 <- fun(cces16, religpew_nondenom, "Non-Denom")
aa3 <- fun(cces16, religpew_lutheran, "Lutheran")
aa4 <- fun(cces16, religpew_presby, "Presbyterian")
aa5 <- fun(cces16, religpew_pentecost, "Pentecostal")
aa6 <- fun(cces16, religpew_episcop, "Episcopal")
aa7 <- fun(cces16, religpew_congreg, "Congregational")
aa8 <- fun(cces16, religpew_holiness, "Holiness")
aa9 <- fun(cces16, religpew_reformed, "Reformed")
## Now running it for all the larger traditions
aa10 <- cces16 %>%
group_by(religpew) %>%
mutate(env1 = recode(CC16_333a, "1=1; 2=0; else = NA")) %>%
mutate(env2 = recode(CC16_333b, "1=1; 2=0; else = NA")) %>%
mutate(env3 = recode(CC16_333c, "1=1; 2=0; else = NA")) %>%
mutate(env4 = recode(CC16_333a, "1=1; 2=0; else = NA")) %>%
mutate(envir = env1 + env2 + env3 + env4) %>%
mean_ci(envir) %>%
filter(n > 100) %>%
mutate(relig = to_factor(religpew)) %>%
filter(relig != "Protestant") %>%
select(-religpew)
## Adding a family column by hand
aa10$family <- c("Catholic", "Mormon", "Orthodox", "Jewish", "Other Religion", "Other Religion", "Other Religion", "None", "None", "None", "Other Religion")
## Binding everything together, but then I want to give the Mormon column a different color
## So there is a column with 1 for the Mormon, and zero for everyon else
graph <- bind_df("aa") %>%
mutate(color = case_when(relig == "Mormon" ~ 1,
TRUE ~ 0 ))
graph %>%
ggplot(., aes(y=mean, x= fct_reorder(relig, mean), color = factor(color))) +
geom_point(size = 2) +
geom_errorbar(aes(ymin = lower, ymax=upper), size = 1) +
coord_flip() +
scale_color_manual(values = c("azure4", "firebrick3")) +
theme_gg("Abel") +
labs(title = "Support for Environmental Protection by Religious Tradition", y = "<--- Less Support for the Environment:More Support for the Environment --->", x = "Religious Tradition", caption = "Data: CCES 2016") +
theme(legend.position = "none") +
theme(legend.title=element_blank()) +
theme(plot.title = element_text(size = 64)) +
geom_hline(yintercept = 2.70, linetype = "dashed") +
annotate("text", x=48, y = 2.55, label = "Sample Avg.", size = 16, family = "font") +
annotate("text", x=46, y = 2.62, label = "2.70", size = 16, family = "font") +
ggsave("D://mormon_env_dots_all.png", width = 12, height = 8)
## I calculated the mean for the entire sample -- so I can shoot a vertical line in the graph to compare the groups to.
cces16 %>%
mutate(env1 = recode(CC16_333a, "1=1; 2=0; else = NA")) %>%
mutate(env2 = recode(CC16_333b, "1=1; 2=0; else = NA")) %>%
mutate(env3 = recode(CC16_333c, "1=1; 2=0; else = NA")) %>%
mutate(env4 = recode(CC16_333a, "1=1; 2=0; else = NA")) %>%
mutate(envir = env1 + env2 + env3 + env4) %>%
mean_ci(envir)
#
# mean sd n level se lower upper
# <dbl> <dbl> <int> <dbl> <dbl> <dbl> <dbl>
# 1 2.70 1.53 64600 0.05 0.00603 2.69 2.71
## Making the Last Graph Comparing by PID ####
graph1 <- cces16 %>%
filter(religpew == 3) %>%
filter(pid3 <= 3) %>%
mutate(env1 = recode(CC16_333a, "1=1; 2=0; else = NA")) %>%
mutate(env2 = recode(CC16_333b, "1=1; 2=0; else = NA")) %>%
mutate(env3 = recode(CC16_333c, "1=1; 2=0; else = NA")) %>%
mutate(env4 = recode(CC16_333a, "1=1; 2=0; else = NA")) %>%
mutate(envir = env1 + env2 + env3 + env4) %>%
group_by(pid3) %>%
mean_ci(envir) %>%
mutate(group = "Mormons")
graph2 <- cces16 %>%
filter(evangelical == 1) %>%
filter(pid3 <= 3) %>%
mutate(env1 = recode(CC16_333a, "1=1; 2=0; else = NA")) %>%
mutate(env2 = recode(CC16_333b, "1=1; 2=0; else = NA")) %>%
mutate(env3 = recode(CC16_333c, "1=1; 2=0; else = NA")) %>%
mutate(env4 = recode(CC16_333a, "1=1; 2=0; else = NA")) %>%
mutate(envir = env1 + env2 + env3 + env4) %>%
group_by(pid3) %>%
mean_ci(envir) %>%
mutate(group = "Evangelicals")
graph <- bind_rows(graph1, graph2)
graph <- graph %>%
mutate(pid3 = frcode(pid3 ==1 ~ "Democrat",
pid3 ==2 ~ "Independent",
pid3 ==3 ~ "Republican"))
graph %>%
ggplot(., aes(y=mean, x= pid3, color = pid3)) +
geom_point(size = 2) +
geom_errorbar(aes(ymin = lower, ymax=upper), size = 1, width = .25) +
coord_flip() +
facet_wrap(~ group, ncol = 1) +
scale_color_manual(values = c("dodgerblue3", "azure4", "firebrick3")) +
theme_gg("Abel") +
scale_y_continuous(limits = c(1,4)) +
labs(title = "Support for Environmental Protection by Party ID", y = "<--- Less Support for the Environment:More Support for the Environment --->", x = "Religious Tradition", subtitle = "", caption = "Data: CCES 2016") +
theme(legend.position = "none") +
theme(legend.title=element_blank()) +
theme(plot.title = element_text(size = 64)) +
ggsave("D://mormon_3pid.png", width = 12, height = 6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment