Leaflet Map-第二个多边形使第一层不可点击 [英] Leaflet Map - second Polygon makes the first layer unclickable
问题描述
我正在制作美国社区调查数据地图.目前,我有一个主要层(在下面称为plotMerge$incomePerCapita
).它运行良好,弹出窗口,图像和所有内容都已充实.当我添加第二层以提供县和地区边界时,区域边界变得不可单击,似乎被新层掩盖了.
I am working on a map of American Community Survey data. Currently I have a primary layer (seen below as plotMerge$incomePerCapita
). It works well, has a fully fleshed out popup, image and all. When I add a second layer, to provide county and regional boundaries, the tract boundaries become un-clickable, seemingly masked by the new layer.
如果我交换图层顺序,则区域边界将变得不可见.
If I swap the layer order, the regional boundaries become invisible.
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
有没有办法只显示第二层边界的轮廓,而保持上一层的完整功能呢?
Is there a way to show just the outline of a boundary in a second layer, yet leave the full functionality of the previous layer intact?
我应该以不同的方式编写addPolygons脚本以显示边界而不施加功能模糊的图层吗?
Should I be scripting the addPolygons in a different way to show the boundary without imposing a functionally obscure layer?
更新:
我修复了一个错误,并交换了addPolygons
代码以正确的顺序获取图层.
I fixed an error and swapped the addPolygons
code to get the layers in the right order.
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)
:
In case you are working with proper spatial objects using sp
, you can coerce your countyPoly
into a SpatialLines(DataFrame)
:
countyLines <- as(countyPoly, "SpatialLinesDataFrame")
然后,您应该能够在显示顶部线条的同时单击基础多边形层.
Then you should be able to click the underlying polygon layer while showing the lines on top.
作为可重现的示例,您可以尝试:
As a reproducible example you can try:
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 Map-第二个多边形使第一层不可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!