Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CCES Counts Maps Nones
cces <- cces %>%
mutate(trad = frcode(evangelical == 1 ~ "Evangelical",
mainline == 1 ~ "Mainline",
bprot == 1 ~ "Black Protestant",
catholic == 1 ~ "Catholic",
jewish == 1 ~ "Jewish",
other == 1 ~ "Other Faith",
none == 1 ~ "No Religion",
TRUE ~ "Unclassified"))
graph <- cces %>%
filter(year > 2006) %>%
group_by(year) %>%
ct(trad, wt = weight, show_na = TRUE)
colors <- c("Evangelical" = "#1F77B4", "Mainline" = "#FF7F0E", "Black Protestant" = "#E377C2", "Catholic" = "#D62728", "Jewish" = "#9467BD", "Other Faith" = "#8C564B", "No Religion" = "#2CA02C", "Unclassified" = "#FF9896")
graph %>%
ggplot(., aes(x = year, y = pct, group = trad, color = trad)) +
geom_point() +
geom_line() +
theme_gg("Abel") +
scale_color_manual(values = colors) +
scale_y_continuous(labels = percent) +
scale_x_continuous(breaks = c(2008, 2010, 2012, 2014, 2016, 2018)) +
annotate("text", x= 2013, y = .272, label = "No Religion", size = 4, family = "font") +
annotate("text", x= 2013, y = .233, label = "Evangelical", size = 4, family = "font") +
annotate("text", x= 2013, y = .19, label = "Catholic", size = 4, family = "font") +
annotate("text", x= 2013, y = .141, label = "Mainline", size = 4, family = "font") +
annotate("text", x= 2013, y = .095, label = "Other Faith", size = 4, family = "font") +
annotate("text", x= 2013, y = .067, label = "Unclassified", size = 4, family = "font") +
annotate("text", x= 2013, y = .038, label = "Black Protestant", size = 4, family = "font") +
annotate("text", x= 2013, y = .015, label = "Jewish", size = 4, family = "font") +
labs(x = "", y = "", title = "Changes in Religion over the Last Decade", caption = "@ryanburge\nData: CCES 2008-2018") +
ggsave("E://growth_decline_lines.png", type = "cairo-png")
### Two Maps ####
com_fun <- function(df, var, name) {
var <- enquo(var)
st <- df %>%
filter(year == 2008) %>%
group_by(state) %>%
mean_ci(!! var, wt = weight)
st1 <- df %>%
filter(year == 2018) %>%
group_by(state) %>%
mean_ci(!! var, wt = weight)
bind_cols(st, st1) %>%
mutate(change = mean1 - mean) %>%
select(state, change) %>%
mutate(group = name)
}
aaa1 <- cces %>% com_fun(evangelical, "Evangelical")
aaa2 <- cces %>% com_fun(mainline, "Mainline")
aaa3 <- cces %>% com_fun(bprot, "Black Protestant")
aaa4 <- cces %>% com_fun(catholic, "Catholic")
aaa5 <- cces %>% com_fun(jewish, "Jewish")
aaa6 <- cces %>% com_fun(other, "Other Faith")
aaa7 <- cces %>% com_fun(none, "No Religion")
graph <- bind_df("aaa")
gg <- graph %>%
filter(change >= 0) %>%
group_by(state) %>%
top_n(1, change) %>%
ungroup(state) %>%
mutate(id = tolower(state))
library(maps)
us_states <- map_data("state")
mapp <- left_join(us_states, gg, by = c("region")) %>% as_tibble()
mapp %>%
ggplot(., mapping = aes(x = long, y = lat, group = group.x, fill = group.y)) +
geom_polygon(color = "gray90", size = 0.1) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
scale_fill_manual(values = colors) +
theme_gg("Abel") +
theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
panel.spacing = unit(0, "lines"),
plot.background = element_blank(),
legend.justification = c(0, 0),
legend.position = "bottom",
plot.title = element_text(size = 14)) +
labs(title = "Which Religious Group Has Grown the Most Per State? (2008-2018)", caption = "@ryanburge\nData: CCES 2008 + 2018") +
ggsave("E://biggest_change_map_growth.png", type = "cairo-png")
graph <- bind_df("aaa")
gg <- graph %>%
filter(group != "Other Faith") %>%
filter(change < 0) %>%
mutate(absolute = abs(change)) %>%
group_by(state) %>%
top_n(1, absolute) %>%
ungroup(state) %>%
mutate(region = tolower(state))
library(maps)
us_states <- map_data("state")
mapp <- left_join(us_states, gg, by = c("region")) %>% as_tibble()
mapp %>%
ggplot(., mapping = aes(x = long, y = lat, group = group.x, fill = group.y)) +
geom_polygon(color = "gray90", size = 0.1) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
scale_fill_manual(values = colors) +
theme_gg("Abel") +
theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
panel.spacing = unit(0, "lines"),
plot.background = element_blank(),
legend.justification = c(0, 0),
legend.position = "bottom",
plot.title = element_text(size = 14)) +
labs(title = "Which Religious Group Has Declined the Most Per State? (2008-2018)", caption = "@ryanburge\nData: CCES 2008 + 2018") +
ggsave("E://biggest_change_map_decline.png", type = "cairo-png")
nn <- bind_df("aaa") %>%
filter(group == "No Religion") %>%
mutate(region = tolower(state))
mapp <- left_join(us_states, nn, by = c("region")) %>% as_tibble()
mapp %>%
ggplot(., mapping = aes(x = long, y = lat, group = group.x, fill = change)) +
geom_polygon(color = "gray90", size = 0.1) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
scale_fill_gradient(low = "azure3", high = "#2CA02C", labels = percent) +
theme_gg("Abel") +
theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
panel.spacing = unit(0, "lines"),
plot.background = element_blank(),
legend.justification = c(0, 0),
legend.position = "right",
plot.title = element_text(size = 14)) +
labs(title = "The Change in Nones (2008-2018)", caption = "@ryanburge\nData: CCES 2008 + 2018") +
ggsave("E://nones_change_map1.png", type = "cairo-png")
## Dumbbells #####
st08 <- cces %>%
filter(year == 2008) %>%
group_by(state) %>%
mean_ci(none, wt = weight)
st18 <- cces %>%
filter(year == 2018) %>%
group_by(state) %>%
mean_ci(none, wt = weight)
graph <- bind_cols(st08, st18) %>%
rename(mean08 = mean) %>%
rename(mean18 = mean1) %>%
mutate(diff1 = mean18 - mean08) %>%
mutate(diff = diff1 * 100) %>%
mutate(diff = round(diff, 2)) %>%
mutate(diff = paste0(diff, '%'))
graph %>%
ggplot(., aes(x = mean08, xend = mean18, y = reorder(state, mean18))) +
geom_dumbbell(colour_x = "#2F2D29", colour_xend = "#FE6F27", size = .75, size_x = 2.75, size_xend = 2.75, shape = 21, stroke =2, fill = "white") +
theme_gg("Abel") +
theme(axis.text.y = element_text(size = 14)) +
theme(axis.text.x = element_text(size = 18)) +
theme(plot.title = element_text(size = 22)) +
labs(x= "Percent Nones", y = "Religious Tradition", title = "Change in Nones from 2008 to 2018", caption = "Data: CCES 2008 and 2018", subtitle = "") +
scale_x_continuous(labels = percent) +
geom_text(data = filter(graph, state == "Oregon"), aes(x= mean08, y = state, label = "2008"), hjust = 1.35, family = "font", size = 4) +
geom_text(data = filter(graph, state == "Oregon"), aes(x= mean18, y = state, label = "2018"), hjust = -.35, family = "font", size = 4) +
geom_text(data = filter(graph, state == "Kentucky"), aes(x= mean08, y = state, label = "2008"), hjust = 1.35, family = "font", size = 4) +
geom_text(data = filter(graph, state == "Kentucky"), aes(x= mean18, y = state, label = "2018"), hjust = -.35, family = "font", size = 4) +
geom_rect(data=graph, aes(xmin=.48, xmax=.50, ymin=-Inf, ymax=Inf), fill="gray") +
geom_text(data=graph, aes(label=diff, y=state, x=.49), fontface="bold", size=4, family="font") +
# geom_text(data=filter(graph, state=="Oregon"), aes(x=.49 y=state, label=""), size=10, fontface="bold", family="font", vjust = -.65) +
ggsave("E://dumbbell_nones.png", type = "cairo-png", height = 10, width = 10)
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.