ggplot2を用いて、東京都都心3区(千代田区、中央区、港区)を、各地区の人口で色分けした地図を作成したい。
Rで地図を表示するためには、分析対象とする行政区のshapeファイルが必要となる。shapeファイルは、政府統計のポータルサイトe-Statからダウンロード可能。 たとえば2020年の国勢調査の結果を含むshapeファイルが集計されているURLはhttps://shorturl.at/cELQR。 なお、jpndistrictパッケージから取得することも可能である。
library(tidyverse)
library(sf) #空間データを扱うためのsfパッケージ
ダウンロードしたデータを読み込む。
注意:以下のスクリプトでは、サンプルデータを自分のプロジェクトフォルダーにおいてzipを解凍してあるものとする。
# 国勢調査小地域データの読みこみ
chome <- read_sf("tokyocity-2018/h27ka13.shp", #shapeファイルの名前
options="ENCODING=CP932")
# 確認
head(chome)
## Simple feature collection with 6 features and 35 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: 138.8144 ymin: 29.79356 xmax: 140.3427 ymax: 35.68572
## Geodetic CRS: JGD2000
## # A tibble: 6 × 36
## KEY_CODE PREF CITY S_AREA PREF_NAME CITY_NAME S_NAME KIGO_E HCODE AREA
## <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <int> <dbl>
## 1 13 13 402 000000 東京都 青ヶ島村 <NA> <NA> 8101 1.36e4
## 2 13 13 402 000000 東京都 青ヶ島村 <NA> <NA> 8101 1.79e5
## 3 13 13 402 000000 東京都 青ヶ島村 <NA> <NA> 8101 1.20e5
## 4 13 13 364 000000 東京都 神津島村 <NA> <NA> 8101 1.31e6
## 5 13101001001 13 101 001001 東京都 千代田区 丸の内… <NA> 8101 3.79e5
## 6 13101001002 13 101 001002 東京都 千代田区 丸の内… <NA> 8101 1.43e5
## # ℹ 26 more variables: PERIMETER <dbl>, H27KAxx_ <int>, H27KAxx_ID <int>,
## # KEN <chr>, KEN_NAME <chr>, SITYO_NAME <chr>, GST_NAME <chr>,
## # CSS_NAME <chr>, KIHON1 <chr>, DUMMY1 <chr>, KIHON2 <chr>, KEYCODE1 <chr>,
## # KEYCODE2 <chr>, AREA_MAX_F <chr>, KIGO_D <chr>, N_KEN <chr>, N_CITY <chr>,
## # KIGO_I <chr>, MOJI <chr>, KBSUM <int>, JINKO <dbl>, SETAI <dbl>,
## # X_CODE <dbl>, Y_CODE <dbl>, KCODE1 <chr>, geometry <POLYGON [°]>
# データの座標系の確認
crs <- st_crs(chome)
crs
## Coordinate Reference System:
## User input: JGD2000
## wkt:
## GEOGCRS["JGD2000",
## DATUM["Japanese Geodetic Datum 2000",
## ELLIPSOID["GRS 1980",6378137,298.257222101,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## CS[ellipsoidal,2],
## AXIS["geodetic latitude (Lat)",north,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433]],
## AXIS["geodetic longitude (Lon)",east,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433]],
## USAGE[
## SCOPE["Horizontal component of 3D system."],
## AREA["Japan - onshore and offshore."],
## BBOX[17.09,122.38,46.05,157.65]],
## ID["EPSG",4612]]
ここでは都心3区に範囲を限定する。
toshin <- chome[chome$CITY_NAME %in% c("千代田区", "中央区", "港区"), ]
参考:なお、もしも他のデータと統合したい場合には、上記のスクリプトに加えてEY_CODEをキーにしてデータ統合する。
# ライブラリを読み込む
library(ggplot2)
library(ggthemes)
# 人口規模のコロプレスマップを作成
g.toshin <- ggplot()+
geom_sf(data = toshin, aes(fill = as.numeric(JINKO)),color = "white")+
labs(fill = "単位:人",
title = "都心3区の人口規模(2018年)")
g.toshin
library(gridExtra)
chiyoda <- ggplot()+
geom_sf(data = filter(toshin,CITY_NAME=="千代田区"), aes(fill = as.numeric(JINKO)),color = "white")+
labs(fill = "単位:人",
title = "千代田区")
chuo <- ggplot()+
geom_sf(data = filter(toshin,CITY_NAME=="中央区"), aes(fill = as.numeric(JINKO)),color = "white")+
labs(fill = "単位:人",
title = "中央区")
minato <- ggplot()+
geom_sf(data = filter(toshin,CITY_NAME=="港区"), aes(fill = as.numeric(JINKO)),color = "white")+
labs(fill = "単位:人",
title = "港区")
# 3つ並べて描写
grid.arrange(chiyoda,chuo,minato,ncol = 3)