Dotplot showing the changes of the top countries in the World Happiness Ranking

In this daily coding exercise, we will look at a neat way of displaying the changes in the Happiness score of the top countries in the world happiness ranking.
We will continue to use the dataset shown in a previous blog post available at Bump chart of the world happiness index using ggplot2 and data.table
The below chart shows the transition in happiness ranking between 2015 and 2019. Finland notably improved from rank 6 to rank 1!


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]
require(countrycode)
countries <- unique(dt_happiness$country)
# inspection
dt_happiness[,code:=countrycode(country, 'country.name', 'iso2c')]
library(dplyr)
library(ggplot2)
dt_happiness_2015_2019 <- dt_happiness[year %in% c(2015, 2019)]
# devtools::install_github(c("jrnold/ggthemes"))
library('ggthemes')
library("forcats")
# Reorder country factor levels
dt_happiness_2015_2019 <- dt_happiness_2015_2019 %>%
arrange(year) %>%
# Reorder countries by rank in 2015
mutate(country = fct_reorder(country,
rank,
first))
ggplot(dt_happiness_2015_2019) + coord_cartesian(xlim=c(0, 15)) +
geom_path(aes(x = rank, y = country),
arrow = arrow(length = unit(1.5, "mm"), type = "closed")) +
# Specify the hjust aesthetic with a conditional value
geom_text(
aes(x = rank,
y = country,
label = round(rank, 1),
vjust = -2.0
),
# Change the appearance of the text
family = "HersheySans",
color = "red",
) + theme_wsj() +
# Add labels to the plot
labs(
x = "Happiness Rank (#)",
y = "Country",
title = "Ranking of World Happiness",
subtitle = "Period between 2015 to 2019",
caption = "Data source: World Happiness Report"
)
dcast.data.table(as.data.table(dt_happiness_2015_2019), country~year, value.var = 'rank')