rgisr-leafletleaflet.markercluster

Leaflet popup when using markerClusterOptions()


My difficulty is that when using markerClusterOptions() the point closest to the exploded circle cannot be clicked on. No issue with the points further away from the cluster marker. In the below example the popup works well for the 3 slightly further away markers but not the closest one to the north of the circle. Is there a way of setting the minimum distance from the cluster marker or an alternative way to fix this?

A reprex for the dataset

b <- structure(list(unique_id = c("jm_1987_sierraleone", "jm_1987_sierraleone", 
                             "jm_1987_sierraleone", "jm_1987_sierraleone"), town_village = c("Yengema", 
                                                                                             "Yengema", "Yengema", "Yengema"), classification = c("mastomys natalensis", 
                                                                                                                                                  "mastomys natalensis", "mastomys natalensis", "mastomys natalensis"
                                                                                             ), assay = c("path_1_tested", "path_2_tested", "ab_ag_path_1_positive", 
                                                                                                          "ab_ag_path_2_positive"), number = c(8, 8, 0, 0), pathogen_tested = c("lassa_mammarenavirus", 
                                                                                                                                                                                "lassa_mammarenavirus", "lassa_mammarenavirus", "lassa_mammarenavirus"
                                                                                                          ), pa_colour = c("#f1a340", "#f1a340", "#f1a340", "#f1a340"), 
               lat = c(`12747` = 8.33333301544189, `12748` = 8.33333301544189, 
                       `12749` = 8.33333301544189, `12750` = 8.33333301544189), 
               lon = c(`12747` = -10.6333332061768, `12748` = -10.6333332061768, 
                       `12749` = -10.6333332061768, `12750` = -10.6333332061768)), row.names = c(NA, 
                                                                                                 -4L), sf_column = "geometry", agr = structure(c(unique_id = NA_integer_, 
                                                                                                                                                 town_village = NA_integer_, 
                                                                                                                                                 classification = NA_integer_, assay = NA_integer_, number = NA_integer_, 
                                                                                                                                                 pathogen_tested = NA_integer_, pa_colour = NA_integer_, 
                                                                                                                                                 lat = NA_integer_, lon = NA_integer_), .Label = c("constant", 
                                                                                                                                                                                                   "aggregate", "identity"), class = "factor"), class = "data.frame")

Leaflet code

leaflet() %>%
  addTiles() %>%
  setView(lng = 0, lat = 10, zoom = 5) %>%
  addCircleMarkers(data = b,
                   fillColor = "black",
                   fillOpacity = 0.3,
                   stroke = F,
                   radius = 1) %>%
  addCircleMarkers(lng = b$lon,
                   lat = b$lat,
                   fillColor = b$pa_colour,
                   fillOpacity = 0.8,
                   stroke = F,
                   radius = 6,
                   popup = paste0("Study ID: ", b$unique_id,
                                  "<br>",
                                  "Rodent species: ", b$classification,
                                  "<br>",
                                  "Microorganism tested: ", b$pathogen_tested,
                                  "<br>",
                                  "Village/Region: ", b$town_village),
                   clusterOptions = markerClusterOptions()) %>%
  clearControls() %>%
  addLegend("topright",
            title = "Presence or Absence",
            colors = c("#f1a340", "#998ec3"),
            labels = c("Absence", "Presence"),
            opacity = 0.8)

Screenshots of the challenge.

Exploded points, popups not shown

Exploded point with popup


Solution

  • One option is to increase the distance away from the center that spiderfied markers are placed by adding a spiderfyDistanceMultiplier to the markerClusterOptions, e.g.:

    clusterOptions = markerClusterOptions(spiderfyDistanceMultiplier=1.5)
    

    See https://github.com/Leaflet/Leaflet.markercluster#customising-the-clustered-markers