| fortify.Spat {tidyterra} | R Documentation |
Fortify Spat* Objects
Description
Fortify SpatRaster and SpatVector objects to data frames. This provide
native compatibility with ggplot2::ggplot().
Usage
## S3 method for class 'SpatRaster'
fortify(
model,
data,
...,
.name_repair = "unique",
maxcell = terra::ncell(model) * 1.1,
pivot = FALSE
)
## S3 method for class 'SpatVector'
fortify(model, data, ...)
Arguments
model |
A |
data |
Not used by this method. |
... |
Other arguments passed on to
|
.name_repair |
Treatment of problematic column names:
|
maxcell |
positive integer. Maximum number of cells to use for the plot. |
pivot |
Logical. When |
Value
fortify.SpatVector() returns a sf object and
fortify.SpatRaster() returns a tibble. See Methods.
Methods
Implementation of the generic ggplot2::fortify() 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(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 theSpatRasterlayer ofvalue. -
value: The value of theSpatRasterin the correspondinglyr.
This option may be useful when using several geom_* and for faceting, see
Examples.
SpatVector
Return a sf object than can be used with ggplot2::geom_sf().
See Also
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
Examples
# Get a SpatRaster
r <- system.file("extdata/volcano2.tif", package = "tidyterra") %>%
terra::rast() %>%
terra::project("EPSG:4326")
fortified <- ggplot2::fortify(r)
fortified
# The crs is an attribute of the fortified SpatRaster
attr(fortified, "crs")
# Back to a SpatRaster with
as_spatraster(fortified)
# You can now use a SpatRaster with any geom
library(ggplot2)
ggplot(r) +
geom_histogram(aes(x = elevation),
bins = 20, fill = "lightblue",
color = "black"
)
# ... and other packages
# Use metR with facets
library(metR)
temp <- terra::rast(system.file("extdata/cyl_temp.tif",
package = "tidyterra"
))
brks <- seq(0, 21, 3) # Fix breaks!
# Pivot option for faceting
ggplot(temp, aes(x, y), pivot = TRUE) +
# tidyterra, don't inherit aes
geom_spatraster_contour_filled(
data = temp, inherit.aes = FALSE,
breaks = brks
) +
# metR
geom_contour_tanaka(aes(z = value), breaks = brks) +
facet_wrap(~lyr, nrow = 1) +
scale_fill_whitebox_d(palette = "muted") +
theme_minimal() +
labs(
title = "tidyterra + metR", subtitle = "Facets",
fill = "temp (°C)", x = "", y = ""
)
# Create a SpatVector
extfile <- system.file("extdata/cyl.gpkg", package = "tidyterra")
cyl <- terra::vect(extfile)
cyl
# To sf
ggplot2::fortify(cyl)
# Now you can use geom_sf() straight away thanks to fortify::SpatVector()
library(ggplot2)
ggplot(cyl) +
geom_sf()