Learn R Programming

tidyterra (version 1.3.0)

tidy.Spat: Tidy Spat* objects for plotting

Description

tidy() methods for SpatRaster, SpatVector, SpatGraticule and SpatExtent objects. These methods return a tibble for SpatRaster objects and sf objects for vector-based inputs. This interface is similar to fortify.Spat and is provided in case the ggplot2::fortify() method is deprecated in the future.

Usage

# S3 method for SpatRaster
tidy(
  x,
  ...,
  .name_repair = c("unique", "check_unique", "universal", "minimal", "unique_quiet",
    "universal_quiet"),
  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(), a SpatVector created with terra::vect(), a SpatGraticule (see terra::graticule()) or a 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

  • "unique_quiet": Same as "unique", but "quiet"

  • "universal_quiet": Same as "universal", but "quiet"

  • 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()

This argument is passed on as repair to vctrs::vec_as_names(). See there for more details on these terms and the strategies used to enforce them.

maxcell

Positive integer or Inf. Maximum number of raster cells to use. Use Inf to use all cells.

pivot

Logical. When TRUE, a SpatRaster is returned in long format. When FALSE (the default), it is returned as a data frame with one column per layer. See Details.

crs

Input that includes or represents a CRS. It can be an sf or sfc object, a SpatRaster or SpatVector object, a crs object from sf::st_crs(), a character string (for example a PROJ string), or an integer representing an EPSG code.

Methods

Implementation of the generic generics::tidy() methods for Spat* objects.

SpatRaster

Returns a tibble that can be used with ggplot2::geom_*, such as ggplot2::geom_point() and ggplot2::geom_raster().

The resulting tibble includes coordinates in the x and y columns. The values of each layer are added as extra columns using the layer names from the SpatRaster.

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

You can convert the tidy object back to a SpatRaster with as_spatraster().

When pivot = TRUE, the SpatRaster is returned in long format (see tidyr::pivot_longer()). The tidy object has the following columns:

  • x, y: Coordinates of the cell center in the corresponding CRS.

  • lyr: Name of the SpatRaster layer associated with value.

  • value: Cell value for the corresponding lyr.

This option can be useful when combining several geom_* layers or when faceting.

SpatVector, SpatGraticule and SpatExtent

Returns an sf object that can be used with ggplot2::geom_sf().

See Also

sf::st_as_sf(), 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

# Convert back to a `SpatRaster`.
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 a 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