The viz_text function adds a text on a geoviz map
and can also generate labels from a spatial dataframe.
Usage
viz_text(
map,
id = NULL,
data = NULL,
text = "text",
textAnchor = NULL,
dominantBaseline = NULL,
fontFamily = NULL,
fontSize = 12,
lineSpacing = 0,
pos = c(0, 0),
dx = 0,
dy = 0,
sort = NULL,
descending = FALSE,
coords = "geo",
fill = NULL,
stroke = NULL,
strokeWidth = 1,
strokeLinejoin = "round",
...
)Arguments
- map
A
geovizmap created withviz_create.- id
character. Optional. Unique layer id.
- data
a spatial dataframe.
- text
character or function. Optional. Text to display (default "text").
- textAnchor
character or function. Optional. Text anchor ("start","middle","end").
- dominantBaseline
character or function. Optional. Baseline alignment ("auto","middle","central","hanging").
- fontFamily
character. Optional. Font family from SVG container.
- fontSize
numeric. Optional. Font size (default 12).
- lineSpacing
numeric. Optional. Line spacing (default 0).
- pos
numeric vector of length 2. Optional. Position for single text element (default c(0,0)).
- dx
numeric. Optional. X shift (default 0).
- dy
numeric. Optional. Y shift (default 0).
- sort
character or function. Optional. Field or function used to sort labels.
- descending
logical. Optional. Sorting order (default FALSE).
- coords
character. Optional. Coordinate system ("geo" or "svg") (default "geo").
- fill
character or function. Optional. Fill color.
- stroke
character or function. Optional. Stroke color.
- strokeWidth
numeric. Optional. Stroke width (default 1).
- strokeLinejoin
character or function. Optional. Stroke line join (default "round").
- ...
Additional SVG attributes applied to text elements.
Examples
library(sf)
world <- st_read(
system.file("gpkg/world.gpkg", package = "geoviz"),
quiet = TRUE
)
# Example 1
viz_create(projection = "EqualEarth", width = 750, background = "white") |>
viz_path(datum = world, fill = "#f1f3f5") |>
viz_text(
pos = c(100, 200), coords = "svg",
text = "All maps are lies,\nbut some are useful lies",
fill = "#38896F"
) |>
viz_render()
# Example 2
viz_create(projection = "EqualEarth", background = "white") |>
viz_path(datum = world, fill = "#f1f3f5") |>
viz_text(data = world, text = "ISO3", fill = "#38896F") |>
viz_render()