dkae
dkae

Reputation: 313

Leaflet cluster marker spacing, or how to have smaller groups when zoomed out

Making a leaflet map with clustered markers. Is there a parameter in markerClusterOptions() that controls the number of groups based on the zoom level? For example, at default zoom my map shows just 3 clusters for 1800 observations. However it would be nice if there were at least twice as many clusters at default zoom.

I don't want to use freezeAtZoom because then users won't be able to zoom in and see each individual observation.

Data procured from: https://data.sccgov.org/Public-Safety/Crime-Reports/n9u6-aijz/data

library(tidyverse);library(leaflet);library(lubridate)

crime_reports <- read_csv("Crime_Reports.csv")
car_burglaries <- crime_reports %>% filter(incident_type_primary=="VEHICLE BURGLARY") %>%
  mutate(datetime=mdy_hms(incident_datetime)) %>% 
  arrange(datetime)

leaflet(car_burglaries) %>% addTiles() %>% 
  addMarkers(
    popup = ~paste(as.character(incident_datetime),",","Case number",as.character(case_number)), 
    label = ~as.character(address_1),
    clusterOptions = markerClusterOptions()
)

Upvotes: 12

Views: 5688

Answers (1)

camille
camille

Reputation: 16842

The R leaflet package docs mention that clustering is done via a plugin for the Leaflet javascript library called Leaflet.markercluster, documented here. Many functions in the R leaflet package mention that additional arguments in ... will be passed to the underlying javascript libraries, but to use these, it's helpful to know your way around the javascript docs. For clustering, you were right to notice that markerClusterOptions will pass on more advanced options.

The markercluster docs say there's an argument maxClusterRadius:

The maximum radius that a cluster will cover from the central marker (in pixels). Default 80. Decreasing will make more, smaller clusters. You can also use a function that accepts the current map zoom and returns the maximum cluster radius in pixels.

So I messed around with a couple options. Screenshots from my RStudio View pane:

The default radius of 80 pixels:

leaflet(car_burglaries) %>% 
  addTiles() %>% 
  addMarkers(
    popup = ~paste(as.character(incident_datetime),",","Case number",as.character(case_number)), 
    label = ~as.character(address_1),
    clusterOptions = markerClusterOptions()
  )

default radius

Smaller radius = more clusters:

leaflet(car_burglaries) %>% 
  addTiles() %>% 
  addMarkers(
    popup = ~paste(as.character(incident_datetime),",","Case number",as.character(case_number)), 
    label = ~as.character(address_1),
    clusterOptions = markerClusterOptions(maxClusterRadius = 50)
  )

smaller radius

Larger radius = fewer clusters:

leaflet(car_burglaries) %>% 
  addTiles() %>% 
  addMarkers(
    popup = ~paste(as.character(incident_datetime),",","Case number",as.character(case_number)), 
    label = ~as.character(address_1),
    clusterOptions = markerClusterOptions(maxClusterRadius = 200)
  )

bigger radius

If you have the default setting of showing the coverage area when hovering over a marker, it should help with figuring out the radius you want.

Worth noting that the radius is measured in pixels as the map is currently being displayed. That means that the number of clusters shown will depend on how big the map is, in pixels. If you need something more complicated, like to set the radius to scale with window size so users can resize the window while keeping the number of clusters approximately constant, you could write a function to pass that radius in as a variable, or possibly write it in javascript and pass it in somehow. I'll leave that for another post.

Upvotes: 15

Related Questions