text.sf | R Documentation |
Plot labels from a sf object on a base plot.
## S3 method for class 'sf' text(x, ...)
x |
sf object |
... |
Arguments passed on to |
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.
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.