Research question

What I care about…

Load data

I want to analyze stuff. There’s data about democracy online. Let’s load it.

library(tidyverse)

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

Wrangle data

I want to see which country changed the most

change_in_score <- democracy %>% 
  mutate(change = `1992` - `1952`) %>% 
  select(country, change) %>% 
  mutate(change_absolute = abs(change)) %>% 
  arrange(desc(change_absolute))

change_in_score
## # A tibble: 96 x 3
##    country   change change_absolute
##    <chr>      <int>           <int>
##  1 Lithuania     19              19
##  2 Portugal      19              19
##  3 Hungary       17              17
##  4 Latvia        17              17
##  5 Slovenia      17              17
##  6 Spain         17              17
##  7 Argentina     16              16
##  8 Armenia       16              16
##  9 Belarus       16              16
## 10 Mongolia      16              16
## # ... with 86 more rows

I want it to be tidy

tidy_democracy <- democracy %>% 
  gather(year, score, -country) %>% 
  mutate(year = parse_number(year))
tidy_democracy
## # 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

Do stuff with data

Myanmar

I want to look at Myanmar

myanmar_only <- tidy_democracy %>% 
  filter(country == "Myanmar")

ggplot(data = myanmar_only, 
       mapping = aes(x = year, y = score)) +
  geom_line() +
  geom_hline(yintercept = -5, color = "red") + 
  geom_hline(yintercept = 5, color = "blue")

Lithuania

Lithuantiua’s turn

lith_only <- tidy_democracy %>% 
  filter(country == "Lithuania")

ggplot(data = lith_only, 
       mapping = aes(x = year, y = score)) +
  geom_line() +
  geom_hline(yintercept = -5, color = "red") + 
  geom_hline(yintercept = 5, color = "blue")

All coutnries

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