Skip to content

Instantly share code, notes, and snippets.

@mschnetzer
Created March 30, 2020 12:37
Show Gist options
  • Save mschnetzer/ed6cc960b03dcd209000ccfa77a7b77b to your computer and use it in GitHub Desktop.
Save mschnetzer/ed6cc960b03dcd209000ccfa77a7b77b to your computer and use it in GitHub Desktop.
Overview of the new HFCS 2017 data
library(tidyverse)
library(readxl)
library(countrycode)
library(msthemes)
library(patchwork)
ineq <- read_xlsx("HFCS - Statistical tables - wave 2017 - March 2020.xlsx",
sheet = "J4 Net wealth inequality ind", skip = 2) %>%
filter(...2 %in% c("Top 10% share", "Gini coefficient")) %>%
select(-c(...1,...3,...4,`euro\r\narea`), measure = ...2) %>%
gather(cty, value, BE:FI) %>% spread(measure,value)
nw <- read_xlsx("HFCS - Statistical tables - wave 2017 - March 2020.xlsx",
sheet = "J3 Net wealth per household ", skip = 2) %>%
filter(...1 %in% c("p20", "p50", "p80")) %>%
select(-c(...2,...3,`euro\r\narea`), measure = ...1) %>%
gather(cty, value, BE:FI) %>% spread(measure,value)
df <- left_join(ineq,nw) %>%
left_join(codelist %>% select(iso2c,country.name.de, country.name.en),
by = c("cty" = "iso2c")) %>%
mutate_at(vars(-starts_with("c")), ~as.numeric(.)) %>%
rename(gini = `Gini coefficient`, topten = `Top 10% share`)
## Map Gini
europe <- map_data("world")
euromap <- left_join(europe, df %>% select(country.name.en,gini),
by = c("region"="country.name.en"))
eumap <- ggplot() +
geom_polygon(data = euromap,
aes(x = long, y = lat, group = group,
fill = gini),
color = "gray60", size = 0.1) +
scale_fill_viridis_c(na.value = "gray80", name = "Gini-Index",
guide = guide_colorbar(barwidth = unit(35,"mm"),
barheight = unit(1.5,"mm"),
title.position = "top",
direction = "horizontal",
label.hjust = 0.5)) +
coord_fixed(xlim = c(-9, 35),
ylim = c(36, 70.1),
ratio = 1.5) +
theme_ms() +
theme(panel.background = element_rect(fill = "lightcyan1", color = NA),
axis.title = element_blank(),
axis.text = element_blank(),
legend.position = c(0.25,0.9),
legend.title = element_text(size=7),
legend.text = element_text(size=5),
legend.background = element_rect(fill="white",color="black",size=0.1))
## Number of observations
bars <- df %>%
ggplot(aes(x = reorder(cty, topten), y = topten)) +
geom_linerange(aes(ymin=30, ymax=topten), color=msc_palette[4], size=1) +
geom_point(size=2, color= msc_palette[4]) +
geom_point(size=1, color= msc_palette[3]) +
coord_flip() +
theme_ms() +
theme(axis.title.y = element_blank(),
axis.title.x = element_text(size=7),
axis.text = element_text(size = 7),
panel.grid.major.y = element_blank()) +
geom_curve(aes(x = 5, y= 57, xend = 18.4, yend = 56.6),
size = 0.1, curvature = 0.2,
arrow = arrow(length=unit(0.02,"npc"), type="closed")) +
annotate("label",x= 5 ,y= 56, label="Austria's richest 10% \n own 56% of net wealth", size=2) +
labs(y="Share of richest 10% in net wealth")
bars2 <- df %>% gather(measure,value,p20:p80) %>%
ggplot(aes(x = reorder(cty, value),y=value)) +
geom_line(aes(group=cty), size = 0.1) +
geom_point(aes(color=measure, shape=measure)) +
scale_color_manual(name="", labels=c("P20","Median","P80"),
values = msc_palette[c(1,4,5)]) +
scale_shape_manual(name="", labels=c("P20","Median","P80"),
values = c(15,19,17)) +
scale_y_continuous(breaks=seq(0,1000,200)) +
theme_ms() +
theme(axis.title.x = element_blank(),
axis.title.y = element_text(size = 7),
axis.text = element_text(size = 7),
panel.grid.major.x = element_blank(),
legend.direction = "horizontal",
legend.text = element_text(size=6),
# legend.background = element_rect(fill="white",color="black",size=0.1),
legend.position = c(0.3,0.6)) +
labs(y="Net wealth in 1,000 €")
finplot <- ((bars / bars2) | eumap) +
plot_annotation(title = "Wealth distribution in Europe",
caption = "Data: ECB, HFCS 2017. Figure: @matschnetzer",
theme = theme_ms(alttf = T))
ggsave(finplot, file = "hfcs2017.png", dpi = 300, width = 8, height = 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment