geom_textsf {geomtextpath} | R Documentation |
Visualise sf objects with labels
Description
This set of geom, stat, and coord are used to visualise simple feature (sf)
objects. For simple plots, you will only need geom_sf()
as it
uses stat_sf()
and adds coord_sf()
for you. geom_textsf()
is
an unusual geom because it will draw different geometric objects depending
on what simple features are present in the data: you can get points, lines,
or polygons.
Usage
geom_textsf(
mapping = aes(),
data = NULL,
stat = "sf",
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...
)
geom_labelsf(
mapping = aes(),
data = NULL,
stat = "sf",
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this
layer, either as a |
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
You can also set this to one of "polygon", "line", and "point" to override the default legend. |
inherit.aes |
If |
... |
Arguments passed on to
|
Value
A Layer
ggproto object that can be added to a plot.
Geometry aesthetic
geom_textsf()
uses a unique aesthetic: geometry
, giving an
column of class sfc
containing simple features data. There
are three ways to supply the geometry
aesthetic:
Do nothing: by default
geom_textsf()
assumes it is stored in thegeometry
column.Explicitly pass an
sf
object to thedata
argument. This will use the primary geometry column, no matter what it's called.Supply your own using
aes(geometry = my_column)
Unlike other aesthetics, geometry
will never be inherited from
the plot.
CRS
coord_sf()
ensures that all layers use a common CRS. You can
either specify it using the crs
param, or coord_sf()
will
take it from the first layer that defines a CRS.
Combining sf layers and regular geoms
Most regular geoms, such as geom_point()
, geom_path()
,
geom_text()
, geom_polygon()
etc. will work fine with coord_sf()
. However
when using these geoms, two problems arise. First, what CRS should be used
for the x and y coordinates used by these non-sf geoms? The CRS applied to
non-sf geoms is set by the default_crs
parameter, and it defaults to
NULL
, which means positions for non-sf geoms are interpreted as projected
coordinates in the coordinate system set by the crs
parameter. This setting
allows you complete control over where exactly items are placed on the plot
canvas, but it may require some understanding of how projections work and how
to generate data in projected coordinates. As an alternative, you can set
default_crs = sf::st_crs(4326)
, the World Geodetic System 1984 (WGS84).
This means that x and y positions are interpreted as longitude and latitude,
respectively. You can also specify any other valid CRS as the default CRS for
non-sf geoms.
The second problem that arises for non-sf geoms is how straight lines
should be interpreted in projected space when default_crs
is not set to NULL
.
The approach coord_sf()
takes is to break straight lines into small pieces
(i.e., segmentize them) and then transform the pieces into projected coordinates.
For the default setting where x and y are interpreted as longitude and latitude,
this approach means that horizontal lines follow the parallels and vertical lines
follow the meridians. If you need a different approach to handling straight lines,
then you should manually segmentize and project coordinates and generate the plot
in projected coordinates.
See Also
stat_sf_coordinates()
.
Other geom layers that place text on paths.
Examples
ggplot(waterways) +
geom_textsf(label = "Forth and Clyde Canal",
hjust = 0.62, vjust = -0.3, fill = "#E4E0A3") +
lims(x = c(-4.2, -3.9), y = c(55.9, 56))