mark_sf_label() draws one text label per sf feature, placed at the
feature's interior point (sf::st_point_on_surface(), guaranteed to fall
inside each polygon — unlike a centroid, which can land in a bay or a hole).
The label points are reprojected through the same coord_sf CRS as
mark_sf(), so they always land where the geometry is drawn. Layer it over a
mark_sf() choropleth to name the regions; repel = TRUE (the default) pushes
crowded labels apart.
Usage
mark_sf_label(
plot,
...,
position = "identity",
size = NULL,
repel = TRUE,
box_padding = 1,
min_segment_length = 2,
blend = NULL,
data = NULL
)Arguments
- plot
- ...
Encodings (tidy-eval): the
labelto draw at each feature, plus an optionalcolor/size. The position comes from the geometry, sox/yare not mapped.- position
Position adjustment: a string —
"identity"(default),"nudge","stack","fill","dodge","dodge2","jitter","jitterdodge","sina"— or a parameterised position object such asposition_dodge()orposition_stack(). Every mark takes"identity"and"nudge"."stack"/"fill"work on bars, areas, points, text and labels (so a label can sit on its stacked bar);"dodge"/"dodge2"on bars, points, text, labels, error bars, line/point ranges, cross-bars, box plots, violins and slab-intervals (so error bars dodge over dodged bars);"jitter"on points, text and labels;"jitterdodge"and"sina"on points. A position a mark does not support is an error naming the ones it does.- size
Font size in points.
- repel
Move overlapping labels apart (force-directed, ggrepel-style), with leader lines to the points? Single cartesian panel only.
- box_padding
Extra space (mm) kept around each label box during repulsion.
- min_segment_length
Shortest leader line (mm) worth drawing; a label that barely moved gets none.
- blend
Optional blend mode for compositing this layer against what is already drawn beneath it (the panel and earlier layers), one of the CSS
mix-blend-modenames, e.g."multiply","screen","darken". The whole layer composites as one isolated group (not per element).- data
An
sfdata frame (defaults to the plot's). Its geometry supplies the label positions.
Value
The modified PlotSpec.
Examples
if (requireNamespace("sf", quietly = TRUE)) {
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
vplot(nc) |>
mark_sf(fill = AREA) |>
mark_sf_label(label = NAME, size = 5)
}
