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