Load data

I got data from somewhere (technically Polity IV, but ModernDive doesn’t tell you that)

library(tidyverse)

dem_score <- read_csv("https://moderndive.com/data/dem_score.csv")

dem_score
## # A tibble: 96 x 10
##    country  `1952` `1957` `1962` `1967` `1972` `1977` `1982` `1987` `1992`
##    <chr>     <int>  <int>  <int>  <int>  <int>  <int>  <int>  <int>  <int>
##  1 Albania      -9     -9     -9     -9     -9     -9     -9     -9      5
##  2 Argenti…     -9     -1     -1     -9     -9     -9     -8      8      7
##  3 Armenia      -9     -7     -7     -7     -7     -7     -7     -7      7
##  4 Austral…     10     10     10     10     10     10     10     10     10
##  5 Austria      10     10     10     10     10     10     10     10     10
##  6 Azerbai…     -9     -7     -7     -7     -7     -7     -7     -7      1
##  7 Belarus      -9     -7     -7     -7     -7     -7     -7     -7      7
##  8 Belgium      10     10     10     10     10     10     10     10     10
##  9 Bhutan      -10    -10    -10    -10    -10    -10    -10    -10    -10
## 10 Bolivia      -4     -3     -3     -4     -7     -7      8      9      9
## # ... with 86 more rows

Manipulate data

This data is in wide format. Can sometimes be useful. What country had the biggest change in democracy score between 1952 and 1992?

biggest_jump <- dem_score %>% 
  mutate(jump = `1992` - `1952`) %>% 
  select(country, `1952`, `1992`, jump) %>% 
  arrange(jump)

biggest_jump
## # A tibble: 96 x 4
##    country          `1952` `1992`  jump
##    <chr>             <int>  <int> <int>
##  1 Myanmar               8     -7   -15
##  2 Cuba                  0     -7    -7
##  3 Indonesia             0     -7    -7
##  4 Iran                 -1     -6    -5
##  5 Iraq                 -4     -9    -5
##  6 Oman                 -6     -9    -3
##  7 Haiti                -5     -7    -2
##  8 Korea, Dem. Rep.     -7     -9    -2
##  9 Lebanon               2      0    -2
## 10 Sri Lanka             7      5    -2
## # ... with 86 more rows

But if you want to see trends over time, the data needs to be in long format

dem_score_long <- dem_score %>% 
  gather(year, score, -country) %>% 
  mutate(year = parse_number(year))

dem_score_long
## # A tibble: 864 x 3
##    country     year score
##    <chr>      <dbl> <int>
##  1 Albania     1952    -9
##  2 Argentina   1952    -9
##  3 Armenia     1952    -9
##  4 Australia   1952    10
##  5 Austria     1952    10
##  6 Azerbaijan  1952    -9
##  7 Belarus     1952    -9
##  8 Belgium     1952    10
##  9 Bhutan      1952   -10
## 10 Bolivia     1952    -4
## # ... with 854 more rows

Answer some questions

What has Myanmar’s political history looked like?

dem_myanmar <- dem_score_long %>% 
  filter(country == "Myanmar")

ggplot(dem_myanmar, aes(x = year, y = score)) +
  geom_line() + 
  geom_hline(yintercept = -5) +
  geom_hline(yintercept = 5)

What about Lithuania?

dem_lithuania <- dem_score_long %>% 
  filter(country == "Lithuania")

ggplot(dem_lithuania, aes(x = year, y = score)) +
  geom_line() + 
  geom_hline(yintercept = -5) +
  geom_hline(yintercept = 5)

Looks like democratization and/or backsliding happens really suddenly (at least for these two cases). Is that normal?

ggplot(dem_score_long, aes(x = year, y = score, group = country)) +
  geom_line(alpha = 0.15) +
  geom_hline(yintercept = -5, color = "red") +
  geom_hline(yintercept = 5, color = "blue")