1 緯度経度付き貿易データ

オンラインでデータ読み込む場合は、以下のコード。

trade <- read_csv("https://ayumu-tanaka.github.io/teaching/line.csv")
  • 便宜的に準備したデータセット。
  • country: USA, AUS, CHN, NZL, DEUの5カ国とJPN
  • import: 上記5カ国から日本への輸出額
  • long lat: 緯度経度
  • countryname: United States, Australia, China, New Zealand, Germanyの5カ国とJapan
library(readr)
line <- read_csv("line.csv")
head(line)
## # A tibble: 6 × 5
##   country import   long   lat countryname  
##   <chr>    <dbl>  <dbl> <dbl> <chr>        
## 1 USA      17.9  -113.   45.7 United States
## 2 AUS       9.32  134.  -25.7 Australia    
## 3 CHN       5.54  104.   36.6 China        
## 4 NZL       2.51  171.  -41.8 New Zealand  
## 5 DEU       1.91   10.4  51.1 Germany      
## 6 JPN      NA     138.   37.5 Japan

2 直線でつなぐ with addPolylines

  • addPolylinesで線を描ける。
  • 線は、データ上、前後の国の緯度と経度を結ぶ。

2.1 日米を線でつなぐ

library(readr)
line <- read_csv("line.csv")
USA<-subset(line,country=="USA"|country=="JPN")

library(leaflet)
x<-leaflet()
x<-addTiles(x)
x<-addPolylines(x,lng=USA$long,lat = USA$lat, weight=USA$import)
x

2.2 線でつなぐ国を増やす

  • addPolylinesで線を描ける。
  • 線は、データ上、前後の国の緯度と経度を結ぶ。
  • そのため、日本とアメリカだけのデータ、日本とオーストラリアだけのデータ・・・を作成する。
library(readr)
line <- read_csv("line.csv")
USA<-subset(line,country=="USA"|country=="JPN")
AUS<-subset(line,country=="AUS"|country=="JPN")
CHN<-subset(line,country=="CHN"|country=="JPN")
NZL<-subset(line,country=="NZL"|country=="JPN")
DEU<-subset(line,country=="DEU"|country=="JPN")


library(leaflet)
x<- leaflet()
x<- addTiles(x)
x<-addPolylines(x,lng=USA$long,lat = USA$lat, weight=USA$import)
x<-addPolylines(x,lng=AUS$long,lat = AUS$lat, weight=AUS$import)
x<-addPolylines(x,lng=CHN$long,lat = CHN$lat, weight=CHN$import)
x<-addPolylines(x,lng=NZL$long,lat = NZL$lat, weight=NZL$import)
x<-addPolylines(x,lng=DEU$long,lat = DEU$lat, weight=DEU$import)

x

3 航路でつなぐ with geosphere

  • 日米を直線ではなく、航路(曲線)で結ぶ
  • まずパッケージgeosphereを用いて、緯度と経度で表される点と点の間をつなぐ点の集合を求める。
  • ここで使うgcIntermediate()という関数は、2地点の航路を求める。
  • addStartEnd = TRUE は出発点と到着点を結果の中に含めることを指定。

3.1 日米航路

#データ
library(readr)
line <- read_csv("line.csv")
USA<-subset(line,country=="USA"|country=="JPN")

#緯度経度登録
library(geosphere)
JPNcenter <- c(137.96, 37.54)
USAcenter <- c(-112.98, 45.72)

#地図1
gcUSA1 <- gcIntermediate(JPNcenter, USAcenter, addStartEnd = TRUE)
library(leaflet)
y<- leaflet()
y<- addTiles(y)
y<-addPolylines(y,data = gcUSA1,col="red")
y

3.2 日付変更線を考慮して日米航路を設定。

  • 日米間で日付変更線またぐため、breakAtDateLine=TRUEと設定。
  • sp = TRUEという設定することで空間データとしてRが認識。
  • 日付変更線を考慮して地図上では航路を2分割するため、sp=TRUEという設定が必要になる。
  • ウェイトとして、日米の貿易額を用いる。
