空間データ

ロンドンにおける1854年のコレラ発生

ジョン・スノーによるロンドンにおけるコレラ感染による死亡事例の地図を再現したもの

ジョン・スノーによる自然実験の地図を再現したもの


Rにおける空間データ

2008年アメリカ大統領選挙での選挙人団の投票先マップ

library(maps)
data(us.cities)
head(us.cities)
##         name country.etc    pop   lat    long capital
## 1 Abilene TX          TX 113888 32.45  -99.74       0
## 2   Akron OH          OH 206634 41.08  -81.52       0
## 3 Alameda CA          CA  70069 37.77 -122.26       0
## 4  Albany GA          GA  75510 31.58  -84.18       0
## 5  Albany NY          NY  93576 42.67  -73.80       2
## 6  Albany OR          OR  45535 44.62 -123.09       0
map(database = "usa") 
capitals <- subset(us.cities, capital == 2) # 州都を部分集合化

## 経度と緯度を用いて人口に比例した点を追加する
points(x = capitals$long, y = capitals$lat, col = "blue", cex = capitals$pop / 500000, pch = 19)
par(family = "HiraginoSans-W3") #日本語が文字化けしないようにおまじない
title("アメリカ合衆国の州都") # タイトルを追加する

map(database = "state", regions = "California")

cal.cities <- subset(us.cities, subset = (country.etc == "CA"))
head(cal.cities)
##              name country.etc    pop   lat    long capital
## 3      Alameda CA          CA  70069 37.77 -122.26       0
## 10    Alhambra CA          CA  88857 34.08 -118.13       0
## 11 Aliso Viejo CA          CA  41975 33.57 -117.73       0
## 15    Altadena CA          CA  43280 34.19 -118.13       0
## 20     Anaheim CA          CA 334909 33.84 -117.87       0
## 27     Antioch CA          CA 109485 37.99 -121.80       0
sind <- order(cal.cities$pop, decreasing = TRUE) # 人口を降順に並べる
top7 <- sind[1:7] # 人口の多い上位7都市
cal.cities[top7, ] # 人口の多い上位7都市のデータを表示
##                 name country.etc     pop   lat    long capital
## 521   Los Angeles CA          CA 3911500 34.11 -118.41       0
## 801     San Diego CA          CA 1299352 32.81 -117.14       0
## 804      San Jose CA          CA  897883 37.30 -121.85       0
## 802 San Francisco CA          CA  723724 37.77 -122.45       0
## 517    Long Beach CA          CA  486571 33.79 -118.16       0
## 778    Sacramento CA          CA  480392 38.57 -121.47       2
## 337        Fresno CA          CA  472517 36.78 -119.79       0
sort(cal.cities$pop, decreasing = TRUE)[1:7] # sort関数は順序づけられた人口を返す
## [1] 3911500 1299352  897883  723724  486571  480392  472517
map(database = "state", regions = "California")
points(x = cal.cities$long[top7], y = cal.cities$lat[top7], pch = 19, col = "blue")

## 円と重なることを避けるため、経度に定数を加える
text(x = cal.cities$long[top7] + 2, y = cal.cities$lat[top7], label = cal.cities$name[top7],
     cex = 0.6, col = "blue")
par(family = "HiraginoSans-W3") #日本語が文字化けしないようにおまじない
title("カリフォルニア州の大都市")

usa <- map(database = "usa", plot = FALSE) # 地図を保存
names(usa)  # 要素の一覧
## [1] "x"     "y"     "range" "names"
length(usa$x) 
## [1] 7252
head(cbind(usa$x, usa$y)) # ポリゴンの最初の6つの座標
##           [,1]     [,2]
## [1,] -101.4078 29.74224
## [2,] -101.3906 29.74224
## [3,] -101.3620 29.65056
## [4,] -101.3505 29.63911
## [5,] -101.3219 29.63338
## [6,] -101.3047 29.64484

日本の空間データ

  • 日本の情報を確認したい場合は mapdata パッケージを用いる。
library(mapdata)
map('japan')
map.axes() # 緯度と経度の枠を表示

  • 緯度と経度の範囲を xlimylim で指定して拡大することもできる。

  • オプション interior = FALSE で都道府県の境界を非表示にして、boundary = 以降で境界を波線で表示するように指定する。

map("japan", interior = FALSE, xlim = c(138,141), ylim = c(34,36))
map("japan", boundary = FALSE, lty = 2, add = TRUE)
map.axes()

法政大学(BT)の緯度と経度を調べて表示する。

  • 街区レベル(xx町x丁目x番)の緯度と経度は国土交通省からダウンロードできる。

  • より細かい緯度と経度を調べるには、例えばココを使う。

map("japan", interior = FALSE, xlim = c(138.5,140.5), ylim = c(35,36))
map("japan", boundary = FALSE, lty = 2, add = TRUE)
points(139.740631, 35.695374, col = "blue", pch=20)
par(family = "HiraginoSans-W3") #日本語が文字化けしないようにおまじない
text(139.740631, 35.695374 + .05, "法政大学", col = "blue")
map.axes()

参考文献

  1. 社会科学のためのデータ分析入門(上)岩波書店

  2. 社会科学のためのデータ分析入門(下)岩波書店

  1. Qiita: Rで地図を描く時便利な「maps, leaflet, NipponMap」