Skip to contents

Add a layer to a map or map proxy

Usage

add_layer(
  map,
  id,
  type = "fill",
  source,
  paint = NULL,
  layout = NULL,
  popup_column = NULL,
  hover_column = NULL,
  can_cluster = FALSE,
  under_id = NULL,
  filter = NULL,
  ...
)

Arguments

map

The map object or map proxy to which the layer will be added.

id

A unique identifier for the layer.

type

The type of layer to add (e.g., "fill", "circle", "line"). Default is "fill".

source

The data source for the layer, if not a GeoJSON, it will be converted.

paint

A list of paint options for styling the layer. See get_paint_options() for defaults and options.

layout

A list of layout options for the layer. See get_layout_options() for defaults and options.

popup_column

The column name to use for popups. Default is NULL.

hover_column

The column name to use for hover effects. Default is NULL.

can_cluster

Whether the layer can be clustered. Default is FALSE.

under_id

The ID of an layer already on the map to place this layer under. Default is NULL.

filter

A filter expression to apply to the layer. Default is NULL. See get_layer_filter() for more details on how to create filter expressions.

...

Additional arguments to include in the layer definition.

  • clusterOptions: A list of options for clustering, if can_cluster is TRUE. See the cluster vignette for details on available options.

Value

The updated map object with the new layer added.

Note

If source is not a string referring to an existing source, it will be converted to a GeoJSON source and added to the map automatically. The Id for the source is generated by appending the layer Id to source-. For example, if you add a layer with id = "my_layer" and source is a sf object, the source is added to the map with the ID "source-my_layer".

See also

get_paint_options() for paint customisation, get_layout_options() for layout customisation, and get_layer_filter() for applying filters to layers.

Examples

# Load libraries
library(spData)
library(dplyr)
library(sf)

nz_data <- spData::nz |>
  dplyr::rename(geometry = geom) |>
  sf::st_transform(4326)

map() |>
 add_layer(
   id = "nz_regions",
   type = "fill",
   source = nz_data,
   hover_column = "Name"
 )