将两个addLayersControl添加到一张地图中(标记位于多个组中)

时间:2019-04-09 21:15:44

标签: r leaflet

我有一个数据集,其中包括一个县中观察到的每只鸟的日期和种类。我已经使用传单映射了它们,但是想要使用两个AddLayersControl来控制日期和种类。现在我只能控制年份或物种。我想要第二组复选框,以便我也可以控制该物种。如果希望未选中年份组或未选中物种组,我希望标记消失。

我想我需要做的是将每个标记分配给我可以独立控制的两个不同的组。我认为我无法将某些标记指定为基本图层,因为我不希望它们的某些子集始终可用。我还尝试过添加另一个AddLayersControl-可悲的是,第二个始终会赢,而且似乎您不能在同一张地图上拥有两个。

library(leaflet)
library(magrittr)
library(dplyr)
library(htmltools)

# Data
birds <- data.frame(observed_on = c("4/4/2009", "4/1/2009", "3/6/2016", "2/9/2016"),
                  url = c("http://www.inaturalist.org/observations/2236",
                          "http://www.inaturalist.org/observations/2237",
                          "http://www.inaturalist.org/observations/2778201",
                          "https://www.inaturalist.org/observations/9796150"),
                  latitude = c(43.08267975, 43.0844841, 43.055512, 43.0180932),
                  longitude = c(-89.43265533, -89.43793488, -89.314878, -89.52836138),
                  scientific_name = c("Agelaius phoeniceus", "Bubo virginianus", "Quiscalus quiscula", "Strix varia"),
                  common_name = c("Red-winged Blackbird", "Great Horned Owl", "Common Grackle", "Barred Owl"),
                  taxon_order_name = c("Passeriformes", "Strigiformes", "Passeriformes", "Strigiformes"),
                  taxon_species_name = c("Agelaius phoeniceus", "Bubo virginianus", "Quiscalus quiscula", "Strix varia" ),
                  year = c("2009", "2009", "2016", "2016"))

# Leaflet Chart Formatting --------------------------------------------------------

palette <- colorFactor(palette = rainbow(length(unique(birds$taxon_order_name))), domain = birds$taxon_order_name)

# Leaflet Chart -------------------------------------------------------------------

mymap <-  leaflet(birds) %>% addProviderTiles(providers$CartoDB.Positron) %>% 
  setView(lng = -89.398721, lat = 43.071580, zoom = 13)

for (t in unique(birds$year)){
  sub <- birds[birds$year == t,]
  labels <- mapply(function(x, y, z, a) {
    HTML(sprintf("%s<br><em>%s</em><br>%s<br><a href=%s>link</a>", htmlEscape(x), htmlEscape(y), htmlEscape(z), htmlEscape(a)))},
    sub$common_name, sub$taxon_species_name, sub$observed_on, sub$url, SIMPLIFY = FALSE)
  mymap <- mymap %>% addCircleMarkers(data = sub, lng = ~longitude, lat = ~latitude,
                                      fillOpacity = 0.6, radius = 8, fillColor = ~palette(taxon_order_name), 
                                      color = "black", weight = 1, opacity = 0.5, popup = labels,
                                      group = as.character(t))
}

mymap %>% 
  addLegend(pal = palette, values = ~taxon_order_name, title = "Taxon Order") %>%
  addLayersControl(overlayGroups = as.character(unique(birds$year)), options = layersControlOptions(collapsed = FALSE))
  # addLayersControl(overlayGroups = unique(birds$taxon_order_name), options = layersControlOptions(collapsed = FALSE))

map showing points with both year and species info but layers control for only year

1 个答案:

答案 0 :(得分:0)

这项工作吗?

addLayersControl(overlayGroups = as.character(c(unique(birds$year),unique(birds$taxon_order_name)), options = layersControlOptions(collapsed = FALSE))