Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created July 28, 2024 15:18
Show Gist options
  • Save ryanburge/3bc65b3835c83252c421a36b77fe8edb to your computer and use it in GitHub Desktop.
Save ryanburge/3bc65b3835c83252c421a36b77fe8edb to your computer and use it in GitHub Desktop.
ess <- read_csv("E://data/ess11.csv") %>% select(-name)
country_names <- tibble(
cntry = c("AL", "AT", "BE", "BG", "CH", "CY", "CZ", "DE", "DK", "EE", "ES", "FI", "FR", "GB", "GE", "GR", "HR", "HU", "IE", "IS", "IL", "IT", "LT", "LU", "LV", "ME", "MK", "NL", "NO", "PL", "PT", "RO", "RS", "RU", "SE", "SI", "SK", "TR", "UA", "XK"),
country = c("Albania", "Austria", "Belgium", "Bulgaria", "Switzerland", "Cyprus", "Czechia", "Germany", "Denmark", "Estonia", "Spain", "Finland", "France", "United Kingdom", "Georgia", "Greece", "Croatia", "Hungary", "Ireland", "Iceland", "Israel", "Italy", "Lithuania", "Luxembourg", "Latvia", "Montenegro", "North Macedonia", "Netherlands", "Norway", "Poland", "Portugal", "Romania", "Serbia", "Russian Federation", "Sweden", "Slovenia", "Slovakia", "Turkey", "Ukraine", "Kosovo")
)
ess <- ess %>%
left_join(country_names, by = "cntry")
rel <- ess %>%
group_by(country) %>%
mutate(bel = rlgblg) %>%
mutate(bel = case_when(bel == 1 ~ 1,
bel == 2 ~ 0)) %>%
mean_ci(bel, wt = weighta, ci = .84)
custom_palette <- c(
"#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00",
"#FFFF33", "#A65628", "#F781BF", "#999999", "#66C2A5",
"#FC8D62", "#8DA0CB", "#E78AC3"
)
rel %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = reorder(country, mean), y = mean, fill = country)) +
geom_col(color = "black") +
coord_flip() +
theme_rb() +
scale_fill_manual(values = custom_palette) +
error_bar() +
y_pct() +
geom_text(aes(y = .035, label = paste0(lab*100, '%')), position = position_dodge(width = .9), size = 7, family = "font") +
labs(x = "", y = "Share Saying Yes", title = "Do you consider yourself as belonging to any particular religion or denomination?", caption = "@ryanburge\nData: European Social Survey, 2023-24")
save("ess_23_rel_belong.png")
gg <- ess %>%
mutate(rel = rlgdnm) %>%
mutate(rel = frcode(rel == 2 ~ "Protestant",
rel == 1 ~ "Roman Catholic",
rel == 6 ~ "Islam",
rel == 66 ~ "No Religion",
TRUE ~ "All Others")) %>%
group_by(country) %>%
ct(rel, wt = weighta, show_na = FALSE)
lvl <- gg %>%
filter(rel == "Protestant" | rel == "Roman Catholic") %>%
group_by(country) %>%
summarise(sum = sum(pct)) %>%
select(country, sort = sum)
gg <- left_join(gg, lvl)
gg$country <- fct_reorder(gg$country, gg$sort, .desc = TRUE)
gg %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(rel))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ country, ncol =1, strip.position = "left") +
theme_rb() +
scale_fill_manual(values = c(moma.colors("OKeeffe", 5))) +
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(rel == "Protestant" & pct > .05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
geom_text(aes(label = ifelse(rel == "Roman Catholic" & pct > .05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
theme(plot.title = element_text(size = 16)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "The Religious Composition of Thirteen European Countries", caption = "@ryanburge\nData: European Social Survey, 2023-24")
save("ess23_rel_comp.png", wd = 9, ht = 8)
#
# > gg %>% filter(rel == "Islam") %>% arrange(-pct)
# # A tibble: 11 × 5
# # Groups: country [11]
# country rel n pct sort
# <fct> <fct> <dbl> <dbl> <dbl>
# 1 Switzerland Islam 6341 0.05 0.426
# 2 United Kingdom Islam 17342 0.044 0.326
# 3 Austria Islam 10549 0.035 0.617
# 4 Norway Islam 3969 0.03 0.314
# 5 Slovenia Islam 4153 0.03 0.485
# 6 Netherlands Islam 4769 0.028 0.24
# 7 Germany Islam 6677 0.025 0.447
# 8 Croatia Islam 2424 0.014 0.696
# 9 Ireland Islam 3184 0.013 0.631
# 10 Finland Islam 976 0.006 0.489
# 11 Lithuania Islam 70 0 0.66
gg <- ess %>%
group_by(country) %>%
mutate(bel = rlgblg) %>%
mutate(bel = case_when(bel == 1 ~ 1,
bel == 2 ~ 0)) %>%
filter(bel == 0) %>%
mutate(ever = rlgblge) %>%
mutate(ever = frcode(ever == 1 ~ "I Once Belonged to a Religious Tradition",
ever == 2 ~ "I Never Belonged")) %>%
ct(ever, wt = weighta, show_na = FALSE)
lvl <- gg %>%
filter(ever == "I Once Belonged to a Religious Tradition") %>%
group_by(country) %>%
select(country, sort = pct)
gg <- left_join(gg, lvl)
gg$country <- fct_reorder(gg$country, gg$sort, .desc = TRUE)
gg %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(ever))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ country, ncol =1, strip.position = "left") +
theme_rb() +
scale_fill_calc() +
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 = "white") +
# geom_text(aes(label = ifelse(rel == "Protestant" & pct > .05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
# geom_text(aes(label = ifelse(rel == "Roman Catholic" & pct > .05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
theme(plot.title = element_text(size = 16)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "Among Those Who Aren't Religious, Did You Ever Belong to a Tradition?", caption = "@ryanburge\nData: European Social Survey, 2023-24")
save("ess23_rel_ever.png", wd = 9, ht = 8)
library(ggridges)
ess_meaned <- ess %>%
mutate(imp = rlgdgr) %>%
filter(imp <= 10) %>%
group_by(country) %>%
mutate(mean = mean(imp)) %>%
ungroup() %>%
mutate(country = fct_reorder(country, mean))
religion_labels <- c(
"Not at\nall religious", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "Very\nreligious")
ess_meaned %>%
mutate(imp = rlgdgr) %>%
filter(imp <= 10) %>%
ggplot() +
geom_density_ridges(
aes(x = imp, y = country, fill = country),
quantile_lines = T, quantile_fun = mean, alpha = .5) +
scale_x_continuous(
breaks = 0:10,
labels = religion_labels,
expand = c(0, 0)
) + # scale_y_discrete(expand = c(0, 0)) + # will generally have to set the `expand` option
# scale_x_continuous(expand = c(0, 0)) + # for both axes to remove unneeded padding
coord_cartesian(clip = "off") +
theme_rb() +
scale_fill_manual(values = custom_palette) +
theme(plot.title = element_text(size = 14)) +
labs(x = "", y = "", title = "Regardless of whether you belong to a particular religion, how religious would you say you are?", caption = "@ryanburge\nData: European Social Survey, 2023-24")
save("ridges_ess23.png", ht = 10)
aa1 <- ess %>%
mutate(imp = rlgdgr) %>%
filter(imp <= 10) %>%
mutate(not = case_when(imp == 0 ~ 1,
TRUE ~ 0)) %>%
group_by(country) %>%
mean_ci(not, wt = weighta, ci = .84) %>%
mutate(type = "Not\nAt All")
aa2 <- ess %>%
mutate(imp = rlgdgr) %>%
filter(imp <= 10) %>%
mutate(not = case_when(imp == 10 ~ 1,
TRUE ~ 0)) %>%
group_by(country) %>%
mean_ci(not, wt = weighta, ci = .84) %>%
mutate(type = "Very\nreligious")
graph <- bind_rows(aa1, aa2)
graph %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = type, y = mean, fill = type)) +
geom_col(color = "black") +
facet_wrap(~ country, ncol = 5) +
scale_fill_tableau() +
labs(x = "", y = "", title = "Share of Each Country At Either End of the Religious Spectrum", caption = "@ryanburge\nData: European Social Survey, 2023-24") +
y_pct() +
theme_rb() +
geom_text(aes(y = mean + .025, label = paste0(lab*100, '%')), position = position_dodge(width = .9), size = 8, family = "font")
save("ends_imp_ess.png", ht = 10, wd = 8)
gg <- ess %>%
mutate(rel = rlgdnm) %>%
mutate(rel = frcode(rel == 2 ~ "Protestant",
rel == 1 ~ "Roman Catholic",
rel == 6 ~ "Islam",
rel == 66 ~ "No Religion",
TRUE ~ "All Others")) %>%
# group_by(country) %>%
ct(rel, wt = weighta, show_na = FALSE)
name <- c("Protestant = 7", "Catholic = 16", "Prot./Catholic = 23", "No Religion = 19")
num <- c(7, 16, 23, 19)
graph <- data.frame(name, num)
graph <- graph %>%
mutate(name = fct_reorder(name, num))
graph$name <- factor(graph$name, levels = c("Protestant = 7", "Catholic = 16", "Prot./Catholic = 23", "No Religion = 19"))
library(waffle)
graph %>%
ggplot(., aes(fill = name, values = num)) +
geom_waffle(color = "white", size = 1.125) +
facet_wrap(~ name, ncol = 4) +
coord_equal() +
theme_rb() +
theme_enhance_waffle()+
scale_fill_gdocs() +
labs(x = "", y = "", title = "For Each Muslim How Many _____ Are There in Europe?", caption = "@ryanburge\nData: European Social Survey, 2023-24")
save("waffle_size_muslim_ess.png", wd = 8)
gg <- ess %>%
mutate(att = rlgatnd) %>%
mutate(att = frcode(att == 7 ~ "Never",
att == 6 ~ "Less Often",
att == 5 ~ "Holy Days",
att == 4 ~ "Monthly",
att == 3 | att == 1 | att == 2 ~ "Weekly")) %>%
group_by(country) %>%
ct(att, wt = weighta, show_na = FALSE)
lvl <- gg %>%
filter(att == "Never") %>%
group_by(country) %>%
select(country, sort = pct)
gg <- left_join(gg, lvl)
gg$country <- fct_reorder(gg$country, gg$sort, .desc = TRUE)
gg %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(att))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ country, ncol =1, strip.position = "left") +
theme_rb() +
theme(legend.position = "bottom") +
scale_fill_manual(values = c(moma.colors("ustwo", 6))) +
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(rel == "Protestant" & pct > .05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
# geom_text(aes(label = ifelse(rel == "Roman Catholic" & pct > .05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
theme(plot.title = element_text(size = 22)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "Apart from special occasions such as weddings and funerals,\nabout how often do you attend religious services nowadays?", caption = "@ryanburge\nData: European Social Survey, 2023-24")
save("ess23_attend.png", wd = 9, ht = 8)
gg <- ess %>%
mutate(pray = frcode(pray == 7 ~ "Never",
pray == 6 ~ "Less Often",
pray == 5 | pray == 4 ~ "Monthly",
pray == 3 | pray == 2 ~ "Weekly",
pray == 1 ~ "Daily")) %>%
# group_by(country) %>%
ct(pray, wt = weighta, show_na = FALSE)
lvl <- gg %>%
filter(pray == "Never") %>%
group_by(country) %>%
select(country, sort = pct)
gg <- left_join(gg, lvl)
gg$country <- fct_reorder(gg$country, gg$sort, .desc = TRUE)
gg %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(pray))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ country, ncol =1, strip.position = "left") +
theme_rb() +
theme(legend.position = "bottom") +
scale_fill_manual(values = c(met.brewer("Hiroshige", 5))) +
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(pray == "Never" & pct > .05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
# geom_text(aes(label = ifelse(rel == "Roman Catholic" & pct > .05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
theme(plot.title = element_text(size = 18)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "Apart from when you are at religious services, how often, if at all, do you pray?", caption = "@ryanburge\nData: European Social Survey, 2023-24")
save("ess23_pray.png", wd = 9, ht = 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment