Rで解析:GoogleMapやStamenMapを利用「ggmap」パッケージ

Rの解析に役に立つ記事
スポンサーリンク

地図をプロットするパッケージは多くありますが、シェープファイルを用意したり、使用方法が複雑だったりすることがあります。APIの利用回数の制限がありますが、GoogleMapやStamenMapのデータを利用して地図をプロットする「ggmap」パッケージを紹介します。なお、通常の利用であればAPIの利用回数の制限は問題ないかと思います。

本パッケージでは、Google APIを利用したルートを地図上に表示することもできます。

例に、札幌駅から北海道大学へ行くルートを示します。札幌駅から北海道大学までは以外と近く、1.127kmの距離でした。

Stamen Map
http://maps.stamen.com

パッケージのバージョンは3.0.1。R version 4.2.2でコマンドを確認しています。

スポンサーリンク

パッケージのインストール

下記コマンドを実行してください。

#パッケージのインストール
install.packages("ggmap")

実行コマンド

詳細はコマンド、パッケージヘルプを確認してください。

#パッケージの読み込み
library("ggmap")

#地名で緯度経度をグーグルから取得:geocodeコマンド
LonLatData <- geocode("sapporo odori", source = "google")
lon     lat
1 141.3477 43.0599

#&#32239;&#24230;&#32076;&#24230;&#12487;&#12540;&#12479;&#12363;&#12425;&#20303;&#25152;&#12434;&#12464;&#12540;&#12464;&#12523;&#12363;&#12425;&#21462;&#24471;:revgeocode&#12467;&#12510;&#12531;&#12489;
revgeocode(c(lon = LonLatData[1, 1], lat = LonLatData[1, 2]))
[1] "Nish 7chome Dori, &#332;d&#333;rinishi, Ch&#363;&#333;-ku, Sapporo-shi, Hokkaid&#333; 060-0042, Japan"

#&#12464;&#12540;&#12464;&#12523;&#12510;&#12483;&#12503;&#30011;&#20687;&#12398;&#21462;&#24471;:get_googlemap&#12467;&#12510;&#12531;&#12489;
#&#22320;&#22259;&#12398;&#31278;&#39006;&#12434;&#25351;&#23450;:maptype&#12458;&#12503;&#12471;&#12519;&#12531;;"terrain", "satellite", "roadmap", "hybrid"&#12364;&#25351;&#23450;&#21487;&#33021;
#&#25313;&#22823;&#12398;&#25351;&#23450;:zoom&#12458;&#12503;&#12471;&#12519;&#12531;;1-21&#12398;&#25972;&#25968;&#12391;&#25351;&#23450;&#21487;&#33021;
GMapData <- get_googlemap(center = c(lon = LonLatData[1, 1], lat = LonLatData[1, 2]),
                          zoom = 16, size = c(640, 640), scale = 2, format = "png8",
                          maptype = "hybrid", language = "jpn", sensor = FALSE,
                          messaging = FALSE, urlonly = FALSE, filename = "ggmapTemp",
                          color = "color", force = FALSE)
#&#12503;&#12525;&#12483;&#12488;
ggmap(GMapData)

#from&#12363;&#12425;to&#12414;&#12391;&#12398;&#12523;&#12540;&#12488;&#12434;&#21462;&#24471;:route&#12467;&#12510;&#12531;&#12489;
#&#21462;&#24471;&#12523;&#12540;&#12488;&#12398;&#12514;&#12540;&#12489;&#25351;&#23450;:mode&#12458;&#12503;&#12471;&#12519;&#12531;;"driving", "walking", "bicycling", "transit"
RouteData <- route(from = "sapporo station", to = "hokkaido university", mode = "walking",
                   structure = "route", output = "simple")
RouteData
m    km     miles seconds    minutes       hours leg      lon      lat
1   104 0.104 0.0646256      74  1.2333333 0.020555556   1 141.3508 43.06866
2    12 0.012 0.0074568      11  0.1833333 0.003055556   2 141.3499 43.06888
3   108 0.108 0.0671112      79  1.3166667 0.021944444   3 141.3498 43.06891
4    71 0.071 0.0441194      51  0.8500000 0.014166667   4 141.3485 43.06879
5    58 0.058 0.0360412      42  0.7000000 0.011666667   5 141.3484 43.06942
6    68 0.068 0.0422552      48  0.8000000 0.013333333   6 141.3482 43.06993
7    57 0.057 0.0354198      40  0.6666667 0.011111111   7 141.3480 43.07052
8   139 0.139 0.0863746     100  1.6666667 0.027777778   8 141.3479 43.07103
9  1127 1.127 0.7003178     811 13.5166667 0.225277778   9 141.3464 43.07100
10   NA    NA        NA      NA         NA          NA  NA 141.3401 43.07805
#&#12487;&#12540;&#12479;&#12434;&#12503;&#12525;&#12483;&#12488;
qmap("sapporo station, hokkaido university", zoom = 15) +
  geom_path(data = RouteData, aes(x = lon, y = lat),
            colour = "red", size = 1.5, lineend = "round")

#Stamen Map&#12363;&#12425;&#12487;&#12540;&#12479;&#12434;&#21462;&#24471;:Stamen Map&#12467;&#12510;&#12531;&#12489;
#&#21462;&#24471;&#12456;&#12522;&#12450;&#12398;&#35373;&#23450;
#&#22823;&#20307;&#12398;&#26413;&#24140;
bbox <- c(left = 141.2, bottom = 43.0, right = 141.5, top = 43.15)
#&#21462;&#24471;&#30011;&#20687;&#26522;&#25968;&#12364;&#22810;&#12367;&#12394;&#12427;zoom&#35373;&#23450;&#12384;&#12392;&#12503;&#12525;&#12483;&#12488;&#12358;&#12414;&#12367;&#12356;&#12363;&#12394;&#12356;&#22580;&#21512;&#12364;&#12354;&#12426;&#12414;&#12377;
#maptype = "watercolor"
ggmap(get_stamenmap(bbox, maptype = "watercolor", zoom = 13))
#maptype = "toner-lite"
ggmap(get_stamenmap(bbox, maptype = "toner-lite", zoom = 11))

出力例

・get_googlemapコマンド:terrain

terrain

・get_googlemapコマンド:satellite

satellite

・get_googlemapコマンド:roadmap

roadmap

・get_googlemapコマンド:hybrid

hybrid

・routeコマンド

route

・Stamen Mapコマンド:watercolor

Stamen Map

・Stamen Mapコマンド:toner-lite

toner-lite

少しでも、あなたのウェブや実験の解析が楽になりますように!!

タイトルとURLをコピーしました