Load Data

df <- read_csv("WHR_15_23.csv")
## Rows: 1367 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): country, region
## dbl (7): happiness_score, gdp_per_capita, generosity, social_support, freedo...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
glimpse(df)
## Rows: 1,367
## Columns: 9
## $ country                      <chr> "Switzerland", "Iceland", "Denmark", "Nor…
## $ region                       <chr> "Western Europe", "Western Europe", "West…
## $ happiness_score              <dbl> 7.587, 7.561, 7.527, 7.522, 7.427, 7.406,…
## $ gdp_per_capita               <dbl> 1.39651, 1.30232, 1.32548, 1.45900, 1.326…
## $ generosity                   <dbl> 0.29678, 0.43630, 0.34139, 0.34699, 0.458…
## $ social_support               <dbl> 1.34951, 1.40223, 1.36058, 1.33095, 1.322…
## $ freedom_to_make_life_choices <dbl> 0.66557, 0.62877, 0.64938, 0.66973, 0.632…
## $ healthy_life_expectancy      <dbl> 0.94143, 0.94784, 0.87464, 0.88521, 0.905…
## $ year                         <dbl> 2015, 2015, 2015, 2015, 2015, 2015, 2015,…

Match Country Names with Geographic Data

df <- df %>%
  mutate(iso3c = countrycode(country, "country.name", "iso3c"))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `iso3c = countrycode(country, "country.name", "iso3c")`.
## Caused by warning:
## ! Some values were not matched unambiguously: Kosovo, Somaliland region, Somaliland Region
world <- ne_countries(scale = "medium", returnclass = "sf")

world <- world %>%
  mutate(iso3c = countrycode(name_en, "country.name", "iso3c"))
## Warning: There was 1 warning in `stopifnot()`.
## ℹ In argument: `iso3c = countrycode(name_en, "country.name", "iso3c")`.
## Caused by warning:
## ! Some values were not matched unambiguously: Ashmore and Cartier Islands, Kosovo, Saint Martin, Siachen Glacier, Somaliland
world_happy_2023 <- world %>%
  left_join(df %>% filter(year == 2023), by = "iso3c")

Color Palette

pal <- colorNumeric(
  palette = "YlGnBu",
  domain = world_happy_2023$freedom_to_make_life_choices
)

Map

leaflet(world_happy_2023) %>%
  addTiles() %>%
  addPolygons(
    fillColor = ~pal(freedom_to_make_life_choices),
    color = "white",
    weight = 1,
    fillOpacity = 0.8,
    label = ~paste(name, ": ", freedom_to_make_life_choices)
  ) %>%
  addLegend(
    pal = pal,
    values = ~freedom_to_make_life_choices,
    title = "Freedom to Make Life Choices (2023)"
  )