您绝对可以在leafletR中进行图层控制。如果您的版本没有此功能,则需要更新,可能来自最新的GITHUB版本。
我正在制作一个具有图层控件的地图,如照片所示。以下是使其发生的代码。正如您所看到的,每个
addPolygons
都有一个
group=" A Name"
,这是您在我的图片上的复选框中标识层的位置。
map<-leaflet()%>%
addTiles()%>%
addPolygons(data = plotMerge,
fillColor = ~pal(plotMerge$incomePerCapita),
color = "#000000",
fillOpacity = 0.8,
group="Tract",
weight = 0.2,
popup=popup)%>%
addPolygons(data = countyPoly,
fillColor = "transparent",
color = "#000000",
fillOpacity = 0.8,
group="County",
popup=countyPoly@data$NAME,
weight = 2)%>%
addPolygons(data = townPoly,
fillColor = "transparent",
color = "#000000",
fillOpacity = 0.8,
group="Town",
weight = .8,
popup=townPoly@data$TOWN)%>%
addPolygons(data = rphnPoly,
fillColor = "transparent",
color = "#000000",
fillOpacity = 0.8,
group="Public Health Region",
weight = .8,
popup=rphnPoly@data$PHN)%>%
addLegend(pal = pal,
values = plotMerge$incomePerCapita,
position = "bottomright",
title = "State-wide Income Percentiles",
labFormat = labelFormat(digits=1))%>%
addLayersControl(
overlayGroups =c("County", "Town", "Public Health Region", "Tract"),
options = layersControlOptions(collapsed=FALSE)
)
saveWidget(map, file="map1.html", selfcontained=FALSE)
这是它的外观:
![ACS社会脆弱指数由R & LEaflet制作](https://istack.dev59.com/wqzga.webp)
您还可以添加其他控件,请在此处查看:
Leaflet R Hidden Layers