我想使用 Rstudio 创建世界街道 map 。我有这个代码:
countries_map <-map_data("world")
world_map<-ggplot() +
geom_map(data = countries_map,
map = countries_map,aes(x = long, y = lat, map_id = region, group = group),
fill = "light blue", color = "black", size = 0.1)
问题:我想查看国家/地区的名称并查看如下所示的 map :
感谢您的帮助!
最佳答案
我们可以使用leaflet
包。请参阅此链接以了解 basemap 的选择 ( https://leaflet-extras.github.io/leaflet-providers/preview/ )。这里我使用了“Esri.WorldStreetMap”,它与您的示例图像显示的相同。
library(leaflet)
leaflet() %>%
addProviderTiles(provider = "Esri.WorldStreetMap") %>%
setView(0, 0, zoom = 1)
除了leaflet
之外,这里我还介绍了另外两个用于创建交互式 map 的包,它们是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))
关于r - 如何用r创建世界街道 map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54964279/