• このセクションで使う 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()