• ここで使っている R パッケージ一覧
library(ggthemes)
library(ggrepel)
library(httr)
library(jpndistrict)
library(sf)
library(tidyverse)

1. Drawing a Population Map of the 23 Wards of Tokyo

  • Create a choropleth map of the population of Tokyo’s 23 wards for the fiscal year 2015. - We need to take the following three steps:
  1. Obtain “map data” and “population data” for Tokyo.
  2. Merge these two datasets.
  3. Draw the map.

The following three variables are necessary to draw the map:

  1. Map Data Variable
  2. Population Data Variable
  3. Names of Wards, Cities, and Towns
  • This section is divided into two parts: map data and population data, explaining how to obtain and process the data, and how to draw the map.

1.1 Obtaining Map Data for the 23 Wards of Tokyo

  • The ne_states() function cannot map down to municipalities.
  • Use the {jpndistrict} package, published by Professor Shinya Uryu of Tokushima University.
  • The {jpndistrict} package is necessary to draw the map.
  • However, the {jpndistrict} package is not available for download from CRAN.

Solution:

  • Install the {jpndistrict} package via GitHub.
    → Install the {remotes} package.
    → Enter the following code in the Console (this needs to be done only once).
install.packages("remotes")
remotes::install_github("uribo/jpndistrict")
  • Next, load the installed {jpndistrict} package.
library(jpndistrict)

→ Extract the map data for Tokyo. → Name it df_tokyo_map.

df_tokyo_map <- jpn_pref(admin_name = "東京都")
  • Let’s check the type of df_tokyo_map.
class(df_tokyo_map)
[1] "sf"         "tbl_df"     "tbl"        "data.frame"
  • Try plotting the map of Tokyo.
df_tokyo_map |> 
  ggplot() +
  geom_sf() +
  theme_minimal()

  • The Tokyo area consists of the “Tokyo region” (the bigger chunk on the upper part of the map above) on Honshu and the “island areas.”
  • Display only the Tokyo Metropolis (23 wards), which consists of the 23 special wards in the “Tokyo region.”
df_tokyo_map <- jpn_pref(admin_name = "東京都") %>% 
  dplyr::filter(str_detect(city, "区")) # Settings to extract only the 23 wards
  • Try plotting the map of Tokyo.
df_tokyo_map |> 
  ggplot() +
  geom_sf() +
  theme_minimal()

  • Check the contents of df_tokyo_map and confirm the pref_code for Tokyo.
DT::datatable(df_tokyo_map)
  • The pref_code for Tokyo is 13.
    → Extract the map data for Tokyo, including only the 23 wards.
df_tokyo_map <- jpn_pref(13, district = TRUE) %>% 
  dplyr::filter(str_detect(city, "区")) # Settings to extract only the 23 wards
  • Display the beginning part of the data.
head(df_tokyo_map, 23)
  • Narrow down to only the necessary variables (city, geometry) for drawing the map.
df_tokyo_map <- df_tokyo_map %>% 
  select(city, geometry)
  • Display the beginning part of the narrowed-down data.
head(df_tokyo_map, 23)
  • With this, the necessary map data has been obtained.
  • Check the contents of df_tokyo_map and confirm the pref_code for Tokyo.
DT::datatable(df_tokyo_map)
  • The pref_code for Tokyo is 13.
    → Extract the map data for Tokyo, including only the 23 wards.
df_tokyo_sf <- jpn_pref(13, district = TRUE) %>% 
  dplyr::filter(str_detect(city, "区")) # Settings to extract only the 23 wards
  • Display the map of the extracted 23 wards of Tokyo.  
df_tokyo_sf %>%
  ggplot() +
  geom_sf() +
  theme_minimal()

  • Display the beginning part of the data.
head(df_tokyo_sf, 23)
  • Narrow down to only the necessary variables (city, geometry) for drawing the map.
df_tokyo_sf <- df_tokyo_sf %>% 
  select(city, geometry)
  • Display the beginning part of the narrowed-down data.
head(df_tokyo_sf, 23)
  • With this, the necessary map data has been obtained.  

1.2 Obtaining Population Data for the 23 Wards of Tokyo

  • There are two methods to obtain population data for prefectures:
  1. Access the e-Stat (Government Statistics Comprehensive Portal) and manually download the data.
  2. Access e-Stat and obtain an “Application ID” to automatically download the data.
  • Choose the method of data acquisition according to your preference.
  • Here, both methods are explained.

Downloading from e-Stat (Manually)

  • Access the e-Stat (Government Statistics Comprehensive Portal).
  • Click on “(地域)Region”.

  • Click「市町村データ」(City and Town Data) → 「データ表示」(Display Data)

  • Choose「現在の市区町村 (Current Cities, Wards, and Towns)」
  • Choose「東京都 (Tokyo)」
  • Choose「特別区 (Special Ward)」
    → Click「実行 (Execute)」

  • Choose「全て選択 (Select all)」→ 「確定 Confirm)」

  • 「A1101 総人口(人)(Total Population)」 → 「項目を選択 (Select Item)」

  • Click「確定」(Confirm)

  • Click 「ダウンロード (Download)」 at the top right of the screen.  

  • Select 「ページ上部の選択項目(調査年):Selected items at the top of the page」
  • Select「csv形式」
  • Select 【ヘッダの出力】から「出力しない」
    【コードの出力】から「出力しない (Do not output)」
  • Check ✅️データがない行を表示しない (Do not display rows with no data)
  • Check ✅️データがない列を表示しない (Do not display columns with no data)
    → Click 「ダウンロード (Download)」

→ Click「ダウンロード (Download)」
→ Open the downloaded CSV file.

  • Renaming Variables::
「調査年」 → year
「地域」 → city
「A1101_総人口【人】」 → population

  • Delete the column named 「/項目」

  • Remove 「東京都」 (Tokyo and a half-width space) from within city.
  • Place the cursor on column B, select the city variable column, then choose “Edit”
    → “Find” → “Replace”. → In「通知 (Find what)」 enter the characters 「東京都」 and a half-width space.
    → Click on Replace All.

  • Confirm「通知 23件を置換しました (The notification 23 items replaced] → Click OK

  • Select ‘Close’ in the next screen
    → If a screen like the one below appears, it is OK.

  • Save the file as tokyo_pop.csv (for example, on the desktop).
  • Note: Specify the file format asCSV UTF-8(コンマ区切り)(.csv)

  • With this, the population data necessary for drawing a map of the 23 wards of Tokyo by population has been obtained.
  • Next, merge the population data of the 23 wards of Tokyo obtained from e-Stat with the map data obtained in ‘1.1 Obtaining Map Data for the 23 Wards of Tokyo’.

1.3 Merging Data (23 Wards of Tokyo)

  • Here, we merge the Tokyo population data (tokyo_pop.csv) with the “map data” (df_tokyo_sf).
  • Create an R project folder and within it, create a folder named data.
    Place the tokyo_pop.csv file in the data folder, read the data, and name it df_tokyo_pop.
df_tokyo_pop <- read_csv("data/tokyo_pop.csv")
  • Display the read Tokyo 23 wards population data (df_tokyo_pop).
head(df_tokyo_pop)
  • Display the Tokyo map data (df_tokyo_sf) downloaded in 1.1 Obtaining Map Data for the 23 Wards of Tokyo.
head(df_tokyo_sf)
  • Merge Tokyo’s map data (df_tokyo_sf) and Tokyo’s population data (df_tokyo_pop) using the common variable (city), and name the resulting dataframe merge_tokyo.
merge_tokyo <- df_tokyo_sf %>% 
  left_join(df_tokyo_pop, by = "city") %>% 
  st_as_sf()
  • Check the merged data.
merge_tokyo %>% 
  head() %>% 
  rmarkdown::paged_table()
  • Select only the necessary variables for drawing the population map of the 23 wards of Tokyo.
merge_tokyo <- merge_tokyo %>% 
  select(city, geometry, population)
  • Display the merged data.
merge_tokyo %>% 
  head() %>% 
  rmarkdown::paged_table()
  • With this, the three necessary variables for drawing a population map of the 23 wards of Tokyo have been assembled:
  1. Map Data Variable: geometry
  2. Population Data Variable: population
  3. Names of Wards, Cities, and Towns: city

1.4 Population Map of the 23 Wards of Tokyo

  • Using the data created above (merge_tokyo), display a map highlighting the wards with higher populations in darker green.
map_pop_tokyo <- merge_tokyo %>% 
  ggplot() +
  geom_sf(aes(fill = population)) +
  scale_fill_distiller(name = "人口", # 人口 means population
                       palette = "Greens", direction = 1) +
  theme_map(base_family = "HiraginoSans-W3") +
  theme(legend.position = c(.1, -.1),
        legend.direction = "horizontal",
        legend.title = element_text(size = 15), 
        legend.text = element_text(size = 15),
        legend.key.size = unit(1, "cm"),
        legend.key.width = unit(3,"cm")) +
  coord_sf(datum = NA) 

map_pop_tokyo

・Furthermore, display the names of the 23 wards on the map.

map_pop_tokyo_text <- merge_tokyo %>% 
  mutate(
    text_x = map_dbl(geometry, ~st_centroid(.x)[[1]]),
    text_y = map_dbl(geometry, ~st_centroid(.x)[[2]])
  ) %>% 
  ggplot() +
  geom_sf(aes(fill = population)) +
  geom_label(aes(x = text_x, y = text_y, label = city), 
             size = 1.7, family = "HiraginoSans-W3") +
  scale_fill_distiller(name = "人口",
                       palette = "Greens", direction = 1) +
  theme_map(base_family = "HiraginoSans-W3") +
  theme(legend.position = c(.8, .05),
        legend.title = element_text(size = 10), 
        legend.text = element_text(size = 5),
        legend.key.size = unit(0.5, "cm"),
        legend.key.width = unit(1,"cm")) +
  coord_sf(datum = NA) 

map_pop_tokyo_text
Population Map of the 23 Wards of Tokyo (2015)

  • If you want to save the created map as figure, before saving, create a folder named, say fig in the R project folder, and then enter the following command.
ggsave("fig/map_pop_tokyo_text.png", map_pop_tokyo_text, width = 13, height = 13)
Reference

参考:
拓殖大学政経学部浅野ゼミ生の中山郁弥さんが作成したサイト「2014年衆議院総選挙選挙区比較マップ」が役に立ちます