#データ
library(readr)
line <- read_csv("line.csv")
JPN<-subset(line,country=="JPN")
USA<-subset(line,country=="USA")

#緯度経度登録
library(geosphere)
JPNcenter <- JPN[, c(3:4)]
USAcenter <- USA[, c(3:4)]

#地図2
gcUSA2 <- gcIntermediate(JPNcenter, USAcenter,sp = TRUE,addStartEnd = TRUE, breakAtDateLine=TRUE)

library(leaflet)
y<- leaflet()
y<- addTiles(y)
y<-addPolylines(y,data = gcUSA2,col="blue",weight = USA$import)
y

3.3 国の追加

#データ
library(readr)
line <- read_csv("line.csv")
JPN<-subset(line,country=="JPN")
USA<-subset(line,country=="USA")
AUS<-subset(line,country=="AUS")
CHN<-subset(line,country=="CHN")
NZL<-subset(line,country=="NZL")
DEU<-subset(line,country=="DEU")

#緯度経度登録
library(geosphere)
JPNcenter <- JPN[, c(3:4)]
USAcenter <- USA[, c(3:4)]
AUScenter <- AUS[, c(3:4)]
CHNcenter <- CHN[, c(3:4)]
NZLcenter <- NZL[, c(3:4)]
DEUcenter <- DEU[, c(3:4)]

#地図2
gcUSA2 <- gcIntermediate(JPNcenter, USAcenter,sp = TRUE,addStartEnd = TRUE, breakAtDateLine=TRUE)
gcAUS2 <- gcIntermediate(JPNcenter, AUScenter,sp = TRUE,addStartEnd = TRUE, breakAtDateLine=TRUE)
gcCHN2 <- gcIntermediate(JPNcenter, CHNcenter,sp = TRUE,addStartEnd = TRUE, breakAtDateLine=TRUE)
gcNZL2 <- gcIntermediate(JPNcenter, NZLcenter,sp = TRUE,addStartEnd = TRUE, breakAtDateLine=TRUE)
gcDEU2 <- gcIntermediate(JPNcenter, DEUcenter,sp = TRUE,addStartEnd = TRUE, breakAtDateLine=TRUE)

library(leaflet)
y<- leaflet()
y<- addTiles(y)
y<-addPolylines(y,data = gcUSA2,col="blue",weight = USA$import,
                label =USA$import,popup = "USA")
y<-addPolylines(y,data = gcAUS2,col="blue",weight = AUS$import,
                label =AUS$import,popup = "AUS")
y<-addPolylines(y,data = gcCHN2,col="blue",weight = CHN$import,
                label =CHN$import,popup = "CHN")
y<-addPolylines(y,data = gcNZL2,col="blue",weight = NZL$import,
                label =NZL$import,popup = "NZL")
y<-addPolylines(y,data = gcDEU2,col="blue",weight = DEU$import,
                label =DEU$import,popup = "DEU")

y

4 <例>ロシアとウクライナから日本への輸出額を描く

UNComtrade UNComtrade

#緯度経度
JPNcenter <- c(137.96, 37.54)
RUScenter<-c(93.85, 66.755)
UKRcenter<-c(30.51069,50.46730)

#航路の計算
library(geosphere)
gcRUS <- gcIntermediate(JPNcenter, RUScenter, addStartEnd = TRUE)
gcUKR <- gcIntermediate(JPNcenter, UKRcenter, addStartEnd = TRUE)

#地図の描画
y<- leaflet()
y<- addTiles(y)
#線の太さは貿易額
y<-addPolylines(y,data = gcRUS,col="blue",weight = 9053904133/100000000)
y<-addPolylines(y,data = gcUKR,col="red",weight = 183662260/100000000)
y