Provides label and text geoms that automatically retrieve the sf object's coordinates.

StatSfCoordinates

geom_sf_label(mapping = NULL, data = NULL,
  fun.geometry = sf::st_point_on_surface, ...)

geom_sf_text(mapping = NULL, data = NULL,
  fun.geometry = sf::st_point_on_surface, ...)

geom_sf_label_repel(mapping = NULL, data = NULL,
  fun.geometry = sf::st_point_on_surface, ...)

geom_sf_text_repel(mapping = NULL, data = NULL,
  fun.geometry = sf::st_point_on_surface, ...)

Arguments

mapping

Set of aesthetic mappings created by aes() or aes_(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.

data

The data to be displayed in this layer. There are three options:

If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot().

A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created.

A function will be called with a single argument, the plot data. The return value must be a data.frame., and will be used as the layer data.

fun.geometry

A function that takes a sfc object and returns a sfc_POINT with the same length as the input (e.g. st_point_on_surface).

...

Other arguments passed to the underlying function.

Format

An object of class StatSfCoordinates (inherits from Stat, ggproto, gg) of length 4.

Details

These functions are thin wrappers of usual geoms like geom_label(), the only difference is that they use StatSfCoordinates for stat. More precisely:

Computed variables

Depending on the type of given sfc object, some variables between X, Y, Z and M are available. Please read Esamples section how to use these.

Examples

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) # st_point_on_surface may not give correct results for longitude/latitude data nc_3857 <- sf::st_transform(nc, 3857) # geom_label() for sf ggplot(nc_3857[1:3, ]) + geom_sf(aes(fill = AREA)) + geom_sf_label(aes(label = NAME))
# ggrepel::geom_label_repel() for sf ggplot(nc_3857[1:3, ]) + geom_sf(aes(fill = AREA)) + geom_sf_label_repel( aes(label = NAME), # additional parameters are passed to geom_label_repel() nudge_x = -5, nudge_y = -5, seed = 10 )
# Of course, you can use StatSfCoordinates with any geoms. ggplot(nc_3857[1:3, ]) + geom_sf(aes(fill = AREA)) + geom_point(aes(geometry = geometry,stat(X), stat(Y)), stat = StatSfCoordinates, fun.geometry = sf::st_centroid, colour = "white", size = 10)