【问题标题】:how to create a world street map with r?如何用 r 创建世界街道地图?
【发布时间】:2019-03-02 23:59:32
【问题描述】:

我想使用 Rstudio 创建世界街道地图。我有这个代码:

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)

问题:我想查看国家/地区的名称并查看像这样的地图:

感谢您的帮助!

【问题讨论】:

    标签: r ggplot2 maps spatial


    【解决方案1】:

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

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

    除了leaflet,这里我进一步介绍了另外两个创建交互式地图的包,分别是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")
    

    # 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_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))
    

    【讨论】:

    • 感谢您的回答。可以选择在没有交互式地图的情况下创建地图吗?只看到带有国家名称等的地图......?谢谢!
    • @LiranBenZion 您要求的底图是典型的在线互动设置,所以您的要求令人困惑。
    • 好的,我明白了。我将尝试解释我自己:在我的代码中,我看到了没有名字的世界地图。我尝试查看带有名称的地图..这可能吗?谢谢!
    • @LiranBenZion 很高兴为您提供帮助。如果我的帖子解决了您的问题,请接受它作为答案。
    猜你喜欢
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2019-08-19
    • 2017-12-15
    相关资源
    最近更新 更多