
Add Visual Parameters to a Spatial Layer
add_visual_param.RdThis function enhances a spatial layer (either sf or sfc object) with
visual parameters, typically colors or other attributes, using a merge
operation (sf) or direct attribute addition (sfc).
Arguments
- layer
spatial object, either an
sfobject or ansfcobject. This is the layer to which visual parameters will be added.- visual_param
data.framecontaining the visual parameters to add. This should have a common identifier with thelayerobject for merging.- junction
character vector specifying the column name(s) to use for merging the
layerandvisual_paramobjects. Forsfobjects, this defines the join key. If missing forsfobjects, an error is thrown.
Details
For sf objects, the function merges the layer and visual_param
data frames based on the specified junction column. If a single junction
is provided, a standard merge is performed. If two junction values are
provided, the merge is done using by.x and by.y. If more than two
junction values are provided, the function stops with an error.
For sfc objects, the visual parameters are directly added as new attributes
to the sfc object.
See also
Other interactive mapping:
add_leaflet_layer(),
map_leaflet_commune(),
map_leaflet_multilayer(),
set_provider()
Examples
enhanced_sf = add_visual_param(
layer = so_ii_hydro,
visual_param = data.frame(
id = unique(so.ii::so_ii_hydro[["type"]]),
color = scales::alpha(c("blue", "red", "dodgerblue"), 0.5),
fill = scales::alpha(c("blue", "red", "dodgerblue"), 0.5),
opacity = 0.5
),
junction = c("type", "id")
)
enhanced_sf = add_visual_param(
layer = so_ii_hydro,
visual_param = data.frame(
type = unique(so.ii::so_ii_hydro[["type"]]),
color = scales::alpha(c("blue", "red", "dodgerblue"), 0.5),
fill = scales::alpha(c("blue", "red", "dodgerblue"), 0.5),
opacity = 0.8
),
junction = "type"
)
# For sfc objects
enhanced_sfc = add_visual_param(
layer = so_ii_limit,
visual_param = data.frame(
id = "perimeter",
label = "perimeter",
color = "white",
fill = "transparent",
opacity = 1
)
)