{mapdeck}を試してみる(5/5) Arc編
{mapdeck}を試すシリーズ最終回。最後はadd_arc()
を試す。
From-to分析(滞在人口) / マップ - RESAS 地域経済分析システムのように起点と終点間を円弧で描画するにはadd_arc()
を使用する。{geosphere}
やggplot2::geom_curve()
など色々試して狭域ではあまりうまくいかなかったのでこれは嬉しい。
前回と同様国土数値情報 交通流動量 パーソントリップOD量データで試してみる。
データの準備
データは前回同様だがmapdeck::add_arc()
は起点・終点をそれぞれのc("経度の列名", "緯度の列名")
かsfc
の列名で指定する必要があるためsf::st_cast()
でPOINTに変換する。
さらにsf::st_cast()
でLINESTRING型からPOINT型に変換すると起点・終点でレコードが2倍になるためtidyr::spread()
でwide-formatに変換する。
> library(tidyr) > sf.od.sub.point <- sf.od.sub %>% + select(S05b_003, S05b_004, S05b_035, geometry) %>% + st_cast("POINT") %>% + mutate(od = ifelse(row_number() %% 2 == 1, "geom_origin", "geom_destination"), + geom_text = st_as_text(geometry)) %>% + st_set_geometry(NULL) %>% + spread(key = od, value = geom_text) %>% + mutate(geom_origin = st_set_crs(st_as_sfc(geom_origin), 4326), + geom_destination = st_set_crs(st_as_sfc(geom_destination), 4326)) %>% + st_as_sf() 警告メッセージ: st_cast.sf(., "POINT") で: repeating attributes for all sub-geometries for which they may not be constant > > glimpse(sf.od.sub.point) Observations: 30 Variables: 5 $ S05b_003 <chr> "0010", "0011", "0012", "0013", "0020", "0021", "0022", "0023", "0024", "0030", "0031", "0032", "0033", "0034"... $ S05b_004 <chr> "0031", "0031", "0031", "0031", "0031", "0031", "0031", "0031", "0031", "0031", "0031", "0031", "0031", "0031"... $ S05b_035 <dbl> 7258, 8850, 6436, 13884, 5099, 4211, 8816, 8493, 3445, 14493, 88833, 11300, 18386, 5832, 5647, 5443, 4016, 36... $ geom_destination <POINT [°]> POINT (139.757 35.68431), POINT (139.7418 35.68724), POINT (139.7668 35.69636), POINT (139.7544 35.6733... $ geom_origin <POINT [°]> POINT (139.7455 35.66203), POINT (139.7455 35.66203), POINT (139.7455 35.66203), POINT (139.7455 35.662...
Arcの描画
{mapdeck}
でArcの描画はadd_arc()
で行う。引数は以下の通り。
引数 | 説明 |
---|---|
map | mapdeck地図オブジェクト |
data | レイヤで使用するデータ |
layer_id | レイヤID(同じタイプのレイヤを区別するために使用される) |
origin | 起点の緯度・経度 or sfcの列 |
destination | 起点の緯度・経度 or sfcの列 |
stroke_from | 起点の線の色 |
stroke_from_opacity | 起点の線の色の透明度 |
stroke_to | 終点の線の色 |
stroke_to_opacity | 終点の線の色の透明度 |
stroke_width | 線の太さ |
tooltip | ツールチップに表示するテキスト or HTMLの列名 |
auto_highlight | マウスオーバー時にオートハイライトするか |
digits | 緯度・経度の小数点以下の桁数 |
palette | 色を生成する関数 |
上で準備したデータをmapdeck:: add_polygon()
で描画すると以下のようになる。
> library(googlePolylines) > library(mapdeck) > mapdeck(style = mapdeck_style("light"), zoom = 10, location = c(139.745433, 35.658581), pitch = 45) %>% + add_arc( + data = sf.od.sub.point, + layer_id = "arc_layer", + origin = "geom_origin", + destination = "geom_destination", + stroke_from = "S05b_035", + stroke_to = "S05b_035", + stroke_width = 3, + tooltip = "S05b_035", + palette = colorRampPalette(rev(brewer.pal(11, "Spectral"))) + )
Stats, Maps n Pix: July 2018のようになかなか綺麗にはいかないけど夏の終わりに花火っぽく。
というわけで{mapdeck}の気になる機能を試してみた。追加できるレイヤには他にも
- GeoJSON
- Point cloud
- Scatter plot
- Screen grid
- Text
があるし、htmlwidgetのためShinyにも簡単に組み込めそう。
ただ現時点では凡例の追加はできないようなので実用的になるのはもう少し先になりそう。