R パッケージ
一覧library(geofacet)
library(ggrepel)
library(rmapshaper)
library(rnaturalearth)
library(sf)
library(tidyverse)
library(zipangu)
ggplot2
} で地図をプロットする方法は様々パッケージ名 | 詳細 |
---|---|
{rnaturalearth } |
:Natural Earth のデータ(国や海域の地理空間データを提供) |
{rnaturalearthdata } |
:Natural Earth のデータ(高解像度) |
・パッケージ管理ツール (packman
パッケージ)を使うと
install.packges()
コマンドを使わなくてもパッケージをインストールしロードできる
{pacman}
パッケージの p_load()
関数を使って
3 つのパッケージを同時にダウンロードする{naturalearth}
が無償で提供する ne_countries()
関数scale
と retunrclass
scale
の設定:scale = "small"
と指定scale = "medium"
と指定scale = "large"
と指定retunrclass
の設定:reutrnclass = "sf"
と指定sf
クラスで読み込んでみるreturnclass = "sf"
では、地理座標系の
sf オブジェクト
を指定sf オブジェクト
をマッピングすることができるworld_map
)
の中の変数と型をチェック[1] "sf" "data.frame"
data.frame
と
sf
の 2 つworld_map
のクラスがdata.frame
あることworld_map
が含む変数名とその数をチェック [1] "featurecla" "scalerank" "labelrank" "sovereignt" "sov_a3"
[6] "adm0_dif" "level" "type" "tlc" "admin"
[11] "adm0_a3" "geou_dif" "geounit" "gu_a3" "su_dif"
[16] "subunit" "su_a3" "brk_diff" "name" "name_long"
[21] "brk_a3" "brk_name" "brk_group" "abbrev" "postal"
[26] "formal_en" "formal_fr" "name_ciawf" "note_adm0" "note_brk"
[31] "name_sort" "name_alt" "mapcolor7" "mapcolor8" "mapcolor9"
[36] "mapcolor13" "pop_est" "pop_rank" "pop_year" "gdp_md"
[41] "gdp_year" "economy" "income_grp" "fips_10" "iso_a2"
[46] "iso_a2_eh" "iso_a3" "iso_a3_eh" "iso_n3" "iso_n3_eh"
[51] "un_a3" "wb_a2" "wb_a3" "woe_id" "woe_id_eh"
[56] "woe_note" "adm0_iso" "adm0_diff" "adm0_tlc" "adm0_a3_us"
[61] "adm0_a3_fr" "adm0_a3_ru" "adm0_a3_es" "adm0_a3_cn" "adm0_a3_tw"
[66] "adm0_a3_in" "adm0_a3_np" "adm0_a3_pk" "adm0_a3_de" "adm0_a3_gb"
[71] "adm0_a3_br" "adm0_a3_il" "adm0_a3_ps" "adm0_a3_sa" "adm0_a3_eg"
[76] "adm0_a3_ma" "adm0_a3_pt" "adm0_a3_ar" "adm0_a3_jp" "adm0_a3_ko"
[81] "adm0_a3_vn" "adm0_a3_tr" "adm0_a3_id" "adm0_a3_pl" "adm0_a3_gr"
[86] "adm0_a3_it" "adm0_a3_nl" "adm0_a3_se" "adm0_a3_bd" "adm0_a3_ua"
[91] "adm0_a3_un" "adm0_a3_wb" "continent" "region_un" "subregion"
[96] "region_wb" "name_len" "long_len" "abbrev_len" "tiny"
[101] "homepart" "min_zoom" "min_label" "max_label" "label_x"
[106] "label_y" "ne_id" "wikidataid" "name_ar" "name_bn"
[111] "name_de" "name_en" "name_es" "name_fa" "name_fr"
[116] "name_el" "name_he" "name_hi" "name_hu" "name_id"
[121] "name_it" "name_ja" "name_ko" "name_nl" "name_pl"
[126] "name_pt" "name_ru" "name_sv" "name_tr" "name_uk"
[131] "name_ur" "name_vi" "name_zh" "name_zht" "fclass_iso"
[136] "tlc_diff" "fclass_tlc" "fclass_us" "fclass_fr" "fclass_ru"
[141] "fclass_es" "fclass_cn" "fclass_tw" "fclass_in" "fclass_np"
[146] "fclass_pk" "fclass_de" "fclass_gb" "fclass_br" "fclass_il"
[151] "fclass_ps" "fclass_sa" "fclass_eg" "fclass_ma" "fclass_pt"
[156] "fclass_ar" "fclass_jp" "fclass_ko" "fclass_vn" "fclass_tr"
[161] "fclass_id" "fclass_pl" "fclass_gr" "fclass_it" "fclass_nl"
[166] "fclass_se" "fclass_bd" "fclass_ua" "geometry"
pop_est
」があるのでこれを使う geom_sf()
幾何オブジェクトを使って世界地図を出力してみるworld_map
のデータ型式が「データフレーム形式」pop_est
(各国の人口データ)に応じて
geom_sf()
内に fill = pop_est
でマッピングできるworld_map %>%
ggplot() +
geom_sf(aes(fill = pop_est)) +
# 人口が少ない国は yellow に、多い国は brown3 色とする
scale_fill_gradient(low = "yellow", high = "green") +
labs(fill = "人口") +
theme_void(base_family = "HiraKakuProN-W3")
ne_countries()
内に continent
引数を指定asia_map
という名のオブジェクトとして格納してみるasia_map <- ne_countries(scale = "medium",
continent = "Asia",
returnclass = "sf")
asia_map |>
ggplot() +
geom_sf() +
theme_void()
asia_map |>
ggplot() +
geom_sf(aes(fill = pop_est)) +
scale_fill_gradient(low = "yellow", high = "green") +
labs(fill = "人口") +
theme_minimal() +
theme_void(base_family = "HiraKakuProN-W3")
ここでは「アジア大陸」から表示規模を「東アジア」に絞って所得規模別地図を描いてみる
まず、東アジアにおける各国の所得規模別地図を描いてみる
asia_map <- ne_countries(scale = "medium",
continent = "Asia",
returnclass = "sf")
asia_map %>%
ggplot() + # 所得グループで色塗り
geom_sf(aes(fill = income_grp)) +
theme_void() +
labs(fill = "Income Group")
filter()
関数を使って
subregion
列を基準に抽出するasia_map %>%
filter(subregion == "Eastern Asia") %>% # 「東アジア」に絞る
ggplot() +
geom_sf(aes(fill = income_grp)) +
theme_void() +
labs(fill = "Income Group")
europe_map <- ne_countries(scale = "medium",
continent = "Europe",
returnclass = "sf")
europe_map |>
ggplot() +
geom_sf() +
theme_void()
coord_sf()
関数で座標系を調整し、ヨーロッパの表示範囲を絞ってみるxlim
で、緯度は ylim
で指定world_map %>%
ggplot() +
geom_sf(aes(fill = pop_est)) +
scale_fill_gradient(low = "yellow", high = "green") +
labs(fill = "人口") +
coord_sf(xlim = c(-10, 45), ylim = c(35, 60)) +
theme_minimal(base_family = "HiraKakuProN-W3") +
theme(legend.position = "bottom")
ロシアとドイツの人口が多いことは分かるが、このマップからはあまり顕著な違いが分からない
人口を分割している単位をもう少し細分化してみる
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千万以上")))
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"))