The World Happiness Report 2022

worldmap leaflet sf

Second series in exploratory data visualizations on World Happiness Report 2022 using the {leaflet} packages in R.

Jewel Johnson https://jeweljohnsonj.github.io/jeweljohnson.github.io/
2022-05-16

Getting the data

Similar to the previous post we will plot an interactive map using the {leaflet} package in R with the world happiness report data 2022.

With the effects of COVID-19 finally getting subdued via mass vaccination drives and other precautionary measures taken by the governments around the world, the happiness data of 2022 shows light on how different countries are doing in the recovery stage.

Like last time, Finland is again ranked first among 149 countries with an overall score of 7.82 and Afghanistan ranked the last with a score of 2.40.

Plotting an interactive world map

Lets plot an interactive world map with happiness score as a variable, where greater scores indicates happier countries and vice versa. We will be using the {leaflet} package in R for plotting the world map.

First lets download and import the data into R. You can download the .csv file using this link. Then we have to download the world map data which comes as a .shp file.

Run the codes given below to download the .shp file and load the .csv file required to plot the map. I will be reusing the codes from the previous post. For0 plotting the interactive map, we will be using the {sf} package for reading the .shp file and the {leaflet} package for plotting the map.

library(readr)
library(dplyr)

# loading the .csv file which was downloaded
# please change the location to where your .csv file is kept
hap_pre <- read.csv("/home/jeweljohnson/Work/datasets/2022.csv")

# renaming column names of ease of use
colnames(hap_pre)[1] <- "rank"
colnames(hap_pre)[2] <- "country"
colnames(hap_pre)[3] <- "score"
hap_pre <- hap_pre[-147,]

# the score values are separated by commas
# let us change that to dots
hap_pre$score <- scan(text=hap_pre$score, dec=",", sep=".")

# selecting country and score columns
hap <- hap_pre %>% select(rank,country,score)

# downloading and loading the .shp file
# please change the 'destfile' location to where your zip file is located
download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip" , destfile="shp/world_shape_file.zip")

# unzip the file into a directory. You can do it with R (as below).
# the directory of my choice was folder named 'shp'
unzip("shp/world_shape_file.zip", exdir = "shp/")

# Read the shape file with 'sf'
#install.packages("sf")
library(sf)
world_spdf <- st_read(paste0(getwd(),"/shp/TM_WORLD_BORDERS_SIMPL-0.3.shp"), stringsAsFactors = FALSE)
Reading layer `TM_WORLD_BORDERS_SIMPL-0.3' from data source 
  `/home/jeweljohnson/Work/R_distill_github/jeweljohnson.github.io/_posts/2022-05-16-the-world-happiness-report-2022/shp/TM_WORLD_BORDERS_SIMPL-0.3.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 246 features and 11 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -180 ymin: -90 xmax: 180 ymax: 83.57027
Geodetic CRS:  WGS 84
# checking which country names are a mismatch between map data and the downloaded dataset
# this is an important check as we have to join the happiness dataset and .shp file with country names
anti_join(hap, world_spdf,  by = c("country" = "NAME"))
   rank                   country score
1     6               Luxembourg* 7.404
2    18                   Czechia 6.920
3    26  Taiwan Province of China 6.512
4    32                    Kosovo 6.455
5    39                Guatemala* 6.262
6    50                   Kuwait* 6.106
7    59               South Korea 5.935
8    62                   Moldova 5.857
9    65                  Belarus* 5.821
10   77                   Vietnam 5.485
11   78             Turkmenistan* 5.474
12   79             North Cyprus* 5.467
13   81 Hong Kong S.A.R. of China 5.425
14   86                    Libya* 5.330
15   88               Ivory Coast 5.235
16   89           North Macedonia 5.199
17   92               Azerbaijan* 5.173
18   93                   Gambia* 5.164
19   95                      Laos 5.140
20   97                  Liberia* 5.122
21  104                    Niger* 5.003
22  110                      Iran 4.888
23  116                  Comoros* 4.609
24  122  Palestinian Territories* 4.483
25  125     Eswatini, Kingdom of* 4.396
26  126                   Myanmar 4.394
27  128               Madagascar* 4.339
28  130                     Chad* 4.251
29  132                    Yemen* 4.197
30  133               Mauritania* 4.153
31  139                  Tanzania 3.702
32  141                  Lesotho* 3.512
33  142                 Botswana* 3.471
34  143                   Rwanda* 3.268
# correcting country names, note that some countries are not available in the .shp file

