You've successfully subscribed to MyPad Blog
Great! Next, complete checkout for full access to MyPad Blog
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

Bump chart of the world happiness index using ggplot2 and data.table

Bump chart of the world happiness index using ggplot2 and data.table

Trying to maintain the #dailycoding and #xdaysofcode exercise. Here is an example of creating a "bumpchart" in R for the World Happiness Index.

Finland rocks!

# 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]

library(dplyr)
library(ggplot2)

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

# Make the bump chart.
bumpchart <- 
  ggplot(dt_happiness, aes(x = year, y = rank, color = country)) +
  geom_line() + 
  geom_point(shape = 21, fill = "white") +
  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)))


ggsave(bumpchart, filename = "world_happiness_bumpchart.png", dpi = 300, units = "cm", width = 36, height = 16)
# TO DO - add flags to the chart