아시아 실업율

세계은행과 지도를 결합하여 아시아 국가 실업율을 도식화하자.

저자
소속
소스코드

1 패키지

코드
if (!require("tidyverse")) install.packages("tidyverse")
if (!require("rnaturalearth")) install.packages("rnaturalearth")
if (!require("sf")) install.packages("sf")
if (!require("wbstats")) install.packages("wbstats")

library(tidyverse)
library(rnaturalearth) # World Map Data from Natural Earth
library(sf) # Geographic Simple Features in R
library(wbstats) # access World Bank API

crs_LONGLAT <- "+proj=longlat +datum=WGS84 +no_defs"

2 데이터

2.1 지도

코드
world <- ne_countries(scale="medium", returnclass="sf") %>%
  filter(admin != "Antarctica")

## ----Change World map projection-----------------
# Mollweide proj
target_crs <- "+proj=longlat +datum=WGS84 +no_defs"

world_wsg <- world %>%
  st_transform(crs = target_crs)

2.2 실업율

코드
## ----Get data from the World Bank----------------
# Example: Unemployment (% of labor force)
# https://data.worldbank.org/indicator/SL.UEM.TOTL.ZS
ind <- "SL.UEM.TOTL.ZS"

indicator_info <- wb_cachelist$indicators %>%
  filter(indicator_id == ind)

indicator_info$indicator
#> [1] "Unemployment, total (% of total labor force) (modeled ILO estimate)"


## ----Download data from World Bank---------------
df <- wb_data(ind, start_date = 2020) %>%
  filter(date == 2020)

glimpse(df)
#> Rows: 217
#> Columns: 9
#> $ iso2c          <chr> "AW", "AF", "AO", "AL", "AD", "AE", "AR", "AM", "AS", "…
#> $ iso3c          <chr> "ABW", "AFG", "AGO", "ALB", "AND", "ARE", "ARG", "ARM",…
#> $ country        <chr> "Aruba", "Afghanistan", "Angola", "Albania", "Andorra",…
#> $ date           <dbl> 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2…
#> $ SL.UEM.TOTL.ZS <dbl> NA, 11.710, 10.350, 13.067, NA, 4.290, 11.460, 12.180, …
#> $ unit           <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
#> $ obs_status     <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
#> $ footnote       <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
#> $ last_updated   <date> 2023-05-10, 2023-05-10, 2023-05-10, 2023-05-10, 2023-0…

3 시각화

3.1 전세계

코드
## ----Plot world map------------------------------
world_wsg %>%
  left_join(df, by = c("iso_a3" = "iso3c")) %>%
  ggplot() +
  geom_sf(aes(fill = SL.UEM.TOTL.ZS)) +
  scale_fill_viridis_c(
    trans = "sqrt",
    labels = scales::percent_format(scale = 1),
    breaks = c(1:5)^2) +
  # fix labels if needed: https://stackoverflow.com/a/60733863
  scale_x_continuous(
    labels = function(x) paste0(x, '\u00B0', "W")
    ) +
  scale_y_continuous(
    labels = function(x) paste0(x, '\u00B0', "N")
    ) +
  theme_bw() +
  theme(panel.background = element_rect(fill = "aliceblue")) +
  labs(
    title = paste(unique(df$date), indicator_info$indicator),
    fill = NULL,
    caption = paste("자료출처:", indicator_info$source_org) 
  )

3.2 아시아

OpenStreetMap 내보내기(Export) 에서 테두리 상자(Bounding Box) 를 특정하여 아시아 지역으로 한정하여 시각화한다.

코드
## ----Create bounding box-------------------------
# Choose manually the region to plot with Open Street Map
# https://www.openstreetmap.org/export
window_coord <- st_sfc(
  st_point(c(60, -10)), #left, bottom
  st_point(c(150,50)), #right, top
  crs = 4326 #the EPSG identifier of WGS84 (used in GPS)
)

window_coord_sf <- window_coord %>%
  st_transform(crs = target_crs) %>%
  st_coordinates() # retrieve coordinates

OpenStreetMap 테두리 상자
코드
## ----Plot regional map---------------------------
world_wsg %>%
  left_join(df, by = c("iso_a3" = "iso3c")) %>% 
  ggplot() + 
  geom_sf(aes(fill = SL.UEM.TOTL.ZS)) +
  # window of the map
  coord_sf(
    xlim = window_coord_sf[, "X"],
    ylim = window_coord_sf[, "Y"],
    expand = FALSE
  ) +
  scale_fill_viridis_c(
    trans = "sqrt", 
    labels = scales::percent_format(scale = 1),
    breaks = c(1:5)^2
  ) +
  # fix labels if needed: https://stackoverflow.com/a/60733863
  scale_x_continuous(
    labels = function(x) paste0(x, '\u00B0', "W")
    ) +
  scale_y_continuous(
    labels = function(x) paste0(x, '\u00B0', "N")
    ) +
  theme_bw() +
  theme(panel.background = element_rect(fill = "aliceblue")) +
  labs(
    title = indicator_info$indicator,
    fill = NULL,
    caption = paste("Source:", indicator_info$source_org) 
  )