传单地图 - 第二个多边形使第一层不可点击
Leaflet Map - second Polygon makes the first layer unclickable
我正在制作美国社区调查数据的地图。目前我有一个主要层(如下所示 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))
感谢观看!
如果您使用 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
对于leaflet_1.1.0
,@TimSalabim 提出的解决方案似乎不再适用。
您现在可以使用 addPolylines
代替 addPolygons
来解决问题。
我正在制作美国社区调查数据的地图。目前我有一个主要层(如下所示 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))
感谢观看!
如果您使用 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
对于leaflet_1.1.0
,@TimSalabim 提出的解决方案似乎不再适用。
您现在可以使用 addPolylines
代替 addPolygons
来解决问题。