我们可以使用leaflet
包裹。请参阅此链接了解底图的选择(https://leaflet-extras.github.io/leaflet-providers/preview/ https://leaflet-extras.github.io/leaflet-providers/preview/)。这里我使用了“Esri.WorldStreetMap”,它与您的示例图像显示的相同。
library(leaflet)
leaflet() %>%
addProviderTiles(provider = "Esri.WorldStreetMap") %>%
setView(0, 0, zoom = 1)
此外leaflet
,这里我进一步介绍了另外两个用于创建交互式地图的包,它们是tmap
and 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")
Update
这是一种使用以下方法向每个多边形添加文本的方法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
and 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))