In yesterday's post, I was wondering if I could add a bit of juice by displaying the flags of the countries.

Here is a way of accomplishing that.

# get data from https://www.kaggle.com/unsdsn/world-happiness

sDir <- "~/Dropbox/pandora/My-Projects/repos/diary/writing/TRC"
setwd(sDir)

require("data.table")

dt_2015 <- fread("./data/world-happiness/2015.csv")
dt_2016 <- fread("./data/world-happiness/2016.csv")
dt_2017 <- fread("./data/world-happiness/2017.csv")
dt_2018 <- fread("./data/world-happiness/2018.csv")
dt_2019 <- fread("./data/world-happiness/2019.csv")

# data is unfortunately not consistent!
# cleanup for consistency

dt_2015[,country:=`Country`]
dt_2015[,rank:=`Happiness Rank`]
dt_2015 <- dt_2015[,list(country, rank)]
dt_2015[,year:=2015]

dt_2016[,country:=`Country`]
dt_2016[,rank:=`Happiness Rank`]
dt_2016 <- dt_2016[,list(country, rank)]
dt_2016[,year:=2016]

dt_2017[,country:=`Country`]
dt_2017[,rank:=`Happiness.Rank`]
dt_2017 <- dt_2017[,list(country, rank)]
dt_2017[,year:=2017]

dt_2018[,country:=`Country or region`]
dt_2018[,rank:=`Overall rank`]
dt_2018 <- dt_2018[,list(country, rank)]
dt_2018[,year:=2018]

dt_2019[,country:=`Country or region`]
dt_2019[,rank:=`Overall rank`]
dt_2019 <- dt_2019[,list(country, rank)]
dt_2019[,year:=2019]

dt_happiness <- rbind(dt_2019, dt_2018, dt_2017, dt_2016, dt_2015)

dt_happiness_original <- dt_happiness

dt_happiness <- dt_happiness[rank <= 10 ]

# to avoid dropouts .. lets reassign!
dt_happiness <- dt_happiness_original[country %in% dt_happiness$country]

# manual is boring!
# dt_countries <- data.table(country=countries, code=c("FI", "DE", "NO", "IC", "NE", "SW", "SI", "NZ", "CA", "AU", "AUS"))

# install.packages("countrycode")
require(countrycode)

countries <- unique(dt_happiness$country)
dt_countries[,code:=countrycode(country, 'country.name', 'iso2c')]
# works!


# inspection
dt_happiness[,code:=countrycode(country, 'country.name', 'iso2c')]

library(dplyr)
library(ggplot2)

dt_happiness <- group_by(dt_happiness, year) %>% mutate(rank = row_number(rank))

library(ggimage)

ggplot(dt_happiness, aes(x = year, y = rank)) +
  geom_flag(aes(image = code)) +
  theme_minimal() +
  scale_y_reverse(breaks = c(1:20)) +
  geom_text(data = filter(dt_happiness, year == 2019), aes(label = country), color = "black", hjust = 0, nudge_x = 0.25) +
  theme(panel.grid = element_blank(), legend.position = "none") +
  scale_x_continuous(breaks = seq(2015,2019,1), expand = expand_scale(add = c(0.25, 0.25)))