library(socsci)
library(car)
library(janitor)
library(knitr)
library(DT)
library(showtext)
library(Cairo)
source("D://theme.R")
barna <- read_csv("D://bb2.csv") %>% clean_names()graph <- barna %>%
mutate(pid3 = frcode(s20 == 2 ~ "Democrat",
s20 == 3 ~ "Independent",
s20 == 1 ~ "Republican")) %>%
mutate(percept = frcode(q24 == 5 | q24 == 4 ~ "Negative",
q24 == 3 ~ "Neutral",
q24 == 1 | q24 == 2 ~ "Positive")) %>%
group_by(pid3) %>%
ct(percept, show_na = FALSE, wt = weight) %>%
na.omit()
graph %>%
ggplot(., aes(x=1, y = pct, fill = fct_rev(percept))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ pid3, ncol =1) +
theme_gg("Abel") +
theme(legend.position = "bottom") +
scale_fill_manual(values = c("#80BD9D", "#F98866", "#FF420E")) +
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 = "black") +
theme(panel.grid.major.y = element_blank()) +
theme(panel.grid.minor.y = element_blank()) +
theme(plot.title = element_text(size = 16)) +
labs(x= "Perception of Evangelicals", y = "Political Partisanship", title = "Perception of Evangelicals Depends on Partisanship", caption = "") pid_fun <- function(df, ques, name){
ques <- enquo(ques)
df %>%
mutate(pid3 = frcode(s20 == 2 ~ "Dem",
s20 == 3 ~ "Ind",
s20 == 1 ~ "Rep")) %>%
group_by(pid3) %>%
ct(!! ques, wt = weight) %>%
mutate(type = name) %>%
na.omit() %>%
filter(!! ques == 1) %>%
select(pid3, n, pct, type)
}
qqq1 <- barna %>% pid_fun(q23_1, "Political Con")
qqq2 <- barna %>% pid_fun(q23_2, "Religious Con")
qqq3 <- barna %>% pid_fun(q23_3, "Racist")
qqq4 <- barna %>% pid_fun(q23_4, "Misogynistic")
qqq5 <- barna %>% pid_fun(q23_5, "Caring")
qqq6 <- barna %>% pid_fun(q23_6, "Invasive")
qqq7 <- barna %>% pid_fun(q23_7, "Friendly")
qqq8 <- barna %>% pid_fun(q23_8, "Unhappy")
qqq9 <- barna %>% pid_fun(q23_9, "Encouraging")
qqq10 <- barna %>% pid_fun(q23_10, "Hopeful")
qqq11 <- barna %>% pid_fun(q23_11, "Uptight")
qqq12 <- barna %>% pid_fun(q23_12, "Good-humored")
qqq13 <- barna %>% pid_fun(q23_13, "Foolish")
qqq14 <- barna %>% pid_fun(q23_14, "Generous")
qqq15 <- barna %>% pid_fun(q23_15, "Selfish")
qqq16 <- barna %>% pid_fun(q23_16, "Hurtful")
qqq17 <- barna %>% pid_fun(q23_17, "Homophobic")
qqq18 <- barna %>% pid_fun(q23_18, "Narrow Minded")
qqq19 <- barna %>% pid_fun(q23_19, "Puritanical")
qqq20 <- barna %>% pid_fun(q23_20, "Misunderstood")
graph <- bind_df("qqq")
graph %>%
ggplot(., aes(x = pid3, y = pct, fill = pid3)) +
geom_col(color = "black") +
facet_wrap(~ type) +
pid3_fill() +
y_pct() +
theme_gg("Abel") +
geom_text(aes(y = pct + .035, label = paste0(pct*100, '%')), position = position_dodge(width = .9), size = 3, family = "font") +
labs(x = "", y = "", title = "Traits of Evangelicals by Partisanship", caption = "") graph <- barna %>%
filter(q22 != 11) %>%
mutate(describe = frcode(q22 == 1 ~ "Christian",
q22 == 2 ~ "Republican",
q22 == 3 ~ "Republican\nChristian",
q22 == 4 ~ "Conservative\nChristian",
q22 == 5 ~ "White\nChristian",
q22 == 6 ~ "Protestant\nChristian",
q22 == 7 ~ "White Rep.\nProt. Christian",
q22 == 8 ~ "Born-again\nChristian",
q22 == 9 ~ "Christian who shares\ntheir faith with others",
q22 == 10 ~ "Christian who subscribes to\na certain set of beliefs")) %>%
ct(describe, wt = weight) %>%
mutate(describe = fct_reorder(describe, pct))
graph %>%
ggplot(., aes(x = describe, y = pct, fill = pct)) +
geom_col(color = "black") +
coord_flip() +
scale_fill_gradient(low = "#C02425", high = "#F0CB35") +
theme_gg("Abel") +
y_pct() +
geom_text(aes(y = pct + .015, label = paste0(pct*100, '%')), position = position_dodge(width = .9), size = 4, family = "font") +
labs(x = "", y = "", title = "Which Best Describes What an 'Evangelical' Is?", caption = "") graph <- barna %>%
filter(q22 != 11) %>%
mutate(describe = frcode(q22 == 1 ~ "Christian",
q22 == 2 ~ "Republican",
q22 == 3 ~ "Republican Christian",
q22 == 4 ~ "Conservative Christian",
q22 == 5 ~ "White Christian",
q22 == 6 ~ "Protestant Christian",
q22 == 7 ~ "White Rep. Prot. Christian",
q22 == 8 ~ "Born-again Christian",
q22 == 9 ~ "Christian who shares their faith with others",
q22 == 10 ~ "Christian who subscribes to a certain set of beliefs")) %>%
ct(describe, wt = weight) %>%
mutate(describe = fct_reorder(describe, pct))
graph %>%
DT::datatable(
colnames = c("Descriptor", "Count", "Percentage"),
extensions = 'Buttons',
options = list(dom = 'Brti',
buttons = c('excel', "csv")))