ggmap在第二个透明背景Google地图上分层

时间:2019-02-28 00:02:02

标签: r ggplot2 ggmap

我尝试使用inset_ggmap在第二条仅道路的google地图上失败

m <- ggmap(dt_map_b) +
  geom_polygon(data = map_data, 
               aes(fill = outcomes,
                   x = long, 
                   y = lat.x, 
                   group = group),
               alpha = 0.5) +
  inset_ggmap(dt_map_r)

一切正常,但是当我关闭地形特征的可见性时,我发现所有图像都隐藏在原始图像的底下

black background like spilled ink

我知道可以更改地图with the google api的背景颜色,但是可以从ggmap更改背景颜色吗?也许通过更改添加到URL的内容?

1 个答案:

答案 0 :(得分:0)

  m <- leaflet(outcomes_map) %>%

  addMapPane("base_map", zIndex = 410) %>%    # Level 1: base
  addMapPane("polygons", zIndex = 420) %>%    # Level 2: polygons
  addMapPane("waters", zIndex = 430) %>%      # Level 3: waters
  addMapPane("roads", zIndex = 440) %>%       # Level 4: roads
  addMapPane("labels", zIndex = 450) %>%      # Level 5: labels

    # adds in the base map

    addTiles(urlTemplate = base_map,
             options = pathOptions(pane = "base_map")) %>%

    # adds in the polygons containing the loaded data

    addPolygons(color = ~pal5(rank),
                weight = 1,
                fillOpacity  = 1,
                options = pathOptions(pane = "polygons")) %>%

    # adds in the other layers for aesthetics like roads, water, and place labels

    addTiles(urlTemplate = water_map,
             options = pathOptions(pane = "waters")) %>%

    addTiles(urlTemplate = road_map,
             options = pathOptions(pane = "roads",
                                   opacity = 0.75)) %>%

    addTiles(urlTemplate = label_map,
             options = pathOptions(pane = "labels")) %>%

      setView(lng = -80.843297, lat = 35.225256, zoom = 11)

        # you can export as either an interactive map or a static image

        mapshot(m, url = file.path(graph_export_path,"alrighty.html"))

        mapshot(m, file = file.path(graph_export_path,"alrighty.png"))

结果证明,仅使用leaflet和mapshot()即可轻松完成此操作。您真正要做的就是为感兴趣的功能创建几个不同的层,然后创建窗格以按所需顺序组织它们。您需要设置一个mapbox帐户并使用api提取地图,但这在R文档的传单中非常简单。这样,您就可以对地图的外观进行更多的自定义!