Skip to contents

Clusters markers on a Leaflet map and visualizes them using customizable charts, such as pie or bar charts, showing counts by category. When using the "custom" type, a pie chart is rendered with aggregated data, employing methods like sum, min, max, mean, or median.

Usage

addClusterCharts(
  map,
  layerId = NULL,
  group = NULL,
  type = c("pie", "bar", "horizontal", "custom"),
  aggregation = c("sum", "min", "max", "mean", "median"),
  valueField = NULL,
  options = clusterchartOptions(),
  icon = NULL,
  html = NULL,
  popup = NULL,
  popupOptions = NULL,
  label = NULL,
  labelOptions = NULL,
  clusterOptions = NULL,
  clusterId = NULL,
  categoryField,
  categoryMap,
  popupFields = NULL,
  popupLabels = NULL,
  markerOptions = NULL,
  legendOptions = list(title = "", position = "topright"),
  data = getMapData(map)
)

Arguments

map

a map widget object created from leaflet()

layerId

the layer id

group

the name of the group the newly created layers should belong to (for clearGroup and addLayersControl purposes). Human-friendly group names are permitted–they need not be short, identifier-style names. Any number of layers and even different types of layers (e.g. markers and polygons) can share the same group name.

type

The type of chart to use for clusters: "pie", "bar", "horizontal", or "custom".

aggregation

Aggregation method for "custom" charts (e.g., sum, min, max, mean, median).

valueField

Column name with values to aggregate for "custom" charts.

options

Additional options for cluster charts (see clusterchartOptions).

icon

An icon or set of icons to include, created with makeIcon or iconList.

html

The column name containing the HTML content to include in the markers.

popup

The column name used to retrieve feature properties for the popup.

popupOptions

A Vector of popupOptions to provide popups

label

a character vector of the HTML content for the labels

labelOptions

A Vector of labelOptions to provide label options for each label. Default NULL

clusterOptions

if not NULL, markers will be clustered using Leaflet.markercluster; you can use markerClusterOptions() to specify marker cluster options

clusterId

the id for the marker cluster layer

categoryField

Column name for categorizing charts.

categoryMap

A data.frame mapping categories to chart properties (e.g., label, color, icons, stroke).

popupFields

A string or vector of strings indicating the column names to include in popups.

popupLabels

A string or vector of strings indicating the labels for the popup fields.

markerOptions

Additional options for markers (see markerOptions::markerOptions()).

legendOptions

A list of options for the legend, including the title and position.

data

the data object from which the argument values are derived; by default, it is the data object provided to leaflet() initially, but can be overridden

Details

The `clusterCharts` use Leaflet's `L.DivIcon`, allowing you to fully customize the styling of individual markers and clusters using CSS. Each individual marker within a cluster is assigned the CSS class `clustermarker`, while the entire cluster is assigned the class `clustermarker-cluster`. You can modify the appearance of these elements by targeting these classes in your custom CSS.

See also

Other clusterCharts: clusterchartOptions()

Examples

# Example usage:
library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
library(leaflet)
library(leaflet.extras2)

data <- sf::st_as_sf(breweries91)
categories <- c("Schwer", "Mäßig", "Leicht", "kein Schaden")
data$category <- sample(categories, size = nrow(data), replace = TRUE)

## Pie Chart
leaflet() %>%
  addProviderTiles("CartoDB.Positron") %>%
  leaflet::addLayersControl(overlayGroups = "clustermarkers") %>%
  addClusterCharts(
    data = data,
    categoryField = "category",
    categoryMap = data.frame(
      labels = categories,
      colors = c("#F88", "#FA0", "#FF3", "#BFB"),
      strokes = "gray"
    ),
    group = "clustermarkers",
    popupFields = c("brewery", "address", "zipcode", "category"),
    popupLabels = c("Brauerei", "Adresse", "PLZ", "Art"),
    label = "brewery"
  )
## Bar Chart leaflet() %>% addProviderTiles("CartoDB.Positron") %>% leaflet::addLayersControl(overlayGroups = "clustermarkers") %>% addClusterCharts( data = data, type = "bar", categoryField = "category", categoryMap = data.frame( labels = categories, colors = c("#F88", "#FA0", "#FF3", "#BFB"), strokes = "gray" ), group = "clustermarkers", popupFields = c("brewery", "address", "zipcode", "category"), popupLabels = c("Brauerei", "Adresse", "PLZ", "Art"), label = "brewery" )
## Custom Pie Chart with "mean" aggregation on column "value" data <- sf::st_as_sf(breweries91) categories <- c("Schwer", "Mäßig", "Leicht", "kein Schaden") data$category <- sample(categories, size = nrow(data), replace = TRUE) data$value <- round(runif(nrow(data), 0, 100), 0) leaflet() %>% addProviderTiles("CartoDB.Positron") %>% leaflet::addLayersControl(overlayGroups = "clustermarkers") %>% addClusterCharts( data = data, type = "custom", valueField = "value", aggregation = "mean", categoryField = "category", categoryMap = data.frame( labels = categories, colors = c("#F88", "#FA0", "#FF3", "#BFB"), strokes = "gray" ), options = clusterchartOptions(rmax = 50, digits = 0, innerRadius = 20), group = "clustermarkers", popupFields = c("brewery", "address", "zipcode", "category", "value"), popupLabels = c("Brauerei", "Adresse", "PLZ", "Art", "Value"), label = "brewery" )
## For Shiny examples, please run: # runApp(system.file("examples/clusterCharts_app.R", package = "leaflet.extras2")) # runApp(system.file("examples/clustercharts_sum.R", package = "leaflet.extras2"))