Leaflet Map - 第二个Polygon使第一个图层不可点击

时间:2016-05-13 19:31:09

标签: r popup leaflet layer polyline

我正在制作美国社区调查数据的地图。目前我有一个主要层(见下文plotMerge$incomePerCapita)。它运作良好,有一个完全充实的弹出窗口,图像和所有。当我添加第二层时,为了提供县和区域边界,道边界变得不可点击,似乎被新图层掩盖。

如果我交换图层顺序,区域边界将变为不可见。

map1<-leaflet()%>%
  addTiles()%>%

addPolygons(data = plotMerge,
          fillColor = ~pal(plotMerge$incomePerCapita),
          color = "#000000", #this is an outline color
          fillOpacity = 0.8,
          weight = 0.2,
          popup=popup)%>%
addPolygons(data = countyPoly,
            fillColor = "transparent",
           color = "#000000",
           stroke = TRUE,
           weight = 1,
           smoothFactor = 0.5,
           group = "Counties")%>%
addLegend(pal = pal,
            values  = plotMerge$incomePerCapita,
            position = "bottomright",
            title = "State-wide Income Percentiles",
            labFormat = labelFormat(digits=1))

saveas(map1, "map1.html")
map1

有没有办法在第二层显示边界的轮廓,但保留上一层的完整功能?

我是否应该以不同的方式编写addPolygons脚本以显示边界而不强加功能模糊的图层?

更新:

我修正了错误并交换了addPolygons代码,以便按正确的顺序获取图层。

map1<-leaflet()%>%
  addTiles()%>%
addPolygons(data = countyPoly,
            fillColor = "transparent",
           color = "#000000",
           stroke = TRUE,
           weight = 1,
           smoothFactor = 0.5,
           group = "Counties")%>%
addPolygons(data = plotMerge,
          fillColor = ~pal(plotMerge$incomePerCapita),
          color = "#000000", #this is an outline color
          fillOpacity = 0.8,
          weight = 0.2,
          popup=popup)%>%
addLegend(pal = pal,
            values  = plotMerge$incomePerCapita,
            position = "bottomright",
            title = "State-wide Income Percentiles",
            labFormat = labelFormat(digits=1))

感谢您的期待!

2 个答案:

答案 0 :(得分:3)

如果您使用sp处理适当的空间对象,可以将countyPoly强制转换为SpatialLines(DataFrame)

countyLines <- as(countyPoly, "SpatialLinesDataFrame")

然后,您应该能够在显示顶部的线条时单击底层多边形图层。

编辑: 作为一个可重复的例子,您可以尝试:

library(mapview)
library(sp)

pol <- as(gadmCHE, "SpatialPolygons")
ln <- as(gadmCHE, "SpatialLines")

mapview(gadmCHE, color = "blue") + pol # not clickable
mapview(gadmCHE, color = "blue") + ln # clickable

答案 1 :(得分:0)

使用leaflet_1.1.0,似乎@TimSalabim提出的解决方案不再适用。
您现在可以使用addPolylines代替addPolygons来解决问题。