vignette("maps") places pic drawings on the Earth and
draws them with ggplot2. This article puts them on an
interactive MapLibre map through mapgl, styled exactly as on rpic.dev’s Maps page: the engine
emits the MapLibre layers that draw a drawing — fills, solid and dashed
strokes, dots, text and shape titles — and every size in them is
true to the ground. Zoom in and a label one inch wide
keeps covering the same few hundred metres of the city; a
thick line stays as wide as the street it was drawn
over.
One call: add_rpic()
The callout from vignette("maps"): a dot on Recife’s
Marco Zero, a box a few hundred metres north-east of it and an arrow
between them, at 400 metres per inch.
library(rpic)
library(mapgl)
callout <- pic('
geo_lon = -34.8711
geo_lat = -8.0631
geo_scale = 400 # metres per inch
dot at (0, 0) rad 0.04
B: box "Marco Zero" "Recife" wid 1.3 ht 0.5 at (1.4, 0.9) fill 0.95
arrow from B.sw to (0.06, 0.06) thick 1.2
')
maplibre(style = carto_style("positron"), height = 400) |>
add_rpic(callout, before = "labels", text_scale = 1.5)add_rpic(map, x) takes a map from
maplibre() or mapboxgl() and the drawing — a
pic() or a string of pic source (passed to
rpic_geojson() with any further arguments), an
rpic_geojson() result or an rpic_sf() data
frame — and adds three things: a GeoJSON source "pic" with
the drawing, the six layers of rpic_maplibre_layers() and,
unless fit = FALSE, a fit_bounds() on the
drawing. Like mapgl’s add_*() verbs it takes the map first
and returns it, so mapgl’s own functions (controls, other layers,
legends) carry on in the same pipe. (rpic_mapgl(), its
earlier name, is the same function.)
Two arguments tune how the drawing sits on the basemap:
-
before = "labels"puts the drawing’s shapes — fills, strokes, dots — under the basemap’s labels: they are inserted at the start of the style’s trailing label block — before the first layer after its last fill or line layer, where the place and street names begin — so they sit over the roads and buildings while “Recife” and the street names stay readable over the box’s fill. (Not before the firstsymbollayer: positron drawswaterway_labelearly, under its roads and buildings.) The drawing’s own labels (pic-text,pic-title) still go on top, so a basemap label never covers “Marco Zero”. The style is read to find that layer: herecarto_style("positron")is a URL, fetched once per R session when the first map is built (offline, a warning says so and everything goes on top).beforealso takes a layer id (before = "water"), andNULL, the default, adds every layer on top — the rule of the engine’s JavaScriptattach(). -
text_scale = 1.5draws every label of the drawing half as large again. By default text is true to the ground like everything else — an 11 pt label covers the ground it would cover on the page at 400 metres per inch — which makes it small at city zooms; the scale keeps it ground-true (it still grows and shrinks with the map) but bigger. Strokes and dots keep their ground sizes.rpic_maplibre_layers(x, text_scale = 1.5)gives the same layers.
A third, font, needs no tuning here: by default
(font = "style") the drawing’s labels use the basemap’s own
font — positron’s Montserrat — so a label placed above a
line sits above it; with the glyph server’s Noto Sans
(font = "spec", the engine’s choice) its different vertical
metrics put “2 km” on the line.
What the helper does: rpic_sf() and
rpic_maplibre_layers()
The arrow between Recife and Olinda — two places the source uses but
does not define, supplied as features — and a geodesic 2 km
circle around Recife, now at one kilometre per inch:
two_cities <- pic('
geo_lon = -34.8711
geo_lat = -8.0631
geo_scale = 1000 # one inch is one kilometre
C: circle rad 2 at (0, 0) dashed fill 0.97
dot at C.c rad 0.04
line from C.c to C.e thin
"2 km" above at 0.5 <C.c, C.e>
arrow from C.c to Olinda thick 1.5 color "firebrick"
dot at Olinda rad 0.04
"Olinda" above at Olinda + (0, 0.08)
')
x <- two_cities |> rpic_sf(features = list(Olinda = c(-34.8553, -8.0089)))
layers <- x |> rpic_maplibre_layers(text_scale = 2)
vapply(layers, \(l) l$id, "")
#> [1] "pic-fill" "pic-stroke" "pic-stroke-dashed"
#> [4] "pic-dot" "pic-text" "pic-title"
layers[[2]]$paint$`line-width`[1:3]
#> [[1]]
#> [1] "interpolate"
#>
#> [[2]]
#> [[2]][[1]]
#> [1] "exponential"
#>
#> [[2]][[2]]
#> [1] 2
#>
#>
#> [[3]]
#> [[3]][[1]]
#> [1] "zoom"Each layer is a MapLibre style-spec object as an R list —
id, type, source,
filter, paint and sometimes
layout — whose expressions read the feature properties
under their GeoJSON names (stroke-width,
geopic:kind, …). Sizes are true to the ground: a label that
is 11 pt on the drawing becomes 11 pt × geo_scale metres on
the map, so at one kilometre per inch it looks small until you zoom in.
text_scale = 2 doubles every label without touching the
strokes.
The map itself is the one call; the layers above are what
add_rpic() adds for you, and before = "labels"
slides the shapes under the basemap’s place names:
maplibre(style = carto_style("positron"), height = 400) |>
add_rpic(x, before = "labels", text_scale = 2)Why add_rpic() and not add_source() +
add_layer() by hand? The layer expressions read the
engine’s property names, so the source must be the engine’s own GeoJSON
text (kept by rpic_sf() in its "geojson"
attribute), not the data frame: mapgl::add_source() would
re-serialise the sf with its R column names
(geopic_kind), absent values as null and
rounded coordinates. add_rpic() hands the text to the
widget verbatim and adds the six layers in order (the four geometry
layers before the style’s trailing label block when
before = "labels", the two text layers on top). The layers
depend only on the collection’s registration (its metres per inch and
latitude) and the font sizes it uses, so other mapgl layers —
choropleths, popups, a basemap of your own — can sit under or over
them.
The sizing formulas and the layer table are in the engine’s docs/geopic.md § 6.