correct_names <- c("Luxembourg*" = "Luxembourg",
                   "Czechia" = "Czech Republic",
                   "Taiwan Province of China" = "Taiwan",
                   "Guatemala*" = "Guatemala",
                   "Kuwait*" = "Kuwait",
                   "South Korea" = "Korea, Republic of",
                   "Moldova"  = "Republic of Moldova",
                   "Belarus*" = "Belarus",
                   "Vietnam" = "Viet Nam",
                   "Turkmenistan*" = "Turkmenistan",
                   "Hong Kong S.A.R. of China"= "Hong Kong",
                   "Libya*" = "Libyan Arab Jamahiriya",
                   "Ivory Coast" = "Cote d'Ivoire",
                   "North Macedonia" = "The former Yugoslav Republic of Macedonia",
                   "Azerbaijan*" = "Azerbaijan",
                   "Gambia*" = "Gambia",
                   "Liberia*" = "Liberia",
                   "Laos" = "Lao People's Democratic Republic",
                   "Niger*" = "Niger",
                   "Iran" = "Iran (Islamic Republic of)",
                   "Comoros*" = "Comoros",
                   "Palestinian Territories*" = "Palestine",
                   "Eswatini, Kingdom of*" = "Swaziland",
                   "Myanmar" = "Burma",
                   "Madagascar*" = "Madagascar",
                   "Chad*" = "Chad",
                   "Yemen*" = "Yemen",
                   "Mauritania*" = "Mauritania",
                   "Tanzania" = "United Republic of Tanzania",
                   "Lesotho*" = "Lesotho",
                   "Botswana*" = "Botswana",
                   "Rwanda*" = "Rwanda")

# recoding country names 
hap2 <- hap %>% mutate(country = recode(country, !!!correct_names))

# joining .shp file and the happiness data
world_hap <-  left_join(world_spdf, hap2, by = c("NAME" = "country"))

#install.packages("leaflet")
library(leaflet)
library(viridis)

# making colour palette for filling
fill_col <- colorNumeric(palette="magma", domain=world_hap$score, na.color="transparent")

# Prepare the text for tooltips:
text <- paste(
  "Country: ", world_hap$NAME,"<br/>", 
  "Score: ", world_hap$score, "<br/>", 
  "Rank: ", world_hap$rank, 
  sep="") %>%
  lapply(htmltools::HTML)

# plotting interactive map
leaflet(world_hap) %>% 
  addTiles()  %>% 
  setView( lat=10, lng=0 , zoom=2) %>%
  addPolygons( 
    fillColor = ~fill_col(score), 
    stroke=TRUE, 
    fillOpacity = 0.9, 
    color= "grey", 
    weight=0.3,
    label = text,
    labelOptions = labelOptions( 
      style = list("font-weight" = "normal", padding = "3px 8px"), 
      textsize = "13px", 
      direction = "auto"
    )
  ) %>%
  addLegend( pal=fill_col, values=~score, opacity=0.7, title = "Score", position = "bottomleft" )

Summary

In continuation with my previous post, this post showcases an interactive map for the latest happiness index data for 2022.

References

  1. H. Wickham. ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York, 2016.

  2. Joe Cheng, Bhaskar Karambelkar and Yihui Xie (2021). leaflet: Create Interactive Web Maps with the JavaScript ‘Leaflet’ Library. R package version 2.0.4.1. https://CRAN.R-project.org/package=leaflet

  3. Pebesma, E., 2018. Simple Features for R: Standardized Support for Spatial Vector Data. The R Journal 10 (1), 439-446, https://doi.org/10.32614/RJ-2018-009

  4. Tutorial on plotting interactive maps in R.

  5. The World Happiness Report 2022

  6. Source for .csv file of World Happiness Score of countries 2022. Compiled by Mathurin Aché in Kaggle.com

Last updated on

[1] "2022-05-17 18:14:32 IST"

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. Source code is available at https://github.com/jeweljohnsonj/jeweljohnson.github.io, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

Citation

For attribution, please cite this work as

Johnson (2022, May 16). One-carat Blog: The World Happiness Report 2022. Retrieved from https://jeweljohnsonj.github.io/jeweljohnson.github.io/posts/2022-05-16-the-world-happiness-report-2022/

BibTeX citation

@misc{johnson2022the,
  author = {Johnson, Jewel},
  title = {One-carat Blog: The World Happiness Report 2022},
  url = {https://jeweljohnsonj.github.io/jeweljohnson.github.io/posts/2022-05-16-the-world-happiness-report-2022/},
  year = {2022}
}