| tidy.Spat | R Documentation |
Spat* object into a tidy tibbleTurn 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.
## S3 method for class '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 class 'SpatVector'
tidy(x, ...)
## S3 method for class 'SpatGraticule'
tidy(x, ...)
## S3 method for class 'SpatExtent'
tidy(x, ..., crs = "")
x |
A |
... |
Ignored by these methods. |
.name_repair |
Treatment of problematic column names:
This argument is passed on as |
maxcell |
positive integer. Maximum number of cells to use for the plot. |
pivot |
Logical. When |
crs |
Input potentially including or representing a CRS. It could be
a |
tidy.SpatVector(), tidy.SpatGraticule() and tidy.SpatExtent()
return a sf object.
tidy.SpatRaster() returns a tibble. See Methods.
Implementation of the generic generics::tidy() method.
SpatRasterReturn 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 SpatExtentReturn a sf object than can be used with ggplot2::geom_sf().
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
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.