library(leaflet)

Add a Level factor to quakes

quakes <- quakes %>%
  dplyr::mutate(mag.level = cut(mag,c(3,4,5,6),
                                labels = c('>3 & <=4', '>4 & <=5', '>5 & <=6')))

l <- leaflet() %>% addTiles()



Default Clustering

l %>%
  addMarkers(data=quakes, clusterOptions = markerClusterOptions())
## Assuming 'long' and 'lat' are longitude and latitude, respectively



Clustering Frozen at level 5

l %>%
  addMarkers(data=quakes, clusterOptions = markerClusterOptions(freezeAtZoom=6))
## Assuming 'long' and 'lat' are longitude and latitude, respectively



Clustering of Label Only Clusters

l %>%
  addLabelOnlyMarkers(data=quakes,
                      lng=~long, lat=~lat,
                      label=~as.character(mag),
                      clusterOptions = markerClusterOptions(),
                      labelOptions = labelOptions(noHide = T,
                                                  direction = 'auto'))



Clustering + Layers

quakes.df <- split(quakes, quakes$mag.level)

l2 <- l
names(quakes.df) %>%
  purrr::walk( function(df) {
    l2 <<- l2 %>%
      addMarkers(data=quakes.df[[df]],
                          lng=~long, lat=~lat,
                          label=~as.character(mag),
                          popup=~as.character(mag),
                          group = df,
                          clusterOptions = markerClusterOptions(removeOutsideVisibleBounds = F),
                          labelOptions = labelOptions(noHide = T,
                                                       direction = 'auto'))
  })

l2 %>%
  addLayersControl(
    overlayGroups = names(quakes.df),
    options = layersControlOptions(collapsed = FALSE)
  )



Clustering with custom iconCreateFunction

leaflet(quakes) %>% addTiles() %>%
  addMarkers(clusterOptions =
               markerClusterOptions(iconCreateFunction =
                                      JS("
                                          function(cluster) {
                                             return new L.DivIcon({
                                               html: '<div style=\"background-color:rgba(77,77,77,0.5)\"><span>' + cluster.getChildCount() + '</div><span>',
                                               className: 'marker-cluster'
                                             });
                                           }")))
## Assuming 'long' and 'lat' are longitude and latitude, respectively

The "leaflet" R package is copyright © 2014-2016 RStudio, Inc.
The Leaflet JavaScript library is © 2010–2016 Vladimir Agafonkin, 2010–2011 CloudMade.
Maps © OpenStreetMap contributors unless otherwise noted.

Fork me on GitHub