Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created May 29, 2018 13:06
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/b55159d25f75f28bcf0d7d05d56ac1b3 to your computer and use it in GitHub Desktop.
Save ryanburge/b55159d25f75f28bcf0d7d05d56ac1b3 to your computer and use it in GitHub Desktop.
Seeing Shifts in Party ID among BA Prots
c06 <- cces06 %>%
select(baprot, v2005, v4034) %>%
rename(race = v2005) %>%
rename(pid3 = v4034) %>%
mutate(year = "2006")
c08 <- cces08 %>%
select(baprot, V211, CC307) %>%
rename(race = V211) %>%
rename(pid3 = CC307) %>%
mutate(year = "2008")
c10 <- cces10 %>%
select(baprot, V211, V212a) %>%
rename(race = V211) %>%
rename(pid3 = V212a) %>%
mutate(year = "2010")
c12 <- cces12 %>%
select(baprot, race, pid3) %>%
mutate(year = "2012")
c14 <- cces14 %>%
select(baprot, race, pid3) %>%
mutate(year = "2014")
c16 <- cces16 %>%
select(baprot, race, pid3) %>%
mutate(year = "2016")
all <- bind_rows(c06, c08, c10, c12, c14, c16)
## White Graph ####
rep <- all %>%
filter(pid3 <4) %>%
filter(race ==1 & baprot ==1) %>%
mutate(pid1 = recode(pid3, "2=1; else=0")) %>%
group_by(year) %>%
summarise(mean = mean(pid1),
sd = sd(pid1),
n = n()) %>%
mutate(se = sd/sqrt(n),
lower = mean - qt(1 - (0.05 /2), n -1) * se,
upper = mean + qt(1 - (0.05 /2), n -1) * se) %>%
mutate(party = "Republican")
dem <- all %>%
filter(pid3 <4) %>%
filter(race ==1 & baprot ==1) %>%
mutate(pid1 = recode(pid3, "1=1; else=0")) %>%
group_by(year) %>%
summarise(mean = mean(pid1),
sd = sd(pid1),
n = n()) %>%
mutate(se = sd/sqrt(n),
lower = mean - qt(1 - (0.05 /2), n -1) * se,
upper = mean + qt(1 - (0.05 /2), n -1) * se) %>%
mutate(party = "Democrat")
ind <- all %>%
filter(pid3 <4) %>%
filter(race ==1 & baprot ==1) %>%
mutate(pid1 = recode(pid3, "3=1; else=0")) %>%
group_by(year) %>%
summarise(mean = mean(pid1),
sd = sd(pid1),
n = n()) %>%
mutate(se = sd/sqrt(n),
lower = mean - qt(1 - (0.05 /2), n -1) * se,
upper = mean + qt(1 - (0.05 /2), n -1) * se) %>%
mutate(party = "Independent")
wht_ba <- bind_rows(rep, dem, ind)
pd <- position_dodge(0.2)
wht_ba %>%
ggplot(., aes(x=year, y=mean, group = party, color = party)) + geom_point() + geom_line(size = 1.25) +
geom_errorbar(aes(ymin=lower, ymax = upper), width = 0, size = 1.25) +
long_rb() +
scale_y_continuous(labels = scales::percent) +
scale_color_manual(values=c("dodgerblue3", "gray", "firebrick3")) +
theme(plot.title = element_text(size =24)) +
labs(x = "Year", y = "Percent of Population", title = "The Change in Partisanship Among White Born-Again Protestants", caption = "Data: CCES 2006-2016")
ggsave(file="D://cces/comparing_pid3_white1.png", type = "cairo-png", width = 12, height = 6)
## All Graph ####
rep <- all %>%
filter(pid3 <4) %>%
filter(baprot ==1) %>%
mutate(pid1 = recode(pid3, "2=1; else=0")) %>%
group_by(year) %>%
summarise(mean = mean(pid1),
sd = sd(pid1),
n = n()) %>%
mutate(se = sd/sqrt(n),
lower = mean - qt(1 - (0.05 /2), n -1) * se,
upper = mean + qt(1 - (0.05 /2), n -1) * se) %>%
mutate(party = "Republican")
dem <- all %>%
filter(pid3 <4) %>%
filter(baprot ==1) %>%
mutate(pid1 = recode(pid3, "1=1; else=0")) %>%
group_by(year) %>%
summarise(mean = mean(pid1),
sd = sd(pid1),
n = n()) %>%
mutate(se = sd/sqrt(n),
lower = mean - qt(1 - (0.05 /2), n -1) * se,
upper = mean + qt(1 - (0.05 /2), n -1) * se) %>%
mutate(party = "Democrat")
ind <- all %>%
filter(pid3 <4) %>%
filter(baprot ==1) %>%
mutate(pid1 = recode(pid3, "3=1; else=0")) %>%
group_by(year) %>%
summarise(mean = mean(pid1),
sd = sd(pid1),
n = n()) %>%
mutate(se = sd/sqrt(n),
lower = mean - qt(1 - (0.05 /2), n -1) * se,
upper = mean + qt(1 - (0.05 /2), n -1) * se) %>%
mutate(party = "Independent")
ba <- bind_rows(rep, dem, ind)
pd <- position_dodge(0.2)
ba %>%
ggplot(., aes(x=year, y=mean, group = party, color = party)) + geom_point() + geom_line(size = 1.25) +
geom_errorbar(aes(ymin=lower, ymax = upper), width = 0, size = 1.25) +
long_rb() +
scale_y_continuous(labels = scales::percent) +
scale_color_manual(values=c("dodgerblue3", "gray", "firebrick3")) +
theme(plot.title = element_text(size =24)) +
labs(x = "Year", y = "Percent of Population", title = "The Change in Partisanship Among Born-Again Protestants", caption = "Data: CCES 2006-2016")
ggsave(file="D://cces/comparing_pid3_all.png", type = "cairo-png", width = 12, height = 6)
## Black Graph ####
rep <- all %>%
filter(pid3 <4) %>%
filter(race ==2 & baprot ==1) %>%
mutate(pid1 = recode(pid3, "2=1; else=0")) %>%
group_by(year) %>%
summarise(mean = mean(pid1),
sd = sd(pid1),
n = n()) %>%
mutate(se = sd/sqrt(n),
lower = mean - qt(1 - (0.05 /2), n -1) * se,
upper = mean + qt(1 - (0.05 /2), n -1) * se) %>%
mutate(party = "Republican")
dem <- all %>%
filter(pid3 <4) %>%
filter(race ==2 & baprot ==1) %>%
mutate(pid1 = recode(pid3, "1=1; else=0")) %>%
group_by(year) %>%
summarise(mean = mean(pid1),
sd = sd(pid1),
n = n()) %>%
mutate(se = sd/sqrt(n),
lower = mean - qt(1 - (0.05 /2), n -1) * se,
upper = mean + qt(1 - (0.05 /2), n -1) * se) %>%
mutate(party = "Democrat")
ind <- all %>%
filter(pid3 <4) %>%
filter(race ==2 & baprot ==1) %>%
mutate(pid1 = recode(pid3, "3=1; else=0")) %>%
group_by(year) %>%
summarise(mean = mean(pid1),
sd = sd(pid1),
n = n()) %>%
mutate(se = sd/sqrt(n),
lower = mean - qt(1 - (0.05 /2), n -1) * se,
upper = mean + qt(1 - (0.05 /2), n -1) * se) %>%
mutate(party = "Independent")
blk_ba <- bind_rows(rep, dem, ind)
blk_ba %>%
ggplot(., aes(x=year, y=mean, group = party, color = party)) + geom_point() + geom_line(size = 1.25) +
geom_errorbar(aes(ymin=lower, ymax = upper), width = 0, size = 1.25) +
long_rb() +
scale_y_continuous(labels = scales::percent) +
scale_color_manual(values=c("dodgerblue3", "gray", "firebrick3")) +
theme(plot.title = element_text(size =24)) +
labs(x = "Year", y = "Percent of Population", title = "The Change in Partisanship Among Black Born-Again Protestants", caption = "Data: CCES 2006-2016")
ggsave(file="D://cces/comparing_pid3_black1.png", type = "cairo-png", width = 12, height = 6)
## Hispanic Graph ####
rep <- all %>%
filter(pid3 <4) %>%
filter(race ==3 & baprot ==1) %>%
mutate(pid1 = recode(pid3, "2=1; else=0")) %>%
group_by(year) %>%
summarise(mean = mean(pid1),
sd = sd(pid1),
n = n()) %>%
mutate(se = sd/sqrt(n),
lower = mean - qt(1 - (0.05 /2), n -1) * se,
upper = mean + qt(1 - (0.05 /2), n -1) * se) %>%
mutate(party = "Republican")
dem <- all %>%
filter(pid3 <4) %>%
filter(race ==3 & baprot ==1) %>%
mutate(pid1 = recode(pid3, "1=1; else=0")) %>%
group_by(year) %>%
summarise(mean = mean(pid1),
sd = sd(pid1),
n = n()) %>%
mutate(se = sd/sqrt(n),
lower = mean - qt(1 - (0.05 /2), n -1) * se,
upper = mean + qt(1 - (0.05 /2), n -1) * se) %>%
mutate(party = "Democrat")
ind <- all %>%
filter(pid3 <4) %>%
filter(race ==3 & baprot ==1) %>%
mutate(pid1 = recode(pid3, "3=1; else=0")) %>%
group_by(year) %>%
summarise(mean = mean(pid1),
sd = sd(pid1),
n = n()) %>%
mutate(se = sd/sqrt(n),
lower = mean - qt(1 - (0.05 /2), n -1) * se,
upper = mean + qt(1 - (0.05 /2), n -1) * se) %>%
mutate(party = "Independent")
hisp_ba <- bind_rows(rep, dem, ind)
hisp_ba %>%
ggplot(., aes(x=year, y=mean, group = party, color = party)) + geom_point() + geom_line(size = 1.25) +
geom_errorbar(aes(ymin=lower, ymax = upper), width = 0, size = 1.25) +
long_rb() +
scale_y_continuous(labels = scales::percent) +
scale_color_manual(values=c("dodgerblue3", "gray", "firebrick3")) +
theme(plot.title = element_text(size =24)) +
labs(x = "Year", y = "Percent of Population", title = "The Change in Partisanship Among Hispanic Born-Again Protestants", caption = "Data: CCES 2006-2016")
ggsave(file="D://cces/comparing_pid3_hisp1.png", type = "cairo-png", width = 12, height = 6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment