Skip to content

Instantly share code, notes, and snippets.

@chrishanretty
Created May 14, 2019 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrishanretty/05fe2a532c066ea68aa7e55a78af178d to your computer and use it in GitHub Desktop.
Save chrishanretty/05fe2a532c066ea68aa7e55a78af178d to your computer and use it in GitHub Desktop.
Plot relationship between inequality and democracy
### This uses code from Marta Kolczynska
### https://martakolczynska.com/post/participation-inequality-indices/
library(rio)
library(tidyverse)
library(vdem)
library(countrycode)
library(hrbrthemes)
vdem_part <- extract_vdem(name_pattern = "v2x_libdem",
include_uncertainty = FALSE) %>%
select(iso3 = vdem_country_text_id,
year,
vdem_par = v2x_libdem)
swiid <- read.csv("https://raw.githubusercontent.com/fsolt/swiid/master/data/swiid7_1_summary.csv",
stringsAsFactors = FALSE, encoding = "UTF-8") %>%
mutate(iso3 = countrycode(country, "country.name", "iso3c")) %>%
mutate(iso3 = ifelse(country == "Kosovo", "XKX", iso3)) %>%
select(iso3, year, gini_disp)
dat <- merge(vdem_part, swiid)
most_recent <- dat %>%
group_by(iso3) %>%
arrange(desc(year)) %>%
filter(row_number() == 1)
mod0 <- lm(vdem_par ~ gini_disp, data = most_recent)
mod1 <- lm(vdem_par ~ poly(gini_disp, 2), data = most_recent)
p1 <- ggplot(most_recent, aes(x = gini_disp, y= vdem_par)) +
geom_hline(yintercept = 0) +
geom_point() +
geom_point(data = subset(most_recent, iso3 == "GBR"),
colour = "red",
size = 2) +
scale_x_continuous("Gini coefficient\n(0-100; higher values indicate greater inequality") +
scale_y_continuous("V-Dem measure of liberal democracy\n(0-1; higher values indicate greater democracy",
limits = c(0, 1)) +
geom_smooth(method = "lm", formula = y ~ poly(x, 2), se = FALSE) +
theme_ipsum_rc() +
labs(title = "The relationship between inequality and democracy is not straightforward",
subtitle = "The most democratic countries are most equal, but some highly unequal countries are also moderately democratic",
caption = "Data: \nV-Dem Project @vdeminstitute\nStandardized World Income Inequality Database @fredericksolt")
ggsave(p1, file = "ineq_levels.png",
width = 1024/300, height = 512/300,
units = "in", dpi = 300, scale = 5)
### Now look at changes
pad <- function(x, len) {
if (length(x) != len) {
return(rep(NA, len))
} else {
return(x)
}
}
changes <- dat %>%
group_by(iso3) %>%
arrange(desc(year)) %>%
filter(row_number() %in% c(1, 20)) %>%
mutate(vdem_chg = pad(-diff(vdem_par), 1),
gini_chg = pad(-diff(gini_disp), 1)) %>%
filter(row_number() == 1) %>%
filter(!is.na(gini_chg)) %>%
filter(!is.na(vdem_chg))
mod0 <- lm(vdem_chg ~ gini_chg, data = changes)
mod1 <- lm(vdem_chg ~ poly(gini_chg, 2), data = changes)
mod2 <- lm(vdem_chg ~ poly(gini_chg, 3), data = changes)
p2 <- ggplot(changes, aes(x = gini_chg, y= vdem_chg)) +
geom_point() +
geom_point(data = subset(changes, iso3 == "GBR"),
colour = "red",
size = 2) +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0) +
scale_x_continuous("Change in Gini coefficient\n(higher values indicate country become more unequal") +
scale_y_continuous("Change in V-Dem measure of liberal democracy\n(positive values indicate country became more democratic") +
geom_smooth(method = "lm", formula = y ~ poly(x, 1), se = FALSE) +
theme_ipsum_rc() +
labs(title = "Changes in inequality over the past twenty years are not related to changes in democracy",
subtitle = "")
ggsave(p2, file = "ineq_changes.png",
width = 1024/300, height = 512/300,
units = "in", dpi = 300, scale = 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment