Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Older Nones RiP
### Graph 1 ####
graph <- cces %>%
mutate(age2 = frcode(age >= 18 & age <= 35 ~ "18-35",
age >= 36 & age <= 44 ~ "36-44",
age >= 45 & age <= 54 ~ "45-54",
age >= 55 & age <= 64 ~ "55-64",
age >= 65 ~ "65+")) %>%
filter(religion == 9 | religion == 10 | religion == 11) %>%
group_by(year) %>%
ct(age2, wt =weight, show_na = FALSE)
graph %>%
ggplot(., aes(x = year, y = pct, fill = age2)) +
geom_area(alpha = .6, color = "black") +
scale_fill_tableau() +
theme_gg("Abel") +
y_pct() +
scale_x_continuous(breaks = c(2008, 2010, 2012, 2014, 2016, 2018)) +
labs(x = "", y = "", title = "The Nones are Getting Older", caption = "@ryanburge\nData: CCES 2008-2018") +
add_text(x = 2013, y = .80, word = "18-35", sz =5) +
add_text(x = 2013, y = .48, word = "36-44", sz =5) +
add_text(x = 2013, y = .33, word = "45-54", sz =5) +
add_text(x = 2013, y = .18, word = "55-64", sz =5) +
add_text(x = 2013, y = .05, word = "65+", sz =5) +
add_text(x = 2008.5, y = .80, word = "48.9%", sz =4) +
add_text(x = 2008.5, y = .45, word = "16.7%", sz =4) +
add_text(x = 2008.5, y = .26, word = "17.0%", sz =4) +
add_text(x = 2008.5, y = .13, word = "9.2%", sz =4) +
add_text(x = 2008.5, y = .05, word = "8.3%", sz =4) +
add_text(x = 2017.5, y = .80, word = "40.8%", sz =4) +
add_text(x = 2017.5, y = .50, word = "16.4%", sz =4) +
add_text(x = 2017.5, y = .35, word = "16.0%", sz =4) +
add_text(x = 2017.5, y = .19, word = "14.4%", sz =4) +
add_text(x = 2017.5, y = .06, word = "12.3%", sz =4) +
ggsave("E://age_nones.png", type = "cairo-png")
### Graph 2
graph <- cces18 %>%
mutate(age = 2018 - birthyr) %>%
mutate(age2 = frcode(age >= 18 & age <= 35 ~ "18-35",
age >= 36 & age <= 44 ~ "36-44",
age >= 45 & age <= 54 ~ "45-54",
age >= 55 & age <= 64 ~ "55-64",
age >= 65 & age <= 74 ~ "65-74",
age >= 75 ~ "75+")) %>%
mutate(trad = frcode(religpew == 1 | religpew == 2 | religpew == 4 ~ "Christian",
religpew == 9 | religpew == 10 | religpew ==11~ "Nones")) %>%
mutate(rep = case_when(pid7 == 5 | pid7 == 6 | pid7 == 7 ~ 1, TRUE ~ 0)) %>%
group_by(age2, trad) %>%
mean_ci(rep) %>%
na.omit()
graph %>%
ggplot(., aes(x = age2, y = mean, fill = trad)) +
geom_col(color = "black", position = "dodge") +
error_bar() +
lab_bar(top = FALSE, type = mean, pos = .1, sz = 4) +
theme_gg("Abel", legend = TRUE) +
y_pct() +
scale_fill_jco() +
labs(x = "", y = "", title = "Share Identifying as Republicans by Age Group", caption = "@ryanburge\nData: CCES 2018") +
ggsave("E://rep_age2_cces18.png", type = "cairo-png", width = 7)
## Graph 3 and 4 ####
cces16 <- cces16 %>%
mutate(guncontrol = recode(CC16_301a, "1=5; 2=4; 3=3; 4=2; 5=1; else=99")) %>%
mutate(abortion = recode(CC16_301b, "1=5; 2=4; 3=3; 4=2; 5=1; else=99")) %>%
mutate(taxes = recode(CC16_301c, "1=5; 2=4; 3=3; 4=2; 5=1; else=99")) %>%
mutate(immigration = recode(CC16_301d, "1=5; 2=4; 3=3; 4=2; 5=1; else=99")) %>%
mutate(deficit = recode(CC16_301e, "1=5; 2=4; 3=3; 4=2; 5=1; else=99")) %>%
mutate(defense = recode(CC16_301f, "1=5; 2=4; 3=3; 4=2; 5=1; else=99")) %>%
mutate(ssecurity = recode(CC16_301g, "1=5; 2=4; 3=3; 4=2; 5=1; else=99")) %>%
mutate(environment = recode(CC16_301h, "1=5; 2=4; 3=3; 4=2; 5=1; else=99")) %>%
mutate(jobs = recode(CC16_301i, "1=5; 2=4; 3=3; 4=2; 5=1; else=99")) %>%
mutate(crime = recode(CC16_301j, "1=5; 2=4; 3=3; 4=2; 5=1; else=99")) %>%
mutate(natsec = recode(CC16_301k, "1=5; 2=4; 3=3; 4=2; 5=1; else=99")) %>%
mutate(racerel = recode(CC16_301l, "1=5; 2=4; 3=3; 4=2; 5=1; else=99")) %>%
mutate(healthcare = recode(CC16_301m, "1=5; 2=4; 3=3; 4=2; 5=1; else=99")) %>%
mutate(gaym = recode(CC16_301n, "1=5; 2=4; 3=3; 4=2; 5=1; else=99")) %>%
mutate(corrupt = recode(CC16_301o, "1=5; 2=4; 3=3; 4=2; 5=1; else=99"))
cces16 <- cces16 %>%
mutate(age = 2016-birthyr) %>%
mutate(age = as.numeric(age)) %>%
mutate(age2 = recode(age, "18:35 = '18-35'; 36:44 = '36-44'; 45:54 = '45-54'; 55:64 = '55-64'; 65:74 = '65-74'; 75:100 = '75 and Older'"))
fun <- function(df, var, name){
df %>%
mutate(trad = frcode(religpew == 1 | religpew == 2 | religpew == 4 ~ "Christian",
religpew == 9 | religpew == 10 | religpew ==11~ "Nones")) %>%
filter({{var}} != 99) %>%
group_by(trad, age2) %>%
mean_ci({{var}}) %>%
mutate(issue = name) %>%
na.omit()
}
aaa1 <- cces16 %>% fun(guncontrol, "Gun Control")
aaa2 <- cces16 %>% fun(abortion, "Abortion")
aaa3 <- cces16 %>% fun(taxes, "Taxes")
aaa4 <- cces16 %>% fun(immigration, "Immigration")
aaa5 <- cces16 %>% fun(deficit, "Budget Deficits")
aaa6 <- cces16 %>% fun(defense, "Defense")
aaa7 <- cces16 %>% fun(ssecurity, "Soc. Security")
aaa8 <- cces16 %>% fun(environment, "Environment")
aaa9 <- cces16 %>% fun(jobs, "Jobs")
aaa10 <- cces16 %>% fun(crime, "Crime")
aaa11 <- cces16 %>% fun(natsec, "Natl. Security")
aaa12 <- cces16 %>% fun(racerel, "Race Relations")
aaa13 <- cces16 %>% fun(healthcare, "Health Care")
aaa14 <- cces16 %>% fun(gaym, "Gay Marriage")
aaa15 <- cces16 %>% fun(corrupt, "Corruption")
all <- bind_df("aaa")
fun2 <- function(range) {
all %>%
filter(age2 == range) %>%
group_by(trad) %>%
arrange(-mean) %>%
mutate(rank = seq(1:15))
}
rrr1 <- fun2("18-35")
rrr2 <- fun2("36-44")
rrr3 <- fun2("45-54")
rrr4 <- fun2("55-64")
rrr5 <- fun2("65-74")
rrr6 <- fun2("75 and Older")
rank <- bind_df("rrr")
rank <- rank %>%
mutate(ag = case_when(age2 == "18-35" ~ 1,
age2 == "36-44" ~ 2,
age2 == "45-54" ~ 3,
age2 == "55-64" ~ 4,
age2 == "65-74" ~ 5,
age2 == "75 and Older" ~ 6))
xtn <- rank %>%
filter(trad == "Christian")
none <- rank %>%
filter(trad == "Nones")
ggplot(xtn, aes(ag, rank, color = issue)) +
geom_point(size=5, color="white") +
geom_point(size=4, shape=1) +
geom_point(size=3, shape=19) +
geom_bump(aes(smooth = 8), size = 2) +
scale_y_reverse(breaks = c(1, 5, 10, 15)) +
scale_x_continuous(breaks = c(1,2,3,4,5,6), labels = c("18-35", "36-44", "45-54", "55-64", "65-74", "75 and Older"), limits = c(-.8, 8)) +
theme_gg("Abel") +
geom_text(data = xtn %>% filter(ag == min(ag)),
aes(x = ag - .1, label = issue), size = 5, hjust = 1, family = "font") +
geom_text(data = xtn %>% filter(ag == max(ag)),
aes(x = ag + .1, label = issue), size = 5, hjust = 0, family = "font") +
scale_colour_tableau(palette = "Tableau 20") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
labs(x = "Age Group", y = "Order of Importance", title = "Issue Ranks Among Christians", caption = "@ryanburge\nData: CCES 2016") +
ggsave("E://bump_rank_christians.png", type = "cairo-png", width = 7.75)
ggplot(none, aes(ag, rank, color = issue)) +
geom_point(size=5, color="white") +
geom_point(size=4, shape=1) +
geom_point(size=3, shape=19) +
geom_bump(aes(smooth = 8), size = 2) +
scale_y_reverse(breaks = c(1, 5, 10, 15)) +
scale_x_continuous(breaks = c(1,2,3,4,5,6), labels = c("18-35", "36-44", "45-54", "55-64", "65-74", "75 and Older"), limits = c(-.8, 8)) +
theme_gg("Abel") +
geom_text(data = none %>% filter(ag == min(ag)),
aes(x = ag - .1, label = issue), size = 5, hjust = 1, family = "font") +
geom_text(data = none %>% filter(ag == max(ag)),
aes(x = ag + .1, label = issue), size = 5, hjust = 0, family = "font") +
scale_colour_tableau(palette = "Tableau 20") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
labs(x = "Age Group", y = "Order of Importance", title = "Issue Ranks Among Nones", caption = "@ryanburge\nData: CCES 2016") +
ggsave("E://bump_rank_nones.png", type = "cairo-png", width = 7.75)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.