我有几列包含分类数据。我想根据这些分类数据创建图层,并使用传单在R中的基础地图上绘制它们。这些列中的每一个都有4个或更多类别。
我对如何解决这个问题完全迷失了。我尝试从一列开始进行一个类别,然后将所有点都放回地图上,并且图层控制了所有点,这不是预期的行为。
这是我所拥有的:
lihn_map <- leaflet(origAddress) %>%
setView(lng = sv_lng, lat = sv_lat, zoom = sv_zoom) %>%
addTiles(group = "OSM (default)") %>%
addCircleMarkers(data = origAddress
, radius = 3
, fillOpacity = 1
, group = "MI"
) %>%
addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>%
# Overlay groups
addCircles(~lat, ~lon, group = "MI") %>%
addLayersControl(
baseGroups = c("OSM (default)", "Toner", "Toner Lite"),
overlayGroups = "MI",
options = layersControlOptions(collapsed = FALSE)
)
lihn_map
我正在尝试执行此循环,它将组添加到图层控件,但是选择不会更改地图:
lsl <- unique(origAddress$LIHN_Line)
mt <- leaflet() %>%
addTiles(group = "OSM (default)") %>%
addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite")
for(i in 1:length(lsl)){
l <- lsl[i]
mt <- mt %>%
addCircleMarkers(
# I have also tried
data = subset(origAddress, LIHN_Line = lsl[i])
data = origAddress
, group = lsl[i]
, radius = 3
, fillOpacity = 0.6)
}
mt <- mt %>%
addLayersControl(
baseGroups = c("OSM (default)", "Toner", "Toner Lite"),
overlayGroups = lsl,
options = layersControlOptions(collapsed = FALSE
, position = "bottomright")
)
mt
我的期望是,该层将仅控制组等于MI
的数据子集,也许我必须创建子集data.frames?如果这样的话,这似乎效率很低,我敢肯定这不是答案。
答案 0 :(得分:0)
我执行了以下操作,并且有效:
# for loop to cycle through adding layers
for(i in 1:length(lsl)){
l <- lsl[i]
mt <- mt %>%
addCircles(
data = subset(origAddress, origAddress$LIHN_Line == lsl[i])
#data = origAddress
, group = lsl[i]
, radius = 3
, fillOpacity = 0.6)
}
使用subset
表现出色