メモ:ggplot2::geom_sf()の背景にOpenStreetMapの地図画像を使いたいときはggspatial::annotation_map_tile()
ggspatialがsf対応したらしい。このパッケージはところどころイケてない感もあるけど*1、まあでも自分でパッケージつくるよりは楽そうなので、まあ使うかーという感じ
素のggplot2でやりたい人は、英語ブログの方にやり方を書いたのでそれを読んでください(ツッコミ歓迎です)。
とりあえず使い方はこんな感じっぽい。
library(ggspatial) #> Loading required package: ggplot2 #> Loading required package: sf #> Linking to GEOS 3.6.1, GDAL 2.2.3, proj.4 4.9.3 nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) ggplot(nc) + # デフォルトのzoominは-2で、それだとちょっと画像が荒すぎる気がするので0にする annotation_map_tile(zoomin = 0) + geom_sf(aes(fill = AREA), alpha = 0.5) + # ライセンス表示をちゃんとつける labs(caption = "\U00a9 OpenStreetMap contributors") + theme_minimal() #> Loading required namespace: raster #> Zoom: 7 #> Fetching 15 missing tiles #> | | | 0% | |==== | 7% | |========= | 13% | |============= | 20% | |================= | 27% | |====================== | 33% | |========================== | 40% | |============================== | 47% | |=================================== | 53% | |======================================= | 60% | |=========================================== | 67% | |================================================ | 73% | |==================================================== | 80% | |======================================================== | 87% | |============================================================= | 93% | |=================================================================| 100% #> ...complete!
Created on 2018-06-24 by the reprex package (v0.2.0).
*1:とりあえず、ライセンス表示つけてほしい...