Skip to content

Instantly share code, notes, and snippets.

@mschnetzer
Created August 30, 2018 17:49
Show Gist options
  • Save mschnetzer/ab5aa0252194d527c574f26e197fcab9 to your computer and use it in GitHub Desktop.
Save mschnetzer/ab5aa0252194d527c574f26e197fcab9 to your computer and use it in GitHub Desktop.
Mean age at first marriage, 1990-2014
library(tidyverse)
library(msthemes)
library(eurostat)
# Download data from UNECE: http://bit.ly/2wtn9Sp
data <- read.csv("marriage.csv",skip=1,na.strings=c("","..","NA"),dec=".")
data <- data %>% gather(year,value, -Sex, -Country)
data$year <- as.numeric(substring(data$year, 2))
# Change in EU countries where data is available
dataeu <- data %>% filter(Country %in% eu_countries$name, year %in% c(1990,2014)) %>%
group_by(Country,year) %>% summarise(mw=mean(value)) %>% filter(!Country %in% c("France","Luxembourg","Malta","United Kingdom")) %>%
arrange(mw) %>% ungroup() %>% mutate(Country = factor(Country, levels = unique(.$Country)))
# Create labels
right_label <- dataeu %>%
group_by(Country) %>%
arrange(desc(year)) %>%
top_n(1)
left_label <- dataeu %>%
group_by(Country) %>%
arrange(desc(year)) %>%
slice(2)
png("eumarriage.png",width=7,height=4,units="in",res=300)
dataeu %>% ggplot(aes(x=mw, y=Country)) +
geom_point(aes(color = as.factor(year)), size = 2) +
geom_line(aes(group = Country),arrow=arrow(type="closed",length=unit(0.15,"cm"))) +
geom_text(data = right_label, aes(color = as.factor(year), label = round(mw, 1)),
size = 3, hjust = -.5) +
geom_text(data = left_label, aes(color = as.factor(year), label = round(mw, 1)),
size = 3, hjust = 1.5) +
scale_color_msdark() + scale_x_continuous(limits=c(22,35),breaks=seq(22,36,2)) +
labs(x = "", y="",title="Happily ever after",subtitle="Mean age at first marriage, 1990-2014",caption="Data: UNECE. Chart: @matschnetzer") +
theme_ms(grid=T) +
theme(legend.title=element_blank(),legend.position = c(0.9,0.1)) +
guides(color = guide_legend(override.aes = list(size=4)))
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment