• このセクションで使う R パッケージ一覧
library(ggthemes)
library(ggrepel)
library(jpndistrict) 
library(magrittr)
library(remotes)
library(rnaturalearth)
library(pacman)
library(RColorBrewer)
library(sf)
library(stargazer)
library(tidyverse)

Choropleth map`

  • A type of thematic map in which a set of pre-defined areas is colored or patterned in proportion to a statistical variable that represents an aggregate summary of a geographic characteristic within each area, such as population density or per-capita income.

1. World map

1.1 rnaturalearthdatapackage

  • Using packman package enables us to install and load a package without typing install.packges() at Console on your RStudio
  • You need to type the following command at Console on your RStudio [only ONCE]{style=“color:red”.
install.packages("pacman")
  • We use free data on world map: Natural Earch

  • Using rnaturalearth package, we get the data necessary to draw a world map

  • Type the following command at a chunk on your RStudio and download the data

pacman::p_load(rnaturalearth, rnaturalearthdata, rgeos)
  • Name the data world_map
world_map <- ne_countries(scale = "small", returnclass = "sf")
  • Show the list of variables included world_map
names(world_map)
 [1] "scalerank"  "featurecla" "labelrank"  "sovereignt" "sov_a3"    
 [6] "adm0_dif"   "level"      "type"       "admin"      "adm0_a3"   
[11] "geou_dif"   "geounit"    "gu_a3"      "su_dif"     "subunit"   
[16] "su_a3"      "brk_diff"   "name"       "name_long"  "brk_a3"    
[21] "brk_name"   "brk_group"  "abbrev"     "postal"     "formal_en" 
[26] "formal_fr"  "note_adm0"  "note_brk"   "name_sort"  "name_alt"  
[31] "mapcolor7"  "mapcolor8"  "mapcolor9"  "mapcolor13" "pop_est"   
[36] "gdp_md_est" "pop_year"   "lastcensus" "gdp_year"   "economy"   
[41] "income_grp" "wikipedia"  "fips_10"    "iso_a2"     "iso_a3"    
[46] "iso_n3"     "un_a3"      "wb_a2"      "wb_a3"      "woe_id"    
[51] "adm0_a3_is" "adm0_a3_us" "adm0_a3_un" "adm0_a3_wb" "continent" 
[56] "region_un"  "subregion"  "region_wb"  "name_len"   "long_len"  
[61] "abbrev_len" "tiny"       "homepart"   "geometry"  
  • We see 64 variables
  • Check the class of world_map
class(world_map)
[1] "sf"         "data.frame"
  • world_map has two classes: sf & data.frame
  • Show descriptive statistics on world_map
library(stargazer)
  • Type {r, results = "asis"} at chunk option
stargazer(as.data.frame(world_map), 
          type ="html",
          digits = 2)
Statistic N Mean St. Dev. Min Pctl(25) Pctl(75) Max
scalerank 177 1.01 0.15 1 1 1 3
labelrank 177 3.71 1.35 2 3 5 7
adm0_dif 177 0.08 0.28 0 0 0 1
level 177 2.00 0.00 2 2 2 2
geou_dif 177 0.00 0.00 0 0 0 0
su_dif 177 0.01 0.11 0 0 0 1
brk_diff 177 0.03 0.18 0 0 0 1
mapcolor7 177 3.23 1.72 1 2 5 7
mapcolor8 177 3.49 1.89 1 2 5 8
mapcolor9 177 3.80 2.10 1 2 5 9
mapcolor13 176 6.33 3.56 1.00 3.00 9.00 13.00
pop_est 176 38,491,454.00 137,822,959.00 140.00 3,481,234.00 26,163,376.00 1,338,612,970.00
gdp_md_est 176 395,512.50 1,392,204.00 16.00 13,197.50 235,100.00 15,094,000.00
pop_year 4 0.00 0.00 0.00 0.00 0.00 0.00
lastcensus 167 2,005.13 7.38 1,970.00 2,002.00 2,010.00 2,012.00
gdp_year 5 401.80 898.45 0.00 0.00 0.00 2,009.00
wikipedia 1 0.00 0.00 0.00 0.00 0.00
name_len 177 8.06 3.15 4 6 10 22
long_len 177 8.79 4.53 4 6 10 35
abbrev_len 177 4.75 1.13 3 4 5 10
tiny 10 3.10 1.29 2.00 2.00 4.00 5.00
homepart 171 1.00 0.00 1.00 1.00 1.00 1.00

1.2 World Population Maps

  • pop_est: population of each country
  • Draw a world map in which a set of pre-defined areas is colored in proportion to population of each country (pop_est)
  • The more populous, the thicker blue
  • You could choose the color you like
  • You see the list of colors available by typing colors at Console of your RStudio
world_map %>% 
  ggplot() +
  geom_sf(aes(fill = pop_est)) +
  scale_fill_gradient(low = "white", high = "green") +
  labs(fill = "人口") +
  theme_minimal(base_family = "HiraKakuProN-W3") +
  theme(legend.position = "bottom")

1.3 Reageonal Population Maps

  • You could limit the area you like and draw a map on population
  • Let’s draw a population map of Europe
world_map %>% 
  ggplot() +
  geom_sf(aes(fill = pop_est)) +
  scale_fill_gradient(low = "white", high = "green") +
  labs(fill = "人口") +
  coord_sf(xlim = c(-10, 45), ylim = c(35, 60)) +
  theme_minimal(base_family = "HiraKakuProN-W3") +
  theme(legend.position = "bottom")

  • You could assign the level of population so that you can clearly see the difference
world_map2 <- ne_countries(scale = "medium", returnclass = "sf") %>% 
  mutate(Population = case_when(pop_est < 30000000  ~ "3千万未満",
                                pop_est < 40000000  ~ "4千万未満",
                                pop_est < 50000000  ~ "5千万未満",
                                pop_est < 60000000  ~ "6千万未満",
                                pop_est < 70000000  ~ "7千万未満",
                                pop_est < 80000000  ~ "8千万未満",
                                TRUE                ~ "8千万以上"),
         Population = factor(Population, 
                             levels = c("3千万未満", "4千万未満", "5千万未満",
                                        "6千万未満", "7千万未満", "8千万未満",
                                        "8千万以上")))
  • You need to type the following at the chunk option: \(```{r, fig.width=27, fig.height=27}\)
world_map2 %>% 
  ggplot() +
  geom_sf(aes(fill = Population)) +
  scale_fill_brewer(palette = "green", drop = FALSE) +
  labs(fill = "人口") +
  coord_sf(xlim = c(-10, 45), ylim = c(35, 60)) +
  geom_label_repel(
    data = st_crop(world_map2,
                   c(xmin = -10, xmax = 45, ymin = 35, ymax = 60)),
    aes(label = name, geometry = geometry),
    size = 11,
    stat = "sf_coordinates",
    min.segment.length = 0
    ) +
  theme_minimal(base_family = "HiraKakuProN-W3") +
  theme(legend.position = "bottom",
        axis.text  = element_text(size = 30),
        legend.title = element_text(size = 32), 
        legend.text = element_text(size = 32),
        legend.key.size = unit(1, "cm"),
        legend.key.width = unit(3,"cm"))

  • If you want to know the population of each country, type the following command
world_map %>% 
  select(sovereignt, pop_est) %>% 
  DT::datatable()

2. Population Map of Japan

2.1 jpndistrict package

  • jpndistrict package includes the mapping data needed to draw a map of Japan
  • First, using remotes() package and download the data from Git
remotes::install_github("uribo/jpndistrict")
  • Load the jpndistrict package
library(jpndistrict)
  • Select the data we need from jpndistrict package and name them df_jpn_map
df_jpn_map <- 1:47 %>%
  map(~ jpn_pref(pref_code = ., district = FALSE)) %>% 
  reduce(rbind) %>% 
  st_simplify(dTolerance = 0.01)
  • Show the list of variables df_jpn_map includes:
names(df_jpn_map)
[1] "pref_code"  "prefecture" "geometry"  
  • Draw a map of Japan
df_jpn_map %>%
  ggplot() +
  geom_sf() +
  theme_minimal()

  • Now we get the mapping data (df_jpn_map) necessary to draw a map of Japan

2.2 Population data of Japan (2015)

  • We use the Japanese population data: (jpn_pop.csv)
  • This data was based on the following data reported by Asahi shinbun (Feb.27, 2016.

  • Load tidyverse
library(tidyverse)
  • Download the Japanese population data (jpn_pop.csv) and put it into data folder made in your R Project folder.
  • Read jpn_pop.csv and name it df_jpn_pop
df_jpn_pop <- read_csv("data/jpn_pop.csv", 
                       locale = locale(encoding = "cp932"))
  • Show the first 6 row of the data frame, df_jpn_pop
head(df_jpn_pop)
# A tibble: 6 x 4
  pref     prefecture    total  diff
  <chr>    <chr>         <dbl> <dbl>
1 okinawa  沖縄県      1430000   3  
2 tokyo    東京都     13510000   2.7
3 aichi    愛知県      7480000   1  
4 kanagawa 神奈川県    9130000   0.9
5 saitama  埼玉県      7260000   0.9
6 fukuoka  福岡県      5100000   0.6

2.3 Merge the Mapping and the Population data

  • Merge df_jpn_map and df_jpn_pop by the shared variable prefecture and name it df_jap
df_jap <- df_jpn_pop %>% 
  left_join(df_jpn_map, by = "prefecture") %>%   
  st_as_sf()
  • Check the merged data frame, df_jap
DT::datatable(df_jap)
row variables details
1 Pref Prefecture (English)
2 Prefecture Prefecture (Japanese)
3 total Population
4 diff Rate of increase/decreate between 2010 and 2015(%)
5 geometry Mapping data

2.4 Japanese Map by Prefectural Population (1)

map_pop_jpn1 <- df_jap %>%
  mutate(geometry = if_else(pref == "okinawa", geometry + c(5.5, 17.5), geometry)) %>%
  ggplot() +
  geom_sf(aes(fill = total), size = .pt / 20) + 
  annotate("segment",
           x = c(128, 132.5, 138),
           xend = c(132.5, 138, 138),
           y = c(40, 40, 42),
           yend = c(40, 42, 46),
           size = .pt / 15
  ) +
  scale_fill_distiller(name = "Population",
                       palette = "Greens", direction = 1) + # 任意の色を指定
  theme_map(base_family = "HiraginoSans-W3", base_size = 8) +
  theme(legend.position = "right") +
  coord_sf(datum = NA)

map_pop_jpn1
Japanese Map by Prefectural Population (2015)
  • Make a new folder in your RProject folder and name it fig
  • You could save the map in fig with the name you like and the size you like
ggsave("fig/map_pop_jpn1.png", map_pop_jpn1, width = 45, height = 45)

2.5 Japanese Map by Prefectural Population (2)

  • You can draw a Japan map with the information of population increase / decrease
  • The thicker blue means the population of a prefecture is increasing
  • The thicker red means the population of a prefecture is decreasing

・Type the following command at the chunk option: {r, fig.align = 'center', fig.height = 45, fig.width = 45}

map_pop_jpn2 <- df_jap %>%
  mutate(geometry = if_else(pref == "okinawa", geometry + c(5.5, 17.5), geometry)) %>%
  ggplot() +
  geom_sf(aes(fill = diff), size = .pt / 20) + 
  annotate("segment",
           x = c(128, 132.5, 138),
           xend = c(132.5, 138, 138),
           y = c(40, 40, 42),
           yend = c(40, 42, 46),
           size = .pt / 15
  ) +
  scale_fill_distiller(name = "Population Increase/Decrease Rates(%)",
                       palette = "RdBu", direction = 1) + 
  theme_map(base_family = "HiraginoSans-W3", base_size = 8) +
  theme(legend.position = "right") +
  coord_sf(datum = NA)

map_pop_jpn2

Population Map of Japan by Prefecture (2015)
  • Make a new folder in your RProject folder and name it fig
  • You could save the map in fig with the name you like and the size you like
ggsave("fig/map_pop_jpn2.png", map_pop_jpn2, width = 45, height = 45)

Tip
The Map of Japan with Japan’s Lower House election data is useful. This map was made by Mr.Ikuya Nakayama: 「2014年衆議院総選挙選挙区比較マップ」

2.6 Exercise

Q1: Draw a map of Japan with the data you like

参考文献