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 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 = "")Arguments
- x
A
SpatRastercreated withterra::rast(), aSpatVectorcreated withterra::vect(), aSpatGraticule(seeterra::graticule()) or aSpatExtent(seeterra::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 areunique,"universal": Make the namesuniqueand 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.namesfor names in the style of base R).A purrr-style anonymous function, see
rlang::as_function()
This argument is passed on as
repairtovctrs::vec_as_names(). See there for more details on these terms and the strategies used to enforce them.- maxcell
Positive integer. Maximum number of cells to use for the plot.
- pivot
Logical. When
TRUE, aSpatRasteris returned in long format. WhenFALSE(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/sfcobject, aSpatRaster/SpatVectorobject, acrsobject fromsf::st_crs(), a character string (for example a proj4 string), or an integer representing an EPSG code.
Value
tidy.SpatVector(), tidy.SpatGraticule() and tidy.SpatExtent()
return a sf object.
tidy.SpatRaster() returns a tibble. See Methods.
Methods
Implementation of the generic generics::tidy() method.
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 centre in the corresponding CRS.lyr: Name of theSpatRasterlayer associated withvalue.value: Cell value for the correspondinglyr.
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(), 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
# \donttest{
# Get a SpatRaster
r <- system.file("extdata/volcano2.tif", package = "tidyterra") |>
terra::rast() |>
terra::project("EPSG:4326")
r_tidy <- tidy(r)
r_tidy
#> # A tibble: 23,166 × 3
#> x y elevation
#> <dbl> <dbl> <dbl>
#> 1 175. -36.9 NA
#> 2 175. -36.9 NA
#> 3 175. -36.9 NA
#> 4 175. -36.9 NA
#> 5 175. -36.9 NA
#> 6 175. -36.9 NA
#> 7 175. -36.9 NA
#> 8 175. -36.9 NA
#> 9 175. -36.9 NA
#> 10 175. -36.9 NA
#> # ℹ 23,156 more rows
# Convert back to a `SpatRaster`.
as_spatraster(r_tidy)
#> class : SpatRaster
#> size : 162, 143, 1 (nrow, ncol, nlyr)
#> resolution : 4.916776e-05, 4.916772e-05 (x, y)
#> extent : 174.7611, 174.7682, -36.87992, -36.87195 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> source(s) : memory
#> name : elevation
#> min value : 76.565987
#> max value : 195.504364
# SpatVector
cyl <- terra::vect(system.file("extdata/cyl.gpkg", package = "tidyterra"))
cyl
#> class : SpatVector
#> geometry : polygons
#> dimensions : 9, 3 (geometries, attributes)
#> extent : 2892687, 3341372, 2017622, 2361600 (xmin, xmax, ymin, ymax)
#> source : cyl.gpkg
#> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035)
#> names : iso2 cpro name
#> type : <chr> <chr> <chr>
#> values : ES-AV 05 Avila
#> ES-BU 09 Burgos
#> ES-LE 24 Leon
#> ...
tidy(cyl)
#> Simple feature collection with 9 features and 3 fields
#> Geometry type: GEOMETRY
#> Dimension: XY
#> Bounding box: xmin: 2892687 ymin: 2017622 xmax: 3341372 ymax: 2361600
#> Projected CRS: ETRS89-extended / LAEA Europe
#> # A tibble: 9 × 4
#> iso2 cpro name geometry
#> * <chr> <chr> <chr> <GEOMETRY [m]>
#> 1 ES-AV 05 Avila POLYGON ((3126360 2066778, 3125074 2065007, 3124303 20…
#> 2 ES-BU 09 Burgos MULTIPOLYGON (((3276731 2262326, 3275910 2265723, 3270…
#> 3 ES-LE 24 Leon POLYGON ((3049427 2233673, 3049069 2234201, 3047819 22…
#> 4 ES-P 34 Palencia MULTIPOLYGON (((3175411 2291868, 3175606 2293658, 3177…
#> 5 ES-SA 37 Salamanca POLYGON ((3042661 2138939, 3043434 2140279, 3046345 21…
#> 6 ES-SG 40 Segovia POLYGON ((3126360 2066778, 3124037 2067928, 3118421 20…
#> 7 ES-SO 42 Soria POLYGON ((3194084 2154251, 3194362 2156613, 3195482 21…
#> 8 ES-VA 47 Valladolid MULTIPOLYGON (((3158120 2161552, 3155455 2155198, 3149…
#> 9 ES-ZA 49 Zamora POLYGON ((3042661 2138939, 3040851 2133391, 3038188 21…
# SpatExtent
ex <- cyl |> terra::ext()
ex
#> SpatExtent : 2892686.537, 3341372.1325, 2017621.67, 2361599.9431 (xmin, xmax, ymin, ymax)
tidy(ex)
#> Simple feature collection with 1 feature and 0 fields
#> Geometry type: POLYGON
#> Dimension: XY
#> Bounding box: xmin: 2892687 ymin: 2017622 xmax: 3341372 ymax: 2361600
#> CRS: NA
#> # A tibble: 1 × 1
#> geometry
#> * <POLYGON>
#> 1 ((2892687 2017622, 2892687 2361600, 3341372 2361600, 3341372 2017622, 2892687…
# With crs
tidy(ex, crs = pull_crs(cyl))
#> Simple feature collection with 1 feature and 0 fields
#> Geometry type: POLYGON
#> Dimension: XY
#> Bounding box: xmin: 2892687 ymin: 2017622 xmax: 3341372 ymax: 2361600
#> Projected CRS: ETRS89-extended / LAEA Europe
#> # A tibble: 1 × 1
#> geometry
#> * <POLYGON [m]>
#> 1 ((2892687 2017622, 2892687 2361600, 3341372 2361600, 3341372 2017622, 2892687…
# SpatGraticule
grat <- terra::graticule(60, 30, crs = "+proj=robin")
grat
#> class : SpatGraticule
#> lon : -180 -120 -60 0 60 120 180
#> lat : -90 -60 -30 0 30 60 90
#> coord. ref. : +proj=robin +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
#> extent : -17005833, 17005833, -8625155, 8625155 (xmin, xmax, ymin, ymax)
tidy(grat)
#> Simple feature collection with 14 features and 3 fields
#> Geometry type: LINESTRING
#> Dimension: XY
#> Bounding box: xmin: -17005830 ymin: -8625155 xmax: 17005830 ymax: 8625155
#> Projected CRS: PROJCRS["unknown",
#> BASEGEOGCRS["unknown",
#> DATUM["World Geodetic System 1984",
#> ELLIPSOID["WGS 84",6378137,298.257223563,
#> LENGTHUNIT["metre",1]],
#> ID["EPSG",6326]],
#> PRIMEM["Greenwich",0,
#> ANGLEUNIT["degree",0.0174532925199433],
#> ID["EPSG",8901]]],
#> CONVERSION["unknown",
#> METHOD["Robinson"],
#> PARAMETER["Longitude of natural origin",0,
#> ANGLEUNIT["degree",0.0174532925199433],
#> ID["EPSG",8802]],
#> PARAMETER["False easting",0,
#> LENGTHUNIT["metre",1],
#> ID["EPSG",8806]],
#> PARAMETER["False northing",0,
#> LENGTHUNIT["metre",1],
#> ID["EPSG",8807]]],
#> CS[Cartesian,2],
#> AXIS["(E)",east,
#> ORDER[1],
#> LENGTHUNIT["metre",1,
#> ID["EPSG",9001]]],
#> AXIS["(N)",north,
#> ORDER[2],
#> LENGTHUNIT["metre",1,
#> ID["EPSG",9001]]]]
#> # A tibble: 14 × 4
#> h lon lat geometry
#> * <lgl> <dbl> <dbl> <LINESTRING [m]>
#> 1 FALSE -180 NaN (-9050504 -8625155, -9157404 -8597567, -9271364 -8565720, …
#> 2 FALSE -120 NaN (-6033669 -8625155, -6104936 -8597567, -6180910 -8565720, …
#> 3 FALSE -60 NaN (-3016835 -8625155, -3052468 -8597567, -3090455 -8565720, …
#> 4 FALSE 0 NaN (0 -8625155, 0 -8597567, 0 -8565720, 0 -8529771, 0 -848987…
#> 5 FALSE 60 NaN (3016835 -8625155, 3052468 -8597567, 3090455 -8565720, 313…
#> 6 FALSE 120 NaN (6033669 -8625155, 6104936 -8597567, 6180910 -8565720, 626…
#> 7 FALSE 180 NaN (9050504 -8625155, 9157404 -8597567, 9271364 -8565720, 939…
#> 8 TRUE NaN -90 (-9050504 -8625155, -9004795 -8625155, -8959085 -8625155, …
#> 9 TRUE NaN -60 (-13580859 -6336039, -13512269 -6336039, -13443678 -633603…
#> 10 TRUE NaN -30 (-16325600 -3208558, -16243147 -3208558, -16160695 -320855…
#> 11 TRUE NaN 0 (-17005833 -4.488677e-11, -16919945 -3.893321e-11, -168340…
#> 12 TRUE NaN 30 (-16325600 3208558, -16243147 3208558, -16160695 3208558, …
#> 13 TRUE NaN 60 (-13580859 6336039, -13512269 6336039, -13443678 6336039, …
#> 14 TRUE NaN 90 (-9050504 8625155, -9004795 8625155, -8959085 8625155, -89…
# }
