Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created September 16, 2024 02:25
Show Gist options
  • Save ryanburge/b9b8ffa486fab32910ec10c4a532a00a to your computer and use it in GitHub Desktop.
Save ryanburge/b9b8ffa486fab32910ec10c4a532a00a to your computer and use it in GitHub Desktop.
library(rio)
library(janitor)
gap <- import("E://data/global_attitudes.sav") %>% clean_names() %>% as_tibble()
gap %>% ct(country)
gap <- gap %>%
mutate(country = frcode(
country == 1 ~ "Australia",
country == 2 ~ "Belgium",
country == 3 ~ "Canada",
country == 4 ~ "France",
country == 5 ~ "Germany",
country == 6 ~ "Greece",
country == 7 ~ "Hungary",
country == 8 ~ "Israel",
country == 9 ~ "Italy",
country == 10 ~ "Japan",
country == 11 ~ "Malaysia",
country == 12 ~ "Netherlands",
country == 13 ~ "Poland",
country == 14 ~ "Singapore",
country == 15 ~ "South Korea",
country == 16 ~ "Spain",
country == 17 ~ "Sweden",
country == 18 ~ "UK"))
library(dplyr)
protest_data <- gap %>%
mutate(imp = case_when(citizen_protest == 1 ~ 1,
citizen_protest %in% c(2, 3, 4) ~ 0)) %>%
group_by(country) %>%
mean_ci(imp, wt = weight, ci = .84) %>%
mutate(type = "Joining Demonstrations")
religion_data <- gap %>%
mutate(imp = case_when(citizen_religion == 1 ~ 1,
citizen_religion %in% c(2, 3, 4) ~ 0)) %>%
group_by(country) %>%
mean_ci(imp, wt = weight, ci = .84) %>%
mutate(type = "Attending Religious Services")
vote_data <- gap %>%
mutate(imp = case_when(citizen_vote == 1 ~ 1,
citizen_vote %in% c(2, 3, 4) ~ 0)) %>%
group_by(country) %>%
mean_ci(imp, wt = weight, ci = .84) %>%
mutate(type = "Voting")
follow_data <- gap %>%
mutate(imp = case_when(citizen_follow == 1 ~ 1,
citizen_follow %in% c(2, 3, 4) ~ 0)) %>%
group_by(country) %>%
mean_ci(imp, wt = weight, ci = .84) %>%
mutate(type = "Follow Local Events")
intlevents_data <- gap %>%
mutate(imp = case_when(citizen_intlevents == 1 ~ 1,
citizen_intlevents %in% c(2, 3, 4) ~ 0)) %>%
group_by(country) %>%
mean_ci(imp, wt = weight, ci = .84) %>%
mutate(type = "Follow Intern. Events")
environment_data <- gap %>%
mutate(imp = case_when(citizen_environment == 1 ~ 1,
citizen_environment %in% c(2, 3, 4) ~ 0)) %>%
group_by(country) %>%
mean_ci(imp, wt = weight, ci = .84) %>%
mutate(type = "Reduce Climate Change")
vaccine_data <- gap %>%
mutate(imp = case_when(citizen_vaccine == 1 ~ 1,
citizen_vaccine %in% c(2, 3, 4) ~ 0)) %>%
group_by(country) %>%
mean_ci(imp, wt = weight, ci = .84) %>%
mutate(type = "Getting COVID Vaccine")
# Combine all the results into one dataframe
all <- bind_rows(protest_data, religion_data, vote_data, follow_data, intlevents_data, environment_data, vaccine_data)
color_mapping <- c(
"Attending Religious Services" = "#FEDC97",
"Follow Intern. Events" = "#5f2680",
"Follow Local Events" = "#B5B682",
"Getting COVID Vaccine" = "#7D3C98",
"Joining Demonstrations" = "#033f63",
"Reduce Climate Change" = "#28666e",
"Voting" = "#5E4A78"
)
all %>%
mutate(lab= round(mean, 2)) %>%
ggplot(., aes(x = type, y = mean, fill = type)) +
geom_col(color = "black") +
facet_wrap(~ country) +
coord_flip() +
y_pct() +
geom_text(aes(y = mean + .1, label = paste0(lab*100, '%')), position = position_dodge(width = .9), size = 5, family = "font") +
scale_fill_manual(values = color_mapping) +
theme_rb() +
labs(x = "", y = "", title = "Share Saying These Things Are 'Very Important' to Being Good Members of a Society", caption = "@ryanburge + @religiondata\nData: Global Attitudes Project, Spring 2022")
save("gap_activity_types.png", ht = 10, wd = 14)
# Continuing the country-specific mapping for religion codes
map_religion <- function(country, rel_column) {
case_when(
# Australia
country == "Australia" & rel_column == 1 ~ "Roman Catholic",
country == "Australia" & rel_column == 2 ~ "Protestant",
country == "Australia" & rel_column == 3 ~ "Orthodox Christian",
country == "Australia" & rel_column == 4 ~ "Atheist",
country == "Australia" & rel_column == 5 ~ "Agnostic",
country == "Australia" & rel_column == 6 ~ "Muslim",
country == "Australia" & rel_column == 7 ~ "Something else",
country == "Australia" & rel_column == 8 ~ "Nothing in particular",
country == "Australia" & rel_column == 9 ~ "Christian, non-specific",
country == "Australia" & rel_column == 10 ~ "Jehovah's Witness",
country == "Australia" & rel_column == 11 ~ "Baha'i",
country == "Australia" & rel_column == 12 ~ "Hindu",
country == "Australia" & rel_column == 13 ~ "Buddhist",
country == "Australia" & rel_column == 14 ~ "Sikh",
country == "Australia" & rel_column == 15 ~ "Jewish",
country == "Australia" & rel_column %in% c(98, 99) ~ "Don't know/Refused",
# Belgium
country == "Belgium" & rel_column == 1 ~ "Roman Catholic",
country == "Belgium" & rel_column == 2 ~ "Protestant",
country == "Belgium" & rel_column == 3 ~ "Muslim",
country == "Belgium" & rel_column == 4 ~ "Jewish",
country == "Belgium" & rel_column == 5 ~ "Buddhist",
country == "Belgium" & rel_column == 7 ~ "Atheist",
country == "Belgium" & rel_column == 8 ~ "Agnostic",
country == "Belgium" & rel_column == 9 ~ "Something else",
country == "Belgium" & rel_column == 10 ~ "Nothing in particular",
country == "Belgium" & rel_column == 11 ~ "Just a Christian",
country == "Belgium" & rel_column %in% c(98, 99) ~ "Don't know/Refused",
# Canada
country == "Canada" & rel_column == 1 ~ "Roman Catholic",
country == "Canada" & rel_column == 2 ~ "Protestant",
country == "Canada" & rel_column == 3 ~ "Christian Orthodox",
country == "Canada" & rel_column == 4 ~ "Jewish",
country == "Canada" & rel_column == 5 ~ "Muslim",
country == "Canada" & rel_column == 6 ~ "Sikh",
country == "Canada" & rel_column == 7 ~ "Hindu",
country == "Canada" & rel_column == 8 ~ "Buddhist",
country == "Canada" & rel_column == 9 ~ "Atheist",
country == "Canada" & rel_column == 10 ~ "Agnostic",
country == "Canada" & rel_column == 11 ~ "Something else",
country == "Canada" & rel_column == 12 ~ "Nothing in particular",
country == "Canada" & rel_column == 13 ~ "Just a Christian",
country == "Canada" & rel_column %in% c(98, 99) ~ "Don't know/Refused",
# France
country == "France" & rel_column == 1 ~ "Roman Catholic",
country == "France" & rel_column == 2 ~ "Protestant",
country == "France" & rel_column == 3 ~ "Muslim",
country == "France" & rel_column == 4 ~ "Jewish",
country == "France" & rel_column == 5 ~ "Atheist",
country == "France" & rel_column == 6 ~ "Agnostic",
country == "France" & rel_column == 7 ~ "Something else",
country == "France" & rel_column == 8 ~ "Nothing in particular",
country == "France" & rel_column == 9 ~ "Just a Christian",
country == "France" & rel_column == 10 ~ "Orthodox Christian",
country == "France" & rel_column == 11 ~ "Buddhist",
country == "France" & rel_column %in% c(98, 99) ~ "Don't know/Refused",
# Germany
country == "Germany" & rel_column == 1 ~ "Roman Catholic",
country == "Germany" & rel_column == 2 ~ "Protestant",
country == "Germany" & rel_column == 3 ~ "Muslim",
country == "Germany" & rel_column == 4 ~ "Jewish",
country == "Germany" & rel_column == 5 ~ "Atheist",
country == "Germany" & rel_column == 6 ~ "Agnostic",
country == "Germany" & rel_column == 7 ~ "Something else",
country == "Germany" & rel_column == 8 ~ "Nothing in particular",
country == "Germany" & rel_column == 9 ~ "Just a Christian",
country == "Germany" & rel_column == 10 ~ "Orthodox Christian",
country == "Germany" & rel_column == 11 ~ "Buddhist",
country == "Germany" & rel_column == 12 ~ "Hindu",
country == "Germany" & rel_column %in% c(98, 99) ~ "Don't know/Refused",
# Greece
country == "Greece" & rel_column == 1 ~ "Orthodox Christian",
country == "Greece" & rel_column == 2 ~ "Roman Catholic",
country == "Greece" & rel_column == 4 ~ "Muslim",
country == "Greece" & rel_column == 5 ~ "Atheist",
country == "Greece" & rel_column == 6 ~ "Agnostic",
country == "Greece" & rel_column == 7 ~ "Something else",
country == "Greece" & rel_column == 8 ~ "Nothing in particular",
country == "Greece" & rel_column == 9 ~ "Just a Christian",
country == "Greece" & rel_column %in% c(98, 99) ~ "Don't know/Refused",
# Italy
country == "Italy" & rel_column == 1 ~ "Roman Catholic",
country == "Italy" & rel_column == 2 ~ "Protestant",
country == "Italy" & rel_column == 3 ~ "Orthodox Christian",
country == "Italy" & rel_column == 4 ~ "Muslim",
country == "Italy" & rel_column == 6 ~ "Atheist",
country == "Italy" & rel_column == 7 ~ "Agnostic",
country == "Italy" & rel_column == 8 ~ "Something else",
country == "Italy" & rel_column == 9 ~ "Nothing in particular",
country == "Italy" & rel_column == 10 ~ "Just a Christian",
country == "Italy" & rel_column %in% c(98, 99) ~ "Don't know/Refused",
# South Korea
country == "South Korea" & rel_column == 1 ~ "Buddhist",
country == "South Korea" & rel_column == 2 ~ "Roman Catholic",
country == "South Korea" & rel_column == 3 ~ "Protestant",
country == "South Korea" & rel_column == 5 ~ "Confucianism or traditional religion",
country == "South Korea" & rel_column == 6 ~ "Atheist",
country == "South Korea" & rel_column == 7 ~ "Agnostic",
country == "South Korea" & rel_column == 8 ~ "Something else",
country == "South Korea" & rel_column == 9 ~ "Nothing in particular",
country == "South Korea" & rel_column == 10 ~ "Just a Christian",
country == "South Korea" & rel_column %in% c(98, 99) ~ "Don't know/Refused",
# Israel
country == "Israel" & rel_column == 1 ~ "Jewish",
country == "Israel" & rel_column == 2 ~ "Muslim",
country == "Israel" & rel_column == 3 ~ "Orthodox Christian",
country == "Israel" & rel_column == 4 ~ "Roman Catholic",
country == "Israel" & rel_column == 5 ~ "Druze",
country == "Israel" & rel_column == 6 ~ "Atheist",
country == "Israel" & rel_column == 8 ~ "Something else",
country == "Israel" & rel_column == 10 ~ "Just a Christian",
country == "Israel" & rel_column %in% c(98, 99) ~ "Don't know/Refused",
country == "Poland" & rel_column == 1 ~ "Roman Catholic",
country == "Poland" & rel_column == 2 ~ "Orthodox Christian",
country == "Poland" & rel_column == 4 ~ "Jehovah's Witness",
country == "Poland" & rel_column == 10 ~ "Atheist",
country == "Poland" & rel_column == 11 ~ "Agnostic",
country == "Poland" & rel_column == 13 ~ "Nothing in particular",
country == "Poland" & rel_column == 14 ~ "Just a Christian",
country == "Poland" & rel_column %in% c(98, 99) ~ "Don't know/Refused",
# Singapore
country == "Singapore" & rel_column == 1 ~ "Buddhist",
country == "Singapore" & rel_column == 2 ~ "Muslim",
country == "Singapore" & rel_column == 3 ~ "Protestant",
country == "Singapore" & rel_column == 4 ~ "Roman Catholic",
country == "Singapore" & rel_column == 5 ~ "Hindu",
country == "Singapore" & rel_column == 6 ~ "Traditional religion",
country == "Singapore" & rel_column == 7 ~ "Atheist",
country == "Singapore" & rel_column == 8 ~ "Agnostic",
country == "Singapore" & rel_column == 9 ~ "Something else",
country == "Singapore" & rel_column == 10 ~ "Nothing in particular",
country == "Singapore" & rel_column == 11 ~ "Just a Christian",
country == "Singapore" & rel_column == 13 ~ "Taoism",
country == "Singapore" & rel_column %in% c(98, 99) ~ "Don't know/Refused",
# South Korea
country == "South Korea" & rel_column == 1 ~ "Buddhist",
country == "South Korea" & rel_column == 2 ~ "Roman Catholic",
country == "South Korea" & rel_column == 3 ~ "Protestant",
country == "South Korea" & rel_column == 5 ~ "Confucianism or traditional religion",
country == "South Korea" & rel_column == 6 ~ "Atheist",
country == "South Korea" & rel_column == 7 ~ "Agnostic",
country == "South Korea" & rel_column == 8 ~ "Something else",
country == "South Korea" & rel_column == 9 ~ "Nothing in particular",
country == "South Korea" & rel_column == 10 ~ "Just a Christian",
country == "South Korea" & rel_column == 11 ~ "Mormon",
country == "South Korea" & rel_column %in% c(98, 99) ~ "Don't know/Refused",
# Spain
country == "Spain" & rel_column == 1 ~ "Roman Catholic",
country == "Spain" & rel_column == 2 ~ "Protestant",
country == "Spain" & rel_column == 3 ~ "Muslim",
country == "Spain" & rel_column == 4 ~ "Jewish",
country == "Spain" & rel_column == 5 ~ "Atheist",
country == "Spain" & rel_column == 6 ~ "Agnostic",
country == "Spain" & rel_column == 7 ~ "Something else",
country == "Spain" & rel_column == 8 ~ "Nothing in particular",
country == "Spain" & rel_column == 9 ~ "Just a Christian",
country == "Spain" & rel_column %in% c(98, 99) ~ "Don't know/Refused",
# Sweden
country == "Sweden" & rel_column == 1 ~ "Lutheran (Church of Sweden)",
country == "Sweden" & rel_column == 2 ~ "Roman Catholic",
country == "Sweden" & rel_column == 3 ~ "Congregationalist",
country == "Sweden" & rel_column == 4 ~ "Pentecostal",
country == "Sweden" & rel_column == 5 ~ "Orthodox Christian",
country == "Sweden" & rel_column == 6 ~ "Muslim",
country == "Sweden" & rel_column == 7 ~ "Atheist",
country == "Sweden" & rel_column == 8 ~ "Agnostic",
country == "Sweden" & rel_column == 9 ~ "Something else",
country == "Sweden" & rel_column == 10 ~ "Nothing in particular",
country == "Sweden" & rel_column == 11 ~ "Just a Christian",
country == "Sweden" & rel_column %in% c(98, 99) ~ "Don't know/Refused",
# UK
country == "UK" & rel_column == 1 ~ "Roman Catholic",
country == "UK" & rel_column == 2 ~ "Protestant",
country == "UK" & rel_column == 3 ~ "Muslim",
country == "UK" & rel_column == 4 ~ "Atheist",
country == "UK" & rel_column == 5 ~ "Agnostic",
country == "UK" & rel_column == 6 ~ "Something else",
country == "UK" & rel_column == 7 ~ "Nothing in particular",
country == "UK" & rel_column == 8 ~ "Just a Christian",
country == "UK" & rel_column == 9 ~ "Jewish",
country == "UK" & rel_column == 10 ~ "Hindu",
country == "UK" & rel_column == 11 ~ "Buddhist",
country == "UK" & rel_column == 12 ~ "Sikh",
country == "UK" & rel_column == 13 ~ "Orthodox Christian",
country == "UK" & rel_column %in% c(98, 99) ~ "Don't know/Refused",
TRUE ~ "Other"
)
}
library(dplyr)
# Function to process data for each country and group small categories
process_country_data <- function(data, country_name, religion_var, threshold = 5) {
data %>%
filter(country == country_name) %>%
mutate(standardized_religion = map_religion(country_name, !!sym(religion_var))) %>%
filter(!standardized_religion %in% c("Just a Christian", "Don't know/Refused", "Something else")) %>%
group_by(standardized_religion) %>%
summarize(count = n()) %>%
mutate(total = sum(count)) %>%
mutate(pct = count / total * 100) %>%
# Recode small categories into "All Others"
mutate(standardized_religion = ifelse(pct < threshold, "All Others", standardized_religion)) %>%
group_by(standardized_religion) %>% # Regroup after recoding
summarize(count = sum(count), pct = sum(count) / total[1] * 100) %>%
mutate(country = country_name)
}
# List of countries and religion variables
countries <- c("Australia", "Belgium", "Canada", "France", "Germany",
"Greece", "Hungary", "Israel", "Italy", "Japan",
"Malaysia", "Netherlands", "Poland", "Singapore",
"South Korea", "Spain", "Sweden", "UK")
religion_vars <- c("d_relig_australia", "d_relig_belgium", "d_relig_canada",
"d_relig_france", "d_relig_germany", "d_relig_greece",
"d_relig_hungary", "d_relig_israel", "d_relig_italy",
"d_relig_japan", "d_relig_malaysia", "d_relig_netherlands",
"d_relig_poland", "d_relig_singapore", "d_relig_skorea_a",
"d_relig_spain", "d_relig_sweden", "d_relig_uk")
# Initialize an empty list to store results
all_results <- list()
# Loop through each country and religion variable
for (i in seq_along(countries)) {
country_name <- countries[i]
religion_var <- religion_vars[i]
# Process data for each country, setting a threshold of 5% for small categories
result <- process_country_data(gap, country_name, religion_var, threshold = 5)
# Append result to the list
all_results[[i]] <- result
}
# Combine all results into a single data frame
final_results <- bind_rows(all_results)
final_results <- final_results %>%
filter(country != "Hungary") %>%
filter(country != "Japan") %>%
filter(country != "Malaysia") %>%
filter(country != "Sweden") %>%
filter(country != "Netherlands")
hungary <- gap %>%
filter(country == "Hungary") %>%
mutate(rel = d_relig_hungary) %>%
mutate(rel = frcode(rel == 1 ~ "Roman Catholic",
rel == 2 | rel == 3 ~ "Protestant",
rel == 7 ~ "Atheist",
rel == 8 ~ "Agnostic",
rel == 10 ~ "Nothing in particular",
TRUE ~ "All Others")) %>%
ct(rel, wt = weight) %>%
rename(standardized_religion = rel) %>%
rename(count = n) %>%
mutate(pct = pct*100) %>%
mutate(country = "Hungary")
japan <- gap %>%
filter(country == "Japan") %>%
mutate(rel = d_relig_japan) %>%
mutate(rel = frcode(rel == 1 ~ "Buddhist",
rel == 8 ~ "Nothing in particular",
TRUE ~ "All Others")) %>%
ct(rel, wt = weight) %>%
rename(standardized_religion = rel) %>%
rename(count = n) %>%
mutate(pct = pct*100) %>%
mutate(country = "Japan")
malaysia <- gap %>%
filter(country == "Malaysia") %>%
mutate(rel = d_relig_malaysia) %>%
mutate(rel = frcode(rel == 1 ~ "Muslim",
rel == 2 ~ "Buddhist",
rel == 3 ~ "Hindu",
TRUE ~ "All Others")) %>%
ct(rel, wt = weight) %>%
rename(standardized_religion = rel) %>%
rename(count = n) %>%
mutate(pct = pct*100) %>%
mutate(country = "Malaysia")
sweden <- gap %>%
filter(country == "Sweden") %>%
mutate(rel = d_relig_sweden) %>%
mutate(rel = frcode(rel == 1 | rel == 3 | rel == 4 ~ "Protestant",
rel == 7 ~ "Atheist",
rel == 8 ~ "Agnostic",
rel == 10 ~ "Nothing in particular",
TRUE ~ "All Others")) %>%
ct(rel, wt = weight) %>%
rename(standardized_religion = rel) %>%
rename(count = n) %>%
mutate(pct = pct*100) %>%
mutate(country = "Sweden")
neth <- gap %>%
filter(country == "Netherlands") %>%
mutate(rel = d_relig_netherlands) %>%
mutate(rel = frcode(rel == 1 ~ "Roman Catholic",
rel == 2 | rel == 3 ~ "Protestant",
rel == 8 ~ "Atheist",
rel == 9 ~ "Agnostic",
rel == 11 ~ "Nothing in particular",
TRUE ~ "All Others")) %>%
ct(rel, wt = weight) %>%
rename(standardized_religion = rel) %>%
rename(count = n) %>%
mutate(pct = pct*100) %>%
mutate(country = "Netherlands")
five <- bind_rows(hungary, japan, malaysia, sweden, neth)
gg <- bind_rows(final_results, five)
gg <- gg %>%
mutate(pct = pct/100)
gg <- gg %>%
mutate(rel = as.factor(standardized_religion))
color_mapping_rel <- c(
"Agnostic" = "#28666e", # Keeping this one, it's distinct
"All Others" = "#5f2680", # Keeping for consistency
"Atheist" = "#f08080", # Light Coral
"Buddhist" = "#ffa500", # Orange
"Hindu" = "#ffd700", # Gold
"Jewish" = "#4b0082", # Indigo
"Muslim" = "#008000", # Green
"Nothing in particular" = "#800080", # Sienna
"Orthodox Christian" = "#a0522d", # Purple
"Protestant" = "#5f2680", # Same as All Others
"Roman Catholic" = "#add8e6" # Light Blue
)
gg <- gg %>%
mutate(rel = fct_relevel(rel,
"Roman Catholic", "Protestant", "Orthodox Christian",
"Jewish", "Muslim", "Buddhist", "Hindu",
"Atheist", "Agnostic", "Nothing in particular", "All Others"))
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() +
theme(legend.position = "bottom") +
scale_fill_manual(values = color_mapping_rel) +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0)) +
guides(fill = guide_legend(reverse=T, nrow = 2)) +
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") +
geom_text(aes(label = ifelse(pct > .05 & rel == "Protestant", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "white") +
geom_text(aes(label = ifelse(pct > .05 & rel == "Jewish", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "white") +
geom_text(aes(label = ifelse(pct > .05 & rel == "Orthodox Christian", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "white") +
geom_text(aes(label = ifelse(pct > .05 & rel == "All Others", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "white") +
geom_text(aes(label = ifelse(pct > .05 & rel == "Nothing in particular", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, 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 18 Countries", caption = "@ryanburge + @religiondata\nData: Global Attitudes Project, Spring 2022")
save("rel_comp_18_countries.png", wd = 9, ht = 8)
gg <- gap %>%
group_by(country) %>%
mutate(imp = religion_import) %>%
mutate(imp = frcode(imp == 4 ~ "Not at all",
imp == 3 ~ "Not too",
imp == 2 ~ "Somewhat",
imp == 1 ~ "Very important")) %>%
ct(imp, wt = weight, show_na = FALSE)
lvl <- gg %>%
filter(imp == "Not at all") %>%
select(country, sort = pct)
gg <- left_join(gg, lvl)
gg$country <- fct_reorder(gg$country, gg$sort, .desc = TRUE)
lighten_color <- function(color, factor = 0.3) {
col <- col2rgb(color)
col <- col + (255 - col) * factor
rgb(t(col), maxColorValue = 255)
}
# Apply lightened colors to your palette
lightened_colors <- c(
lighten_color("#033f63", 0.3),
lighten_color("#28666e", 0.3),
lighten_color("#7D3C98", 0.3),
lighten_color("#5f2680", 0.3)
)
distinguishable_colors <- diverging_hcl(4, palette = "Purple-Blue")
gg %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(imp))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ country, ncol =1, strip.position = "left") +
scale_fill_manual(values = lightened_colors) +
theme_rb() +
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 = 4, family = "font", color = "white") +
labs(x = "", y = "", title = "How important is religion in your life?", caption = "@ryanburge + @religiondata\nData: Global Attitudes Project, Spring 2022")
save("rel_importance.png", wd = 9, ht = 6)
library(dplyr)
# Adding the region variable
gap <- gap %>%
mutate(region = case_when(
country %in% c("Australia") ~ "Australia",
country %in% c("Belgium", "France", "Germany", "Greece", "Hungary", "Italy", "Netherlands", "Poland", "Spain", "Sweden", "UK") ~ "Europe",
country %in% c("Canada") ~ "Canada",
country %in% c("Israel") ~ "Israel",
country %in% c("Japan", "Malaysia", "Singapore", "South Korea") ~ "Asia",
TRUE ~ "Other" # If there are other countries, assign them to "Other"
))
one <- gap %>%
filter(country != "Australia") %>%
mutate(age2 = frcode(age >= 18 & age <= 29 ~ "18-29",
age >= 30 & age <= 49 ~ "30-49",
age >= 50 & age <= 64 ~ "50-64",
age >= 65 ~ "65+")) %>%
mutate(imp = religion_import) %>%
mutate(imp = case_when(imp == 4 ~ 1,
imp == 1 | imp == 2 | imp == 3 ~ 0)) %>%
group_by(country, age2) %>%
mean_ci(imp, wt = weight, ci = .84)
two <- gap %>%
filter(country == "Australia") %>%
mutate(age2 = age_australia) %>%
mutate(age2 = frcode(age2 == 1 ~ "18-29",
age2 == 2 ~ "30-49",
age2 == 3 ~ "50-64",
age2 == 4 ~ "65+")) %>%
mutate(imp = religion_import) %>%
mutate(imp = case_when(imp == 4 ~ 1,
imp == 1 | imp == 2 | imp == 3 ~ 0)) %>%
group_by(country, age2) %>%
mean_ci(imp, wt = weight, ci = .84) %>% filter(age2 != "NA")
both <- bind_rows(one, two)
color_mapping <- c(
"Belgium" = "#e1e1e1",
"Canada" = "#28666e",
"France" = "#5f2680",
"Germany" = "#B5B682",
"Greece" = "#FEDC97",
"Hungary" = "#7D3C98",
"Israel" = "#5f2680",
"Italy" = "#033f63",
"Japan" = "#28666e",
"Malaysia" = "#e1e1e1",
"Netherlands" = "#B5B682",
"Poland" = "#FEDC97",
"Singapore" = "#7D3C98",
"South Korea" = "#5f2680",
"Spain" = "#033f63",
"Sweden" = "#28666e",
"UK" = "#e1e1e1",
"Australia" = "#B5B682"
)
both %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = reorder(country, mean), y = mean, fill = country)) +
geom_col(color = "black") +
facet_wrap(~ age2) +
coord_flip() +
scale_fill_manual(values = color_mapping) +
theme_rb() +
y_pct() +
theme(strip.text = element_text(size = 20)) +
geom_text(aes(y = mean + .035, label = paste0(lab*100, '%')), position = position_dodge(width = .9), size = 5, family = "font") +
labs(x = "", y = "", title = "Share Saying Religion is Not Important at All by Age and Country", caption = "@ryanburge + @religiondata\nData: Global Attitudes Project, Spring 2022")
save("rel_imp_18_countries_arda.png", ht = 10)
library(ggpubr)
both %>%
filter(age2 == "18-29" | age2 == "65+") %>%
ggplot(., aes(x = age2, y = mean, fill = country)) +
geom_col(color = 'black') +
facet_wrap(~ country)
one <- both %>%
filter(age2 == "18-29") %>%
select(country, young = mean)
two <- both %>%
filter(age2 == "65+") %>%
select(country, old = mean)
lab <- left_join(one, two) %>%
mutate(diff = old - young) %>%
mutate(diff = round(diff, 2)) %>%
mutate(diff = diff*100) %>%
mutate(diff = abs(diff)) %>%
mutate(diff = paste0(diff, "%")) %>%
select(country, diff)
ttt <- left_join(both, lab)
library(ggpubr)
ttt %>%
filter(age2 == "18-29" | age2 == "65+") %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = age2, y = mean, fill = country)) +
geom_col(color = "black") +
facet_wrap(~ country) +
theme_rb() +
geom_bracket(
data = ttt,
aes(label = ifelse(country %in% c("Belgium", "Israel"), paste0("+", diff), diff)),
xmin = 1, xmax = 2, y.position = .675, label.size = 7, size = 1, family = "font"
) +
scale_y_continuous(labels = percent, limits = c(0, .75)) +
scale_fill_manual(values = color_mapping) +
geom_text(aes(y = mean + .075, label = paste0(lab*100, '%')), position = position_dodge(width = .9), size = 9, family = "font") +
# geom_text(aes(y = .075, label = ifelse(rel == "Religious", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 9, family = "font", color = "white") +
theme(strip.text = element_text(size = 20)) +
labs(x = "", y = "", title = "Share Saying Religion is Not Important at All Among the Youngest and Oldest Adults", caption = "@ryanburge + @religiondata\nData: Global Attitudes Project, Spring 2022")
save("compare_imp_young_old.png", wd = 9, ht = 10)
gg <- gap %>%
mutate(rel = citizen_religion) %>%
mutate(rel = case_when(rel == 4 ~ 1,
rel == 1 | rel == 2 | rel == 3 ~ 0)) %>%
group_by(country) %>%
socsci::mean_ci(rel, wt = weight, ci = .84)
color_mapping <- c(
"Belgium" = "#e1e1e1",
"Canada" = "#28666e",
"France" = "#5f2680",
"Germany" = "#B5B682",
"Greece" = "#FEDC97",
"Hungary" = "#7D3C98",
"Israel" = "#5f2680",
"Italy" = "#033f63",
"Japan" = "#28666e",
"Malaysia" = "#e1e1e1",
"Netherlands" = "#B5B682",
"Poland" = "#FEDC97",
"Singapore" = "#7D3C98",
"South Korea" = "#5f2680",
"Spain" = "#033f63",
"Sweden" = "#28666e",
"UK" = "#e1e1e1",
"Australia" = "#B5B682"
)
gg %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = reorder(country, mean), y = mean, fill = country)) +
geom_col(color = "black") +
coord_flip() +
theme_rb() +
error_bar() +
y_pct() +
lab_bar(top = FALSE, type = lab, pos = .02, sz = 6) +
geom_text(aes(y = .02, label = ifelse(country == "Spain", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 6, family = "font", color = "white") +
geom_text(aes(y = .02, label = ifelse(country == "France", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 6, family = "font", color = "white") +
geom_text(aes(y = .02, label = ifelse(country == "Hungary", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 6, family = "font", color = "white") +
geom_text(aes(y = .02, label = ifelse(country == "Israel", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 6, family = "font", color = "white") +
geom_text(aes(y = .02, label = ifelse(country == "South Korea", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 6, family = "font", color = "white") +
geom_text(aes(y = .02, label = ifelse(country == "Singapore", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 6, family = "font", color = "white") +
geom_text(aes(y = .02, label = ifelse(country == "Italy", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 6, family = "font", color = "white") +
geom_text(aes(y = .02, label = ifelse(country == "Canada", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 6, family = "font", color = "white") +
geom_text(aes(y = .02, label = ifelse(country == "Sweden", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 6, family = "font", color = "white") +
geom_text(aes(y = .02, label = ifelse(country == "Japan", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 6, family = "font", color = "white") +
scale_fill_manual(values = color_mapping) +
theme(plot.title = element_text(size = 14)) +
labs(x = "", y = "", title = "Share Saying Attending Religious Services is Not at All Important to be a Good Member of Society", caption = "@ryanburge + @religiondata\nData: Global Attitudes Project, Spring 2022")
save("good_member_rel_gap.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment