| fortify.Spat | R Documentation |
Spat* ObjectsFortify SpatRaster and SpatVector objects to data frames. This provide
native compatibility with ggplot2::ggplot().
Note that these methods are now implemented as a wrapper of tidy.Spat
methods.
## S3 method for class 'SpatRaster'
fortify(
model,
data,
...,
.name_repair = c("unique", "check_unique", "universal", "minimal", "unique_quiet",
"universal_quiet"),
maxcell = terra::ncell(model) * 1.1,
pivot = FALSE
)
## S3 method for class 'SpatVector'
fortify(model, data, ...)
## S3 method for class 'SpatGraticule'
fortify(model, data, ...)
## S3 method for class 'SpatExtent'
fortify(model, data, ..., crs = "")
model |
A |
data |
Not used by this method. |
... |
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 |
fortify.SpatVector(), fortify.SpatGraticule() and fortify.SpatExtent()
return a sf object.
fortify.SpatRaster() returns a tibble. See Methods.
Implementation of the generic ggplot2::fortify() 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(fortifiedSpatRaster, "crs").
It is possible to convert the fortified object onto a SpatRaster again with
as_spatraster().
When pivot = TRUE the SpatRaster is fortified in a "long" format (see
tidyr::pivot_longer()). The fortified 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, see
Examples.
SpatVector, SpatGraticule and SpatExtentReturn a sf object than can be used with ggplot2::geom_sf().
tidy.Spat, sf::st_as_sf(), as_tibble.Spat,
as_spatraster(), ggplot2::fortify().
Other ggplot2 utils:
autoplot.Spat,
geom_spat_contour,
geom_spatraster(),
geom_spatraster_rgb(),
ggspatvector,
stat_spat_coordinates()
Other ggplot2 methods:
autoplot.Spat
Coercing objects:
as_coordinates(),
as_sf(),
as_spatraster(),
as_spatvector(),
as_tibble.Spat,
tidy.Spat
# Demonstrate the use with ggplot2
library(ggplot2)
# Get a SpatRaster
r <- system.file("extdata/volcano2.tif", package = "tidyterra") |>
terra::rast() |>
terra::project("EPSG:4326")
# You can now use a SpatRaster with any geom
ggplot(r, maxcell = 50) +
geom_histogram(aes(x = elevation),
bins = 20, fill = "lightblue",
color = "black"
)
# For SpatVector, SpatGraticule and SpatExtent you can use now geom_sf()
# Create a SpatVector
extfile <- system.file("extdata/cyl.gpkg", package = "tidyterra")
cyl <- terra::vect(extfile)
class(cyl)
ggplot(cyl) +
geom_sf()
# SpatGraticule
g <- terra::graticule(60, 30, crs = "+proj=robin")
class(g)
ggplot(g) +
geom_sf()
# SpatExtent
ex <- terra::ext(cyl)
class(ex)
ggplot(ex, crs = cyl) +
geom_sf(fill = "red", alpha = 0.3) +
geom_sf(data = cyl, fill = NA)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.