Skip to content

Instantly share code, notes, and snippets.

@expersso
Last active November 30, 2015 17:13
Show Gist options
  • Save expersso/e758a32dab9b806121bf to your computer and use it in GitHub Desktop.
Save expersso/e758a32dab9b806121bf to your computer and use it in GitHub Desktop.
Relationship between wealth and tolerance
lapply(c("WDI", "dplyr", "stringr", "ggplot2", "tidyr", "ggthemes"), library,
character.only = TRUE)
library(wvs) # local package
# Get shares who mentioned specific groups when asked who they would not want
# as a neighbour
values <- wvs %>%
filter(wave %in% c("2005-2009", "2010-2014")) %>%
group_by(country.abbreviation) %>%
summarise(race = sum(neighbours..people.of.a.different.race == "Mentioned") / n(),
homo = sum(neighbours..homosexuals == "Mentioned") / n())
# Get GDP per capita (NB: relatively large number of NAs)
df <- WDI(indicator = "NY.GDP.PCAP.PP.KD", start = 2005, end = 2014,
extra = TRUE) %>% tbl_df()
names(df)[3] <- "gdp"
# Calculate mean GDP per capita for period 2005-2014
df_sum <- df %>%
group_by(iso2c, country, region, income) %>%
summarise(mgdp = mean(gdp, na.rm = TRUE))
# Join together datasets
df_comb <- left_join(df_sum, values, by = c("iso2c" = "country.abbreviation"))
df_plot <- df_comb %>%
filter(region != "Aggregates") %>%
gather(key = question, value = value,
-iso2c, -country, -region, -income, -mgdp) %>%
mutate(question = ifelse(question == "race",
"People of another race",
"Homosexuals")) %>%
filter(value != 0)
ggplot(df_plot, aes(x = mgdp, y = value * 100, label = country)) +
geom_smooth(method = "lm", alpha = 0.05,
fill = "steelblue",
color = "lightblue") +
geom_text(aes(color = region), size = 3) +
scale_x_log10(labels = comma) +
scale_color_few() +
facet_wrap(~question) +
theme(legend.position = "bottom") +
guides(color = guide_legend(override.aes = list(fill = 1))) +
labs(x = "\nGDP per capita (PPP, average 2005-2014)",
y = "% share of respondents mentioning group\n", color = NULL,
title = bquote(atop(bold(.("Relationship between wealth and tolerance")),
scriptstyle(.(paste0("% share of respondents that would not want ",
"a homosexual or a person of another race as",
" a neighbour"))))))
@expersso
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment