Learn R Programming

tidyterra (version 1.0.0)

tidy.Spat: Turn Spat* object into a tidy tibble

Description

Turn Spat* object into a tidy tibble. This is similar to fortify.Spat, and it is provided just in case ggplot2::fortify() method is deprecated in the future.

Usage

# S3 method for SpatRaster
tidy(
  x,
  ...,
  .name_repair = "unique",
  maxcell = terra::ncell(x) * 1.1,
  pivot = FALSE
)

# S3 method for SpatVector tidy(x, ...)

# S3 method for SpatGraticule tidy(x, ...)

# S3 method for SpatExtent tidy(x, ..., crs = "")

Value

tidy.SpatVector(), tidy.SpatGraticule() and tidy.SpatExtent()

return a sf object.

tidy.SpatRaster() returns a tibble. See Methods.

Arguments

x

A SpatRaster created with terra::rast() or a SpatVector created with terra::vect(). Also support SpatGraticule (see terra::graticule()) and SpatExtent (see terra::ext()).

...

Ignored by these methods.

.name_repair

Treatment of problematic column names:

  • "minimal": No name repair or checks, beyond basic existence.

  • "unique": Make sure names are unique and not empty.

  • "check_unique": (default value), no name repair, but check they are unique.

  • "universal": Make the names unique and syntactic.

  • a function: apply custom name repair (e.g., .name_repair = make.names for names in the style of base R).

  • A purrr-style anonymous function, see rlang::as_function().

maxcell

positive integer. Maximum number of cells to use for the plot.

pivot

Logical. When TRUE the SpatRaster would be provided on long format. When FALSE (the default) it would be provided as a data frame with a column for each layer. See Details.

crs

Input potentially including or representing a CRS. It could be a sf/sfc object, a SpatRaster/SpatVector object, a crs object from sf::st_crs(), a character (for example a proj4 string) or a integer (representing an EPSG code).

Methods

Implementation of the generic generics::tidy() method.

SpatRaster

Return a tibble than can be used with ggplot2::geom_* like ggplot2::geom_point(), ggplot2::geom_raster(), etc.

The resulting tibble includes the coordinates on the columns x, y. The values of each layer are included as additional columns named as per the name of the layer on the SpatRaster.

The CRS of the SpatRaster can be retrieved with attr(tidySpatRaster, "crs").

It is possible to convert the tidy object onto a SpatRaster again with as_spatraster().

When pivot = TRUE the SpatRaster is provided in a "long" format (see tidyr::pivot_longer()). The tidy object would have the following columns:

  • x,y: Coordinates (center) of the cell on the corresponding CRS.

  • lyr: Indicating the name of the SpatRaster layer of value.

  • value: The value of the SpatRaster in the corresponding lyr.

This option may be useful when using several geom_* and for faceting.

SpatVector, SpatGraticule and SpatExtent

Return a sf object than can be used with ggplot2::geom_sf().

See Also

sf::st_as_sf(), as_tibble.Spat, as_spatraster(), fortify.Spat, generics::tidy().

Other generics methods: glance.Spat, required_pkgs.Spat

Coercing objects: as_coordinates(), as_sf(), as_spatraster(), as_spatvector(), as_tibble.Spat, fortify.Spat

Examples

Run this code
# \donttest{

# Get a SpatRaster
r <- system.file("extdata/volcano2.tif", package = "tidyterra") |>
  terra::rast() |>
  terra::project("EPSG:4326")

r_tidy <- tidy(r)

r_tidy

# Back to a SpatRaster with
as_spatraster(r_tidy)

# SpatVector
cyl <- terra::vect(system.file("extdata/cyl.gpkg", package = "tidyterra"))

cyl

tidy(cyl)

# SpatExtent
ex <- cyl |> terra::ext()

ex

tidy(ex)

# With crs
tidy(ex, crs = pull_crs(cyl))

# SpatGraticule
grat <- terra::graticule(60, 30, crs = "+proj=robin")

grat
tidy(grat)
# }

Run the code above in your browser using DataLab