Add a symbol layer to a map
add_symbol_layer(
map,
id,
source,
source_layer = NULL,
icon_allow_overlap = NULL,
icon_anchor = NULL,
icon_color = NULL,
icon_color_brightness_max = NULL,
icon_color_brightness_min = NULL,
icon_color_contrast = NULL,
icon_color_saturation = NULL,
icon_emissive_strength = NULL,
icon_halo_blur = NULL,
icon_halo_color = NULL,
icon_halo_width = NULL,
icon_ignore_placement = NULL,
icon_image = NULL,
icon_image_cross_fade = NULL,
icon_keep_upright = NULL,
icon_offset = NULL,
icon_opacity = NULL,
icon_optional = NULL,
icon_padding = NULL,
icon_pitch_alignment = NULL,
icon_rotate = NULL,
icon_rotation_alignment = NULL,
icon_size = NULL,
icon_text_fit = NULL,
icon_text_fit_padding = NULL,
icon_translate = NULL,
icon_translate_anchor = NULL,
symbol_avoid_edges = NULL,
symbol_placement = NULL,
symbol_sort_key = NULL,
symbol_spacing = NULL,
symbol_z_elevate = NULL,
symbol_z_offset = NULL,
symbol_z_order = NULL,
text_allow_overlap = NULL,
text_anchor = NULL,
text_color = "black",
text_emissive_strength = NULL,
text_field = NULL,
text_font = NULL,
text_halo_blur = NULL,
text_halo_color = NULL,
text_halo_width = NULL,
text_ignore_placement = NULL,
text_justify = NULL,
text_keep_upright = NULL,
text_letter_spacing = NULL,
text_line_height = NULL,
text_max_angle = NULL,
text_max_width = NULL,
text_offset = NULL,
text_opacity = NULL,
text_optional = NULL,
text_padding = NULL,
text_pitch_alignment = NULL,
text_radial_offset = NULL,
text_rotate = NULL,
text_rotation_alignment = NULL,
text_size = NULL,
text_transform = NULL,
text_translate = NULL,
text_translate_anchor = NULL,
text_variable_anchor = NULL,
text_writing_mode = NULL,
visibility = "visible",
slot = NULL,
min_zoom = NULL,
max_zoom = NULL,
popup = NULL,
tooltip = NULL,
hover_options = NULL,
before_id = NULL,
filter = NULL,
cluster_options = NULL
)
The modified map object with the new symbol layer added.
A map object created by the mapboxgl
or maplibre
functions.
A unique ID for the layer.
The ID of the source, alternatively an sf object (which will be converted to a GeoJSON source) or a named list that specifies type
and url
for a remote source.
The source layer (for vector sources).
If TRUE, the icon will be visible even if it collides with other previously drawn symbols.
Part of the icon placed closest to the anchor.
The color of the icon. This is not supported for many Mapbox icons; read more at https://docs.mapbox.com/help/troubleshooting/using-recolorable-images-in-mapbox-maps/.
The maximum brightness of the icon color.
The minimum brightness of the icon color.
The contrast of the icon color.
The saturation of the icon color.
The strength of the icon's emissive color.
The blur applied to the icon's halo.
The color of the icon's halo.
The width of the icon's halo.
If TRUE, the icon will be visible even if it collides with other symbols.
Name of image in sprite to use for drawing an image background.
To use values in a column of your input dataset, use get_column('YOUR_ICON_COLUMN_NAME')
.
Images can also be loaded with the add_image()
function which should precede the add_symbol_layer()
function.
The cross-fade parameter for the icon image.
If TRUE, the icon will be kept upright.
Offset distance of icon.
The opacity at which the icon will be drawn.
If TRUE, the icon will be optional.
Padding around the icon.
Alignment of the icon with respect to the pitch of the map.
Rotates the icon clockwise.
Alignment of the icon with respect to the map.
The size of the icon, specified relative to the original size of the image. For example, a value of 5 would make the icon 5 times larger than the original size, whereas a value of 0.5 would make the icon half the size of the original.
Scales the text to fit the icon.
Padding for text fitting the icon.
The offset distance of the icon.
Controls the frame of reference for icon-translate
.
If TRUE, the symbol will be avoided when near the edges.
Placement of the symbol on the map.
Sorts features in ascending order based on this value.
Spacing between symbols.
If TRUE
, positions the symbol on top of a fill-extrusion
layer.
Requires symbol_placement
to be set to "point"
and symbol-z-order
to be set to "auto"
.
The elevation of the symbol, in meters. Use get_column()
to get elevations from a column in the dataset.
Orders the symbol z-axis.
If TRUE, the text will be visible even if it collides with other previously drawn symbols.
Part of the text placed closest to the anchor.
The color of the text.
The strength of the text's emissive color.
Value to use for a text label.
Font stack to use for displaying text.
The blur applied to the text's halo.
The color of the text's halo.
The width of the text's halo.
If TRUE, the text will be visible even if it collides with other symbols.
The justification of the text.
If TRUE, the text will be kept upright.
Spacing between text letters.
Height of the text lines.
Maximum angle of the text.
Maximum width of the text.
Offset distance of text.
The opacity at which the text will be drawn.
If TRUE, the text will be optional.
Padding around the text.
Alignment of the text with respect to the pitch of the map.
Radial offset of the text.
Rotates the text clockwise.
Alignment of the text with respect to the map.
The size of the text.
Transform applied to the text.
The offset distance of the text.
Controls the frame of reference for text-translate
.
Variable anchor for the text.
Writing mode for the text.
Whether this layer is displayed.
An optional slot for layer order.
The minimum zoom level for the layer.
The maximum zoom level for the layer.
A column name containing information to display in a popup on click. Columns containing HTML will be parsed.
A column name containing information to display in a tooltip on hover. Columns containing HTML will be parsed.
A named list of options for highlighting features in the layer on hover. Not all elements of SVG icons can be styled.
The name of the layer that this layer appears "before", allowing you to insert layers below other layers in your basemap (e.g. labels).
An optional filter expression to subset features in the layer.
A list of options for clustering symbols, created by the cluster_options()
function.
if (FALSE) {
library(mapgl)
library(sf)
library(dplyr)
# Set seed for reproducibility
set.seed(1234)
# Define the bounding box for Washington DC (approximately)
bbox <- st_bbox(
c(
xmin = -77.119759,
ymin = 38.791645,
xmax = -76.909393,
ymax = 38.995548
),
crs = st_crs(4326)
)
# Generate 30 random points within the bounding box
random_points <- st_as_sf(
data.frame(
id = 1:30,
lon = runif(30, bbox["xmin"], bbox["xmax"]),
lat = runif(30, bbox["ymin"], bbox["ymax"])
),
coords = c("lon", "lat"),
crs = 4326
)
# Assign random icons
icons <- c("music", "bar", "theatre", "bicycle")
random_points <- random_points |>
mutate(icon = sample(icons, n(), replace = TRUE))
# Map with icons
mapboxgl(style = mapbox_style("light")) |>
fit_bounds(random_points, animate = FALSE) |>
add_symbol_layer(
id = "points-of-interest",
source = random_points,
icon_image = c("get", "icon"),
icon_allow_overlap = TRUE,
tooltip = "icon"
)
}
Run the code above in your browser using DataLab