Skip to contents

This geom is used to visualise SpatVector objects (see terra::vect()). For simple plots, you will only need geom_spatvector(). For text and labels, you can use geom_spatvector_text() and geom_spatvector_label().

The underlying implementation is based on ggplot2::geom_sf().

Usage

geom_spatvector(mapping = aes(), data, na.rm = FALSE, show.legend = NA, ...)

geom_spatvector_label(
  mapping = aes(),
  data,
  na.rm = FALSE,
  show.legend = NA,
  ...
)

geom_spatvector_text(
  mapping = aes(),
  data,
  na.rm = FALSE,
  show.legend = NA,
  ...
)

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

A SpatVector object, see terra::vect().

na.rm

If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed.

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes.

You can also set this to one of "polygon", "line", and "point" to override the default legend.

...

Other arguments passed on to ggplot2::geom_sf(). These are often aesthetics, used to set an aesthetic to a fixed value, like colour = "red" or size = 3.

Value

A ggplot2 layer

Details

See ggplot2::geom_sf() for details on aesthetics, etc.

terra equivalent

terra::plot()

Examples

# \donttest{
# Create a SpatVector

extfile <- system.file("extdata/cyl.gpkg", package = "tidyterra")

cyl <- terra::vect(extfile)
class(cyl)
#> [1] "SpatVector"
#> attr(,"package")
#> [1] "terra"
#' # Create a SpatVector

extfile <- system.file("extdata/cyl.gpkg", package = "tidyterra")

cyl <- terra::vect(extfile)
class(cyl)
#> [1] "SpatVector"
#> attr(,"package")
#> [1] "terra"

library(ggplot2)

ggplot() +
  geom_spatvector(data = cyl)


# Avoid this!
if (FALSE) {
  # Would produce an error

  ggplot(cyl) +
    geom_spatvector()
}
# With params

ggplot() +
  geom_spatvector(data = cyl, aes(fill = name), color = NA) +
  scale_fill_viridis_d() +
  coord_sf(crs = 3857)


# Add labels
ggplot() +
  geom_spatvector(data = cyl, aes(fill = name), color = NA) +
  geom_spatvector_text(
    data = cyl, aes(label = iso2), fontface = "bold", size = 3,
    color = "red"
  ) +
  scale_fill_viridis_d(alpha = 0.4) +
  coord_sf(crs = 3857)

# }