Angus Watters
Angus Watters

Reputation: 21

Leaflet layer control not toggling or displaying correct data from input

I am trying to add layers to a leaflet map in which they layers can toggle the circle markers by ranges (0 - 100, 100 - 200, etc) but for some reason when I toggle the 100 - 200ft layer nothing changes on the map. The 'All' and 'AMA' layers will toggle.

Moreover the 100-200 layer is not displaying the data that is within the b dataframe. The b dataframe only contains values from 100 - 200 in the dtw column but the markers on the map are displaying below 100 and above 200 values. However the x dataframe will display the entire dataframe from 0 -1000 and will toggle correctly. All of the sf objects are in the same CRS and are of the same class.

I am new to leaflet and layer controls and I would really appreciate some help. Here are images of the leaflet map not toggling correctly. Thanks!

x = usgs_spatial %>% st_transform(4326)

ama3 = ama %>% st_transform(4326)


pal1 = RColorBrewer::brewer.pal(9,"Blues")
pal2 = RColorBrewer::brewer.pal(9,"YlOrRd")

pals1 = colorNumeric(pal1, domain = x$dtw)
pals2 = colorNumeric(pal2, domain = x$dtw)
pals3 = colorBin("magma", domain = 1:8)

a = dtw_range(x, 0, 100) %>% select(wellid, date, dtw, measurement_dist)
b = dtw_range(x, 100, 200) %>% select(wellid, date, dtw, measurement_dist)
c = dtw_range(x, 200, 300) %>% select(wellid, date, dtw, measurement_dist)
leaflet() %>% 
  addProviderTiles(providers$CartoDB.Positron,[enter image description here][1] group = 'Tiles') %>% 
  addCircleMarkers(data =  b, #clusterOptions = markerClusterOptions(interactive()),
                   color = ~pals2(dtw), fillOpacity = .5,
                   stroke = FALSE,
                   popup = leafpop::popupTable(st_drop_geometry(x[,c(4, 7, 8, 13)]),
                                               feature.id = FALSE, 
                                               row.numbers = FALSE), group = '100 - 200 ft') %>% 
  addCircleMarkers(data = x, #clusterOptions = markerClusterOptions(interactive()),
                   color = ~pals2(dtw), fillOpacity = .5,
                   stroke = FALSE,
                   popup = leafpop::popupTable(st_drop_geometry(x[,c(4, 7, 8, 13)]),
                                               feature.id = FALSE,
                                               row.numbers = FALSE), group = 'All') %>%
  addPolygons(data = ama3,
              fillColor  = ~pals3(OBJECTID),
              color = 'black',
              label = ~MAP_LABEL, group = 'AMA') %>%
  addLayersControl(overlayGroups = c('All', '100-200 ft', 'AMA'), baseGroups = c("Tiles"))

Upvotes: 0

Views: 506

Answers (1)

Angus Watters
Angus Watters

Reputation: 21

I figured it out, I had not changed the x argument in the popupTable() to the appropriate dataframe, and I also had spaces between the '-' in the addlayGroup() for '100-200 ft' which is why the toggle option was not being picked up.

Upvotes: 0

Related Questions