[英]how to create a world street map with r?
我們可以使用leaflet
包。 請參閱此鏈接以了解底圖的選擇( https://leaflet-extras.github.io/leaflet-providers/preview/ )。 在這里,我使用了“Esri.WorldStreetMap”,它與您的示例圖像顯示的相同。
library(leaflet)
leaflet() %>%
addProviderTiles(provider = "Esri.WorldStreetMap") %>%
setView(0, 0, zoom = 1)
除了leaflet
,這里我進一步介紹了另外兩個包來創建交互式地圖,分別是tmap
和mapview
。
library(sf)
library(leaflet)
library(mapview)
library(tmap)
# Gett the World sf data
data("World")
# Turn on the view mode in tmap
tmap_mode("view")
# Plot World using tmap
tm_basemap("Esri.WorldStreetMap") +
tm_shape(World) +
tm_polygons(col = "continent")
# Plot world using mapview
mapview(World, map.types = "Esri.WorldStreetMap")
更新
這是使用tmap
包向每個多邊形添加文本的方法。
library(sf)
library(leaflet)
library(mapview)
library(tmap)
# Gett the World sf data
data("World")
# Turn on the view mode in tmap
tmap_mode("plot")
# Plot World using tmap
tm_basemap("Esri.WorldStreetMap") +
tm_shape(World) +
tm_polygons() +
tm_text(text = "iso_a3")
如果必須使用ggplot2
,則可以將數據准備為sf
對象並使用geom_sf
和geom_sf_text
如下。
library(sf)
library(tmap)
library(ggplot2)
# Gett the World sf data
data("World")
ggplot(World) +
geom_sf() +
geom_sf_text(aes(label = iso_a3))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.