r - 如何用r创建世界街道 map ?

标签 r ggplot2 maps spatial

我想使用 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 :

enter image description here

感谢您的帮助!

最佳答案

我们可以使用leaflet包。请参阅此链接以了解 basemap 的选择 ( https://leaflet-extras.github.io/leaflet-providers/preview/ )。这里我使用了“Esri.WorldStreetMap”,它与您的示例图像显示的相同。

library(leaflet)
leaflet() %>%
  addProviderTiles(provider = "Esri.WorldStreetMap") %>%
  setView(0, 0, zoom = 1)

enter image description here

除了leaflet之外,这里我还介绍了另外两个用于创建交互式 map 的包,它们是tmapmapview

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")

enter image description here

# Plot world using mapview
mapview(World, map.types = "Esri.WorldStreetMap")

enter image description here

更新

这是一种使用 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")

enter image description here

如果您必须使用ggplot2,您可以将数据准备为sf对象并使用geom_sfgeom_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))

enter image description here

关于r - 如何用r创建世界街道 map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54964279/

相关文章:

r - 混合数据帧与向量的乘法

Rcpp - 如何检查任何属性是否为 NULL

r - 用 ggplot2 绘制多列

java - Google map 不显示在 fragment 中

google-maps - 从 Google map 获取边界多边形坐标

ios - 如何在 GMSMarker Google 上制作自定义 UIView 或标签

r - 错误: non-numeric argument to binary operator, but class is numeric

r - 在给定一列的情况下计算 R 中的耗时

r - ggplot2 轴标签中的 SI 前缀

r - 修改 PlotSlope 输出(pequod 包)