Skip to content

Instantly share code, notes, and snippets.

@thoughtfulbloke
Created August 17, 2019 05:20
Show Gist options
  • Save thoughtfulbloke/aa6df88d425b6315c2d7618b58d1be39 to your computer and use it in GitHub Desktop.
Save thoughtfulbloke/aa6df88d425b6315c2d7618b58d1be39 to your computer and use it in GitHub Desktop.
library(dplyr)
library(tidyr)
library(ggplot2)
library(forcats)
library(sjlabelled)
library(ggthemes)
w5 <- readRDS("wvs5_2005_2009.rds")
wv5 <- w5 %>%
mutate(Country = as.character(as_label(V2)),
Sex = as_label(V235),
n.child = as_label(V56),
Diversity = sjlabelled::as_label(V221),
Year = as.character(as_label(V260)),
Country = paste(Country, Year),
Year.of.Birth = as.vector(V236),
Age = as.numeric(Year) - Year.of.Birth
) %>%
select(Country, Diversity, Age, n.child, Sex)
wv5 %>% filter(as.numeric(Diversity) > 5, Age < 90, as.numeric(n.child) > 5,
Sex != "No answer") %>%
mutate(children = ifelse(as.numeric(n.child) > 6, "parent", "nonparent"),
diversity = ifelse(as.numeric(Diversity) > 12, "Strongly supportive", "Not strongly supportive")) %>%
group_by(Sex, Country, children) %>%
summarise(av_age = mean(Age), respondents=n(),
diversity_support = 100* sum(diversity == "Strongly supportive") / n()) %>%
ungroup() %>% arrange(Sex, Country, children) %>%
group_by(Sex, Country) %>%
summarise(parent_age_diff = av_age[2] - av_age[1],
diversity_diff = diversity_support[2] - diversity_support[1]) %>%
ungroup() %>%
ggplot(aes(x=parent_age_diff, y=diversity_diff)) + geom_point() + geom_vline(xintercept=15, alpha=0.2) +
geom_smooth(method="lm") + theme_tufte() + facet_wrap(~Sex) + scale_colour_viridis_d() +
ggtitle("Change in average age given parenthood vs change in strong support for ethnic diversity") +
geom_hline(yintercept=0, alpha=0.2) + ylab("change in strong ethnic diversity support with parenthood") +
xlab("difference in mean age between parents and non-parents")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment