Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created December 6, 2023 15:40
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/8cbea2360f31dd1a625e2a9788931d21 to your computer and use it in GitHub Desktop.
Save ryanburge/8cbea2360f31dd1a625e2a9788931d21 to your computer and use it in GitHub Desktop.
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)
graph <- wvs %>%
mutate(rel = q6) %>%
mutate(rel = frcode(rel == 4 ~ "Not at all",
rel == 3 ~ "Not very",
rel == 2 ~ "Rather",
rel == 1 ~ "Very")) %>%
group_by(name) %>%
ct(rel, wt = w_weight, show_na = FALSE)
region <- read_sheet("https://docs.google.com/spreadsheets/d/1avVTlFzx5xhT5JK6OS3Gcbi5CrjFnGM_f5zvItddgsQ/edit?usp=sharing")
gg <- left_join(graph, region)
gg[225, 1] <- "United Kingdom"
gg[226, 1] <- "United Kingdom"
gg[227, 1] <- "United Kingdom"
gg[228, 1] <- "United Kingdom"
lvl <- gg %>%
filter(rel == "Very") %>%
select(name, sort = pct)
gg <- left_join(gg, lvl)
gg$name <- fct_reorder(gg$name, gg$sort, .desc = TRUE)
gg %>%
filter(region == "Africa") %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(name), y = pct, fill = fct_rev(rel))) +
geom_col(color = "black") +
coord_flip() +
scale_fill_manual(values = c("#D7423B", "#F06A63", "#96CBA6", "#008D98")) +
theme_rb() +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0, hjust = 1)) +
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 = 6, family = "font", color = "black") +
labs(x = "", y = "", title = "How Important is Religion to You?", caption = "@ryanburge\nData: World Values Survey, Wave 7 (2017-2022)")
save("wvs_rel_import_africa.png", wd = 9, ht = 4)
gg %>%
filter(region == "Latin America") %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(name), y = pct, fill = fct_rev(rel))) +
geom_col(color = "black") +
coord_flip() +
scale_fill_manual(values = c("#D7423B", "#F06A63", "#96CBA6", "#008D98")) +
theme_rb() +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0, hjust = 1)) +
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 = 6, family = "font", color = "black") +
labs(x = "", y = "", title = "How Important is Religion to You?", caption = "@ryanburge\nData: World Values Survey, Wave 7 (2017-2022)")
save("wvs_rel_import_latin.png", wd = 9, ht = 6)
gg %>%
filter(region == "Middle East") %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(name), y = pct, fill = fct_rev(rel))) +
geom_col(color = "black") +
coord_flip() +
scale_fill_manual(values = c("#D7423B", "#F06A63", "#96CBA6", "#008D98")) +
theme_rb() +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0, hjust = 1)) +
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 = 6, family = "font", color = "black") +
labs(x = "", y = "", title = "How Important is Religion to You?", caption = "@ryanburge\nData: World Values Survey, Wave 7 (2017-2022)")
save("wvs_rel_import_mid_east.png", wd = 9, ht = 4)
gg %>%
filter(region == "Asia & Pacific") %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(name), y = pct, fill = fct_rev(rel))) +
geom_col(color = "black") +
coord_flip() +
scale_fill_manual(values = c("#D7423B", "#F06A63", "#96CBA6", "#008D98")) +
theme_rb() +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0, hjust = 1)) +
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 = 6, family = "font", color = "black") +
labs(x = "", y = "", title = "How Important is Religion to You?", caption = "@ryanburge\nData: World Values Survey, Wave 7 (2017-2022)")
save("wvs_rel_import_asia.png", wd = 9, ht = 8)
gg %>%
filter(region == "Europe") %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(name), y = pct, fill = fct_rev(rel))) +
geom_col(color = "black") +
coord_flip() +
scale_fill_manual(values = c("#D7423B", "#F06A63", "#96CBA6", "#008D98")) +
theme_rb() +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0, hjust = 1)) +
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 = 6, family = "font", color = "black") +
labs(x = "", y = "", title = "How Important is Religion to You?", caption = "@ryanburge\nData: World Values Survey, Wave 7 (2017-2022)")
save("wvs_rel_import_europe.png", wd = 9, ht = 6)
gg %>%
filter(region == "North America") %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(name), y = pct, fill = fct_rev(rel))) +
geom_col(color = "black") +
coord_flip() +
scale_fill_manual(values = c("#D7423B", "#F06A63", "#96CBA6", "#008D98")) +
theme_rb() +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0, hjust = 1)) +
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 = 7, family = "font", color = "black") +
labs(x = "", y = "", title = "How Important is Religion to You?", caption = "@ryanburge\nData: World Values Survey, Wave 7 (2017-2022)")
save("wvs_rel_import_north_america.png", wd = 9, ht = 3)
graph <- 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, ci = .84) %>%
arrange(-mean) %>%
head(10)
graph %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = reorder(name, mean), y = mean, fill = mean)) +
geom_col(color = "black") +
coord_flip() +
y_pct() +
error_bar() +
scale_fill_gradient(low = "#C04848", high = "#480048") +
lab_bar(top = FALSE, type = lab, pos = .05, sz = 8) +
geom_text(aes(y = .05, label = ifelse(mean > .80, paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 8, family = "font", color = "white") +
theme_rb() +
labs(x = "", y = "", title = "The Ten Most Religious Countries in the World Values Survey, Wave 7", subtitle = "Share Saying Religion is Very Important", caption = "@ryanburge\nData: World Values Survey, Wave 7 (2017-2022)")
save("wvs_most_relig.png")
graph <- wvs %>%
mutate(rel = q6) %>%
mutate(rel = case_when(rel == 4 ~ 1,
rel == 2 | rel == 3 | rel == 1 ~ 0)) %>%
group_by(name) %>%
mean_ci(rel, wt = w_weight, ci = .84) %>%
arrange(-mean) %>%
head(10)
graph[7,1] <- "United Kingdom"
graph %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = reorder(name, mean), y = mean, fill = mean)) +
geom_col(color = "black") +
coord_flip() +
y_pct() +
error_bar() +
scale_fill_gradient(low = "#49a09d", high = "#5f2c82") +
lab_bar(top = FALSE, type = lab, pos = .035, sz = 8) +
geom_text(aes(y = .035, label = ifelse(mean > .15, paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 8, family = "font", color = "white") +
theme_rb() +
labs(x = "", y = "", title = "The Ten Least Religious Countries in the World Values Survey, Wave 7", subtitle = "Share Saying Religion is Not At All Important", caption = "@ryanburge\nData: World Values Survey, Wave 7 (2017-2022)")
save("wvs_least_relig.png")
graph <- 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, ci = .84) %>%
arrange(name)
graph %>% write_sheet()
graph <- read_sheet("https://docs.google.com/spreadsheets/d/1cG9OoAp_fn9Pg_OQWRVaopSMTwW1tiiWEkSckO1p7ow/edit?usp=sharing")
graph <- graph %>% mutate(region = name)
world_coordinates <- map_data("world")
world_coordinates <- left_join(world_coordinates, graph)
ggplot() +
geom_map(
data = world_coordinates, map = world_coordinates, aes(long, lat, map_id = region, fill = mean)) +
scale_fill_gradient(low = "#C04848", high = "#480048", labels = percent_format()) +
theme_rb(legend = TRUE) +
theme(legend.position = "right") +
labs(x = "", y = "", caption = "@ryanburge\nData: World Values Survey, Wave 7 (2017-2022)", title = "The Share Saying Religion is Very Important") +
theme(legend.position = "right",
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_map_most.png")
graph <- wvs %>%
mutate(rel = q6) %>%
mutate(rel = case_when(rel == 4 ~ 1,
rel == 2 | rel == 3 | rel == 1 ~ 0)) %>%
group_by(name) %>%
mean_ci(rel, wt = w_weight, ci = .84) %>%
arrange(name)
clean <- read_sheet("https://docs.google.com/spreadsheets/d/1cG9OoAp_fn9Pg_OQWRVaopSMTwW1tiiWEkSckO1p7ow/edit?usp=sharing")
clean <- clean %>% select(region = name)
graph <- bind_cols(graph, clean)
world_coordinates <- map_data("world")
world_coordinates <- left_join(world_coordinates, graph)
ggplot() +
geom_map(
data = world_coordinates, map = world_coordinates, aes(long, lat, map_id = region, fill = mean)) +
scale_fill_gradient(low = "#49a09d", high = "#5f2c82", labels = percent_format()) +
theme_rb(legend = TRUE) +
theme(legend.position = "right") +
labs(x = "", y = "", caption = "@ryanburge\nData: World Values Survey, Wave 7 (2017-2022)", title = "The Share Saying Religion is Not Imporant at All") +
theme(legend.position = "right",
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_map_least.png")
not <- gg %>% filter(rel == "Not at all") %>% select(name, low = pct)
very <- gg %>% filter(rel == "Very") %>% select(name, high = pct, region)
graph <- left_join(not, very) %>%
mutate(diff = high - low) %>%
select(name, diff, region)
graph[6,1] <- "Bolivia"
graph[20, 1] <- "Iran"
graph[26, 1] <- "South Korea"
graph[36, 1] <- "Netherlands"
graph[42, 1] <- "Philippines"
graph[45, 1] <- "Russia"
graph[49, 1] <- "Taiwan"
graph[56, 1] <- "United States"
graph[58, 1] <- "Venezuela"
graph[59, 1] <- "Vietnam"
graph %>%
filter(name != "NA") %>%
ggplot(., aes(x= reorder(name, diff), y=diff)) +
geom_point(size = 2.5, stroke = 1, shape = 21, fill = "white", aes(color = region)) +
geom_segment(aes(x = name, xend = name, y = 0, yend = ifelse(diff > 0, diff - .0075, 0)), show.legend = FALSE) +
geom_segment(aes(x = name, xend = name, y = 0, yend = ifelse(diff < 0, diff + .0075, 0)), show.legend = FALSE) +
coord_flip() +
theme_rb() +
scale_color_gdocs() +
scale_y_continuous(labels = percent) +
geom_hline(yintercept = 0.328, linetype = "dashed", color = "red") +
add_text(x = 20, y = .525, word = "Overall Mean: 32.8%", sz = 6) +
labs(x = "", y = "", title = "The Most Religious Countries and the Least Religious", subtitle = "Share Saying Religion is Very Important - Share Saying Not Important at All", caption = "@ryanburge\nData: World Values Survey, Wave 7 (2017-2022)")
save("lollipops_wvs.png", ht = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment