text.sf: Label (text) simple features plots

View source: R/plot.R

text.sfR Documentation

Label (text) simple features plots

Description

Plot labels from a sf object on a base plot.

Usage

## S3 method for class 'sf'
text(x, ...)

Arguments

x

sf object

...

Arguments passed on to graphics::text

Details

The first argument is an sf object and the 'labels' argument may be set manually, or if unset will be taken as the first field (column) of data in the object. If there is no fields, the integer index '1:nrow' will be used.

Use '...' to control arguments of [text].

To control which field is used to label, the easiest is to drop all columns but the first or to select them with the one you want first in the usual ways of dplyr and so on.

Labels are positioned with the centroid [sf::st_centroid] of the feature object (point, line, or area).

There are many kinds of centroids, and many more ways of labelling things ... a short list of possibilities that might make there way into this facility: https://github.com/mdsumner/basf/issues/1#issuecomment-1030427651.

Examples

x <- read_sf(system.file("shape/nc.shp", package="sf"))
plot(x)
text(x)  ## we get AREA
plot(x)
text(x["NAME"]) ## we want NAME
plot(x)
text(x, label = x$NAME)  # another way
ix <- sample(1:nrow(x), 8)
plot(x[ix, ])
plot(st_centroid(x[ix, ]), add = TRUE, pch = "+", cex = 0.8)
text(x[ix, 5], cex = 1.5, col = hcl.colors(8),  pos = 1:4, font = 2:5)

mdsumner/basf documentation built on Sept. 11, 2022, 1:08 p.m.