Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created May 14, 2024 19:11
Show Gist options
  • Save ryanburge/587c520313be71107aefd8528597c797 to your computer and use it in GitHub Desktop.
Save ryanburge/587c520313be71107aefd8528597c797 to your computer and use it in GitHub Desktop.
library(janitor)
wvs <- read_csv("E://data/wvs_7.csv") %>% clean_names()
library(googlesheets4)
codes <- read_sheet("https://docs.google.com/spreadsheets/d/13CPhje432YrYamL6nkWSk-5dowH3_km_SQdmbF2tZvI/edit?usp=sharing")
wvs <- wvs %>%
mutate(num = b_country)
wvs <- left_join(wvs, codes)
process_group <- function(data, question, type) {
data %>%
mutate(vv = !!sym(question)) %>%
mutate(vv = case_when(
vv == 2 ~ 1,
vv == 1 | vv == 0 ~ 0
)) %>%
group_by(name) %>%
mean_ci(vv, wt = w_weight, ci = .84) %>%
mutate(type = type)
}
questions <- c("q94", "q95", "q96", "q97", "q98", "q99", "q100", "q101", "q102", "q103", "q104")
types <- c("Religious", "Recreational", "Art/Music/Educ.", "Labor Union", "Political Party", "Environmental Org.", "Professional Org.", "Charitable Org.", "Consumer Org.", "Mutual Aid", "Women's Group")
results <- map2_dfr(questions, types, ~process_group(wvs, .x, .y))
all <- results %>% filter(name != "NA")
region <- read_sheet("https://docs.google.com/spreadsheets/d/1avVTlFzx5xhT5JK6OS3Gcbi5CrjFnGM_f5zvItddgsQ/edit?usp=sharing")
gg <- left_join(all, region)
graph <- gg %>%
filter(type == "Religious") %>%
arrange(-mean) %>% as.data.frame()
graph[7,1] <- "Bolivia"
graph[21, 1] <- "Iran"
graph[39, 1] <- "South Korea"
graph[42, 1] <- "Netherlands"
graph[11, 1] <- "Philippines"
graph[56, 1] <- "Russia"
graph[40, 1] <- "Taiwan"
graph[14, 1] <- "United States"
graph[18, 1] <- "Venezuela"
graph[59, 1] <- "Vietnam"
graph[31, 1] <- "United Kingdom"
graph %>%
ggplot(., aes(x= reorder(name, mean), y=mean)) +
geom_point(size = 2.5, stroke = 1, shape = 21, fill = "white", aes(color = region)) +
geom_segment(aes(x = name, xend = name, y = 0, yend = mean - .0075), show.legend = FALSE) +
coord_flip() +
theme_rb() +
scale_color_gdocs() +
scale_y_continuous(labels = percent) +
labs(x = "", y = "", title = "Share Who Are Active Members in A Church or Religious Organization", caption = "@ryanburge\nData: World Values Survey, Wave 7 (2017-2022)")
save("lollipops_wvs_church.png", ht = 10)
graph <- gg %>%
filter(type == "Religious") %>%
arrange(-mean) %>% as.data.frame()
imp <- wvs %>%
mutate(rel = q6) %>%
mutate(rel = case_when(rel == 1 ~ 1,
rel == 2 | rel == 3 | rel == 4 ~ 0)) %>%
group_by(name) %>%
mean_ci(rel, wt = w_weight)
imp <- imp %>%
select(name, imp = mean)
org <- graph %>%
select(name, org = mean)
both <- left_join(org, imp)
both <- left_join(both, codes)
both <- left_join(both, region)
both[7,1] <- "Bolivia"
both[21, 1] <- "Iran"
both[39, 1] <- "South Korea"
both[42, 1] <- "Netherlands"
both[11, 1] <- "Philippines"
both[56, 1] <- "Russia"
both[40, 1] <- "Taiwan"
both[14, 1] <- "United States"
both[18, 1] <- "Venezuela"
both[59, 1] <- "Vietnam"
both[31, 1] <- "United Kingdom"
library(ggrepel)
both %>%
ggplot(., aes(x = imp, y = org)) +
geom_point(aes(color = region), stroke = .75, shape = 21) +
geom_smooth(se = FALSE, method = lm, color = "black", linewidth = .5, linetype = "twodash") +
scale_color_gdocs() +
geom_text_repel(data = both, aes(x = imp, y = org, label = abb), family = "font", size = 4) +
theme_rb() +
y_pct() +
x_pct() +
labs(x = "Share Saying Religion is Very Important", y = "Share Who Are Active Members in Religious Group", title = "Relationship Between Religious Importance and Active Membership", caption = "@ryanburge\nData: World Values Survey, Wave 7 (2017-2022)")
save("wvs_scatter_imp_org.png")
aa <- both %>%
filter(imp > .5) %>%
filter(org < .20) %>%
mutate(flag = "a")
count <- wvs %>%
group_by(name) %>%
mutate(bel = q94) %>%
mutate(bel = frcode(q94 == 2 ~ "Active",
q94 == 1 ~ "Inactive",
q94 == 0 ~ "Don't Belong")) %>%
ct(bel, wt = w_weight, show_na = FALSE)
graph <- left_join(count, aa) %>% filter(flag == "a")
graph %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(bel))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ name, ncol =1, strip.position = "left") +
theme_rb() +
scale_fill_manual(values = c("#19798B", "#E74B47", "#F3D567")) +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0)) +
guides(fill = guide_legend(reverse=T, nrow = 1)) +
theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) +
theme(panel.grid.minor.y=element_blank(), panel.grid.major.y=element_blank()) +
geom_text(aes(label = ifelse(pct >.05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "black") +
geom_text(aes(label = ifelse(bel == "Don't Belong", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
theme(plot.title = element_text(size = 14)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "Are a member, an active member, an inactive member or not a member of a church or religious organization", caption = "@ryanburge\nData: World Values Survey, Wave 7 (2017-2022)")
save("wvs_active_inactive.png", wd = 9, ht = 8)
highest_percentage <- results %>%
group_by(name) %>%
filter(mean == max(mean)) %>%
select(region = name, type)
highest_percentage %>% write_sheet()
high <- read_sheet("https://docs.google.com/spreadsheets/d/1AYsevwSAF2XUICNq3vOLU3BTNzWRcBAIaj7aktRgtgk/edit?usp=sharing")
world_coordinates <- map_data("world")
world_coordinates <- left_join(world_coordinates, high)
ggplot() +
geom_map(
data = world_coordinates, map = world_coordinates, aes(long, lat, map_id = region, fill = type)) +
theme_rb(legend = TRUE) +
scale_fill_manual(values = c(moma.colors("Klein", 9))) +
theme(legend.position = "bottom") +
labs(x = "", y = "", caption = "@ryanburge\nData: World Values Survey, Wave 7 (2017-2022)", title = "Group with the Highest Percentage of Active Members") +
theme(legend.position = "bottom",
axis.text = element_blank(), # Hide axis text
axis.title = element_blank(), # Hide axis title
panel.grid = element_blank()) + # Hide gridlines
theme(axis.text = element_blank())
save("wvs_active_mem_map.png")
last <- all %>%
filter(type != "Religious") %>%
group_by(name) %>%
summarise(sum = sum(mean)) %>%
arrange(-sum)
graph <- gg %>%
filter(type == "Religious") %>%
select(name, rel = mean, region)
last <- left_join(last, graph)
last <- left_join(last, codes)
last[7,1] <- "Bolivia"
last[14, 1] <- "United States"
last[16, 1] <- "Philippines"
last[20, 1] <- "United Kingdom"
last[23, 1] <- "Iran"
last[32, 1] <- "Netherlands"
last[33, 1] <- "Taiwan"
last[37, 1] <- "Venezuela"
last[38, 1] <- "Vietnam"
last[46, 1] <- "South Korea"
last[62, 1] <- "Russia"
last %>%
ggplot(., aes(x = rel, y = sum)) +
geom_point(aes(color = region), stroke = .75, shape = 21) +
geom_smooth(se = FALSE, method = lm, color = "black", linewidth = .5, linetype = "twodash") +
scale_color_gdocs() +
geom_text_repel(data = last, aes(x = rel, y = sum, label = abb), family = "font", size = 4) +
theme_rb() +
x_pct() +
labs(x = "Share Who are an Active Member of a Religious Group",
y = "Summed Index of All Group Membership (Excluding Religion)",
title = "Does Religious Membership Drive All Group Membership?",
caption = "@ryanburge\nData: World Values Survey, Wave 7 (2017-2022)")
save("wvs_scatter_joining.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment