Guest User

Untitled

a guest
Dec 9th, 2018
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.37 KB | None | 0 0
  1. library(socsci)
  2. library(haven)
  3.  
  4. sbc <- gss %>%
  5. mutate(abort = recode(abany, "1=1; 2=0; else = 99")) %>%
  6. # filter(evangelical ==1) %>%
  7. group_by(year) %>%
  8. filter(abort != 99) %>%
  9. mean_ci(abort)
  10.  
  11.  
  12.  
  13. ggplot(sbc, aes(x=year, y=mean)) +
  14. geom_line(size = 1) +
  15. geom_point(colour="cornflowerblue", size=2, stroke=2)+
  16. geom_smooth() +
  17. theme_minimal() +
  18. theme_gg("Abel") +
  19. scale_y_continuous(limits=c(.28,.55), labels = scales::percent) +
  20. labs(x = "Year", y = "Percent in Favor", title = "Allow An Abortion If The Woman Wants It For Any Reason", caption = "Data: GSS (1977-2016)") +
  21. ggsave("D://abort_gss_overall.png", width = 8, height = 6)
  22.  
  23.  
  24.  
  25. ## Gay Marriage Reltrad Bars ####
  26.  
  27.  
  28. gay <- gss %>%
  29. filter(marhomo != "NA") %>%
  30. filter(year == 2016) %>%
  31. mutate(reltrad = frcode(reltrad == 1 ~ "Evangelicals",
  32. reltrad == 2 ~ "Black Protestant",
  33. reltrad == 3 ~ "Mainline",
  34. reltrad == 4 ~ "Catholic",
  35. reltrad == 5 ~ "Jewish",
  36. reltrad == 6 ~ "Other Faith",
  37. reltrad == 7 ~ "No Faith")) %>%
  38. group_by(reltrad) %>%
  39. ct(marhomo) %>%
  40. ungroup(reltrad) %>%
  41. mutate(gaym = frcode(marhomo == 5 ~ "Strongly disagree",
  42. marhomo == 4 ~ "Disagree",
  43. marhomo == 3 ~ "Neither Agree or Disagree",
  44. marhomo == 2 ~ "Agree",
  45. marhomo == 1 ~ "Strongly agree",
  46. TRUE ~ "REMOVE"))
  47.  
  48.  
  49. gay %>%
  50. filter(reltrad != "NA") %>%
  51. ggplot(., aes(x=gaym, y = pct, fill = reltrad)) +
  52. geom_col(color = "black") +
  53. facet_wrap(~ reltrad, ncol = 4) +
  54. theme_gg("Abel") +
  55. scale_y_continuous(labels = scales::percent) +
  56. theme(axis.text.x = element_text(angle = 45, hjust =1, size = 24)) +
  57. geom_text(aes(y = pct + .045, label = paste0(pct*100, '%')), position = position_dodge(width = .9), size = 10, family = "font") +
  58. scale_fill_paletteer_d(ggthemes, calc) +
  59. labs(x = "Homosexual couples should have the right to marry one another", y = "", title = "Support for Gay Marriage by Religious Tradition", caption = "Data: GSS 2016") +
  60. ggsave("D://gay_m_reltrad.png")
  61.  
  62. ## Two generations ribbons ####
  63.  
  64. ggg1 <- gss %>%
  65. filter(marhomo != "NA") %>%
  66. mutate(gaym = recode(marhomo, "1=5; 2=4; 3=3; 4=2; 5=1; else = NA")) %>%
  67. filter(evangelical == 1) %>%
  68. filter(race ==1) %>%
  69. mutate(age2 = recode(age, "18:35 = '35 and Under'; 36:100 = 'Over 35'")) %>%
  70. group_by(year, age2) %>%
  71. mean_ci(gaym) %>%
  72. mutate(group = "White Evangelicals") %>%
  73. na.omit()
  74.  
  75. ggg2 <- gss %>%
  76. filter(marhomo != "NA") %>%
  77. mutate(gaym = recode(marhomo, "1=5; 2=4; 3=3; 4=2; 5=1; else = NA")) %>%
  78. mutate(age2 = recode(age, "18:35 = '35 and Under'; 36:100 = 'Over 35'")) %>%
  79. group_by(year, age2) %>%
  80. mean_ci(gaym) %>%
  81. mutate(group = "Entire Sample") %>%
  82. na.omit()
  83.  
  84.  
  85. gss %>%
  86. filter(marhomo != "NA") %>%
  87. mutate(gaym = recode(marhomo, "1=5; 2=4; 3=3; 4=2; 5=1; else = NA")) %>%
  88. group_by(year) %>%
  89. mean_ci(gaym) %>%
  90. mutate(group = "Entire Sample") %>%
  91. na.omit()
  92.  
  93. graph <- bind_df("ggg")
  94.  
  95. graph <- graph %>%
  96. mutate(g2 = paste(age2, group, sep = " - "))
  97.  
  98. graph %>%
  99. filter(year > 2000) %>%
  100. ggplot(., aes(x = year, y = mean, group = g2, color = g2)) +
  101. geom_line(size = 1) +
  102. geom_ribbon(aes(ymin=lower, ymax=upper, color = g2, fill = g2), alpha = .4, show.legend = FALSE) +
  103. theme_gg("Abel") +
  104. scale_y_continuous(breaks = c(1,2,3,4,5), labels = c("Strongly Disagree", "Disagree", "Neither Agr. or Dis.", "Agree", "Strongly Agree")) +
  105. theme(legend.position = c(0.8, 0.1)) +
  106. scale_fill_paletteer_d(ggthemes, calc) +
  107. scale_color_paletteer_d(ggthemes, calc) +
  108. labs(x = "Church Attendance", y = "Support for Gay Marriage", title = "Younger Evangelicals are Rapidly Accepting SSM", caption = "Data: GSS 2004-2016") +
  109. ggsave("D://gay_2ages.png", width = 8)
  110.  
  111. ### Attendance Ribbons ####
  112.  
  113.  
  114. aaa1 <- cces16 %>%
  115. filter(evangelical ==1) %>%
  116. filter(race ==1) %>%
  117. mutate(gaym = recode(CC16_335, "1=1; 2=0; else = NA")) %>%
  118. mutate(attend = frcode(pew_churatd == 6 ~ "Never",
  119. pew_churatd == 5 ~ "Seldom",
  120. pew_churatd == 4 ~ "Yearly",
  121. pew_churatd == 3 ~ "Monthly",
  122. pew_churatd == 2 ~ "Weekly",
  123. pew_churatd == 1 ~ "Weekly+",
  124. TRUE ~ "REMOVE")) %>%
  125. group_by(attend) %>%
  126. mean_ci(gaym) %>%
  127. mutate(group = "White Evangelicals")
  128.  
  129. aaa2 <- cces16 %>%
  130. filter(catholic ==1) %>%
  131. mutate(gaym = recode(CC16_335, "1=1; 2=0; else = NA")) %>%
  132. mutate(attend = frcode(pew_churatd == 6 ~ "Never",
  133. pew_churatd == 5 ~ "Seldom",
  134. pew_churatd == 4 ~ "Yearly",
  135. pew_churatd == 3 ~ "Monthly",
  136. pew_churatd == 2 ~ "Weekly",
  137. pew_churatd == 1 ~ "Weekly+",
  138. TRUE ~ "REMOVE")) %>%
  139. group_by(attend) %>%
  140. mean_ci(gaym) %>%
  141. mutate(group = "Catholics")
  142.  
  143. aaa3 <- cces16 %>%
  144. filter(mainline ==1) %>%
  145. mutate(gaym = recode(CC16_335, "1=1; 2=0; else = NA")) %>%
  146. mutate(attend = frcode(pew_churatd == 6 ~ "Never",
  147. pew_churatd == 5 ~ "Seldom",
  148. pew_churatd == 4 ~ "Yearly",
  149. pew_churatd == 3 ~ "Monthly",
  150. pew_churatd == 2 ~ "Weekly",
  151. pew_churatd == 1 ~ "Weekly+",
  152. TRUE ~ "REMOVE")) %>%
  153. group_by(attend) %>%
  154. mean_ci(gaym) %>%
  155. mutate(group = "Mainline")
  156.  
  157.  
  158. aaa4 <- cces16 %>%
  159. mutate(gaym = recode(CC16_335, "1=1; 2=0; else = NA")) %>%
  160. mutate(attend = frcode(pew_churatd == 6 ~ "Never",
  161. pew_churatd == 5 ~ "Seldom",
  162. pew_churatd == 4 ~ "Yearly",
  163. pew_churatd == 3 ~ "Monthly",
  164. pew_churatd == 2 ~ "Weekly",
  165. pew_churatd == 1 ~ "Weekly+",
  166. TRUE ~ "REMOVE")) %>%
  167. group_by(attend) %>%
  168. mean_ci(gaym) %>%
  169. mutate(group = "Entire Sample")
  170.  
  171. graph <- bind_df("aaa")
  172.  
  173. graph %>%
  174. filter(attend != "REMOVE") %>%
  175. ggplot(., aes(x= attend, y = mean, group = group, color = group)) +
  176. geom_line() +
  177. geom_ribbon(aes(ymin=lower, ymax=upper, color = group, fill = group), alpha = .4, show.legend = FALSE) +
  178. theme_gg("Abel") +
  179. scale_y_continuous(labels = scales::percent) +
  180. theme(legend.position = c(0.4, 0.2)) +
  181. scale_fill_paletteer_d(ggthemes, calc) +
  182. scale_color_paletteer_d(ggthemes, calc) +
  183. labs(x = "Church Attendance", y = "Support for Gay Marriage", title = "Frequent Church Attendance is Related to Lower Support for SSM", caption = "Data: CCES 2016") +
  184. ggsave("D://gay_attend_reltrad.png", width = 8)
  185.  
  186.  
  187. ## Dumbbells of Support Changes ####
  188.  
  189. fun <- function(df, relig, fam){
  190. relig <- enquo(relig)
  191. fam <- enquo(fam)
  192.  
  193. df %>%
  194. group_by(!! relig) %>%
  195. mutate(gaym = recode(CC16_335, "1=1; 2=0; else = NA")) %>%
  196. mean_ci(gaym) %>%
  197. filter(n > 100) %>%
  198. mutate(relig = to_factor(!! relig)) %>%
  199. filter(relig != "Skipped") %>%
  200. select(relig, mean, sd, n, level, se, lower, upper) %>%
  201. mutate(family = !! fam)
  202.  
  203.  
  204. }
  205.  
  206. aa <- fun(cces16, religpew_methodist, "Methodist")
  207. aa1 <- fun(cces16, religpew_baptist, "Baptist")
  208. aa2 <- fun(cces16, religpew_nondenom, "Non-Denom")
  209. aa3 <- fun(cces16, religpew_lutheran, "Lutheran")
  210. aa4 <- fun(cces16, religpew_presby, "Presbyterian")
  211. aa5 <- fun(cces16, religpew_pentecost, "Pentecostal")
  212. aa6 <- fun(cces16, religpew_episcop, "Episcopal")
  213. aa7 <- fun(cces16, religpew_congreg, "Congregational")
  214. aa8 <- fun(cces16, religpew_holiness, "Holiness")
  215. aa9 <- fun(cces16, religpew_reformed, "Reformed")
  216.  
  217. aa10 <- cces16 %>%
  218. group_by(religpew) %>%
  219. mutate(gaym = recode(CC16_335, "1=1; 2=0; else = NA")) %>%
  220. mean_ci(gaym) %>%
  221. filter(n > 100) %>%
  222. mutate(relig = to_factor(religpew)) %>%
  223. filter(relig != "Protestant") %>%
  224. select(-religpew)
  225.  
  226. aa10$family <- c("Catholic", "Mormon", "Orthodox", "Jewish", "Other Religion", "Other Religion", "Other Religion", "None", "None", "None", "Other Religion")
  227.  
  228.  
  229. g16 <- bind_df("aa") %>%
  230. rename(mean16 = mean) %>%
  231. select(relig, mean16, family)
  232.  
  233.  
  234. cces14 <- read_dta("D://cces/data/cces14.dta")
  235.  
  236. fun <- function(df, relig, fam){
  237. relig <- enquo(relig)
  238. fam <- enquo(fam)
  239.  
  240. df %>%
  241. group_by(!! relig) %>%
  242. mutate(gaym = recode(CC14_327, "1=1; 2=0; else = NA")) %>%
  243. mean_ci(gaym) %>%
  244. filter(n > 100) %>%
  245. mutate(relig = to_factor(!! relig)) %>%
  246. filter(relig != "Skipped") %>%
  247. select(relig, mean, sd, n, level, se, lower, upper) %>%
  248. mutate(family = !! fam)
  249.  
  250.  
  251. }
  252.  
  253.  
  254. aa <- fun(cces14, religpew_methodist, "Methodist")
  255. aa1 <- fun(cces14, religpew_baptist, "Baptist")
  256. aa2 <- fun(cces14, religpew_nondenom, "Non-Denom")
  257. aa3 <- fun(cces14, religpew_lutheran, "Lutheran")
  258. aa4 <- fun(cces14, religpew_presby, "Presbyterian")
  259. aa5 <- fun(cces14, religpew_pentecost, "Pentecostal")
  260. aa6 <- fun(cces14, religpew_episcop, "Episcopal")
  261. aa7 <- fun(cces14, religpew_congreg, "Congregational")
  262. aa8 <- fun(cces14, religpew_holiness, "Holiness")
  263. aa9 <- fun(cces14, religpew_reformed, "Reformed")
  264.  
  265.  
  266. aa10 <- cces14 %>%
  267. group_by(religpew) %>%
  268. mutate(gaym = recode(CC14_327, "1=1; 2=0; else = NA")) %>%
  269. mean_ci(gaym) %>%
  270. filter(n > 100) %>%
  271. mutate(relig = to_factor(religpew)) %>%
  272. filter(relig != "Protestant") %>%
  273. select(-religpew)
  274.  
  275. aa10$family <- c("Catholic", "Mormon", "Orthodox", "Jewish", "Other Religion", "Other Religion", "Other Religion", "None", "None", "None", "Other Religion")
  276.  
  277. g14 <- bind_df("aa")
  278.  
  279. g14 <- g14 %>%
  280. rename(mean14 = mean) %>%
  281. select(relig, mean14, family)
  282.  
  283.  
  284.  
  285. cces12 <- read_dta("D://cces/data/cces12.dta")
  286.  
  287. fun <- function(df, relig, fam){
  288. relig <- enquo(relig)
  289. fam <- enquo(fam)
  290.  
  291. df %>%
  292. group_by(!! relig) %>%
  293. mutate(gaym = recode(CC326, "1=1; 2=0; else = NA")) %>%
  294. mean_ci(gaym) %>%
  295. filter(n > 100) %>%
  296. mutate(relig = to_factor(!! relig)) %>%
  297. filter(relig != "Skipped") %>%
  298. select(relig, mean, sd, n, level, se, lower, upper) %>%
  299. mutate(family = !! fam)
  300.  
  301.  
  302. }
  303.  
  304.  
  305. aa <- fun(cces12, religpew_methodist, "Methodist")
  306. aa1 <- fun(cces12, religpew_baptist, "Baptist")
  307. aa2 <- fun(cces12, religpew_nondenom, "Non-Denom")
  308. aa3 <- fun(cces12, religpew_lutheran, "Lutheran")
  309. aa4 <- fun(cces12, religpew_presby, "Presbyterian")
  310. aa5 <- fun(cces12, religpew_pentecost, "Pentecostal")
  311. aa6 <- fun(cces12, religpew_episcop, "Episcopal")
  312. aa7 <- fun(cces12, religpew_congreg, "Congregational")
  313. aa8 <- fun(cces12, religpew_holiness, "Holiness")
  314. aa9 <- fun(cces12, religpew_reformed, "Reformed")
  315.  
  316.  
  317. aa10 <- cces12 %>%
  318. group_by(religpew) %>%
  319. mutate(gaym = recode(CC326, "1=1; 2=0; else = NA")) %>%
  320. mean_ci(gaym) %>%
  321. filter(n > 100) %>%
  322. mutate(relig = to_factor(religpew)) %>%
  323. filter(relig != "Protestant") %>%
  324. select(-religpew)
  325.  
  326. aa10$family <- c("Catholic", "Mormon", "Orthodox", "Jewish", "Other Religion", "Other Religion", "None", "None", "None", "Other Religion")
  327.  
  328. g12 <- bind_df("aa")
  329.  
  330. g12 <- g12 %>%
  331. rename(mean12 = mean) %>%
  332. select(relig, mean12, family)
  333.  
  334.  
  335.  
  336. graph <- left_join(g12, g16) %>%
  337. mutate(diff = mean16 - mean12)
  338.  
  339. font_add_google("Abel", "font")
  340. showtext_auto()
  341.  
  342. graph <- graph %>%
  343. mutate(diff2 = round(diff, 3)) %>%
  344. mutate(diff2 = diff2 * 100) %>%
  345. mutate(diff2 = paste(diff2, "%", sep = ""))
  346.  
  347. graph %>%
  348. ggplot(., aes(x = mean12, xend = mean16, y = reorder(relig, mean16))) +
  349. geom_dumbbell(colour_x = "firebrick2", colour_xend = "darkorchid", size = .75, size_x = 2.75, size_xend = 2.75 ) +
  350. theme_minimal() +
  351. scale_x_continuous(labels = scales::percent) +
  352. labs(x= "Percent in Favor", y = "Religious Tradition", title = "Change in Support for Gay Marriage from 2012 to 2016", caption = "Data: CCES 2012 and 2016", subtitle = "")+
  353. theme(text=element_text(size=64, family="font")) +
  354. theme(plot.subtitle = element_text(size = 36)) +
  355. theme(plot.title = element_text(face= "bold", size = 66)) +
  356. theme(legend.position = "bottom") +
  357. theme(legend.title=element_blank()) +
  358. geom_vline(xintercept = .521, color = "firebrick2", linetype = "dashed") +
  359. geom_vline(xintercept = .651, color = "darkorchid", linetype = "dashed") +
  360. annotate("text", x=.521, y = 44, label = "Avg.", size = 16, family = "font") +
  361. annotate("text", x=.521, y = 43, label = "2012", size = 16, family = "font") +
  362. annotate("text", x=.651, y = 44, label = "Avg.", size = 16, family = "font") +
  363. annotate("text", x=.651, y = 43, label = "2016", size = 16, family = "font") +
  364. geom_text(data = filter(graph, relig == "Anglican Church (Church of England)"), aes(x= mean12, y = relig, label = "2012"), vjust = -.75, family = "font", size = 16) +
  365. geom_text(data = filter(graph, relig == "Anglican Church (Church of England)"), aes(x= mean16, y = relig, label = "2016"), vjust = -.75, family = "font", size = 16) +
  366. geom_rect(data=graph, aes(xmin=1, xmax=1.1, ymin=-Inf, ymax=Inf), fill="gray") +
  367. geom_text(data=graph, aes(label=diff2, y=relig, x=1.05), fontface="bold", size=13, family="font") +
  368. geom_text(data=filter(graph, relig=="Atheist"), aes(x=1.05, y=relig, label=""), size=10, fontface="bold", family="font", vjust = -.65) +
  369. ggsave("D://cces/images/gay_m_moves12.png", height = 10, width = 12)
  370.  
  371.  
  372. cces12 %>%
  373. mutate(gaym = recode(CC326, "1=1; 2=0; else = NA")) %>%
  374. mean_ci(gaym)
  375.  
  376. cces16 %>%
  377. mutate(gaym = recode(CC16_335, "1=1; 2=0; else = NA")) %>%
  378. mean_ci(gaym)
Add Comment
Please, Sign In to add comment