# \donttest{
# 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"
)
# 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()
# }
Run the code above in your browser using DataLab