r <- rast(system.file("ex/elev.tif", package="terra"))
plot(r)
contour(r, add=TRUE)
v <- as.contour(r)
plot(r)
lines(v)
contour(r, filled=TRUE, nlevels=5)
## if you want a SpatVector with contour lines
template <- disagg(rast(r), 10)
rr <- resample(r, template)
rr <- floor(rr/100) * 100
v <- as.polygons(rr)
plot(v, 1, col=terrain.colors(7))
## to combine filled contours with contour lines (or other spatial data)
br <- seq(100, 600, 100)
plot(r, breaks=br)
lines(as.contour(r, levels=br))
## or
x <- classify(r, br) |> as.polygons()
plot(x, "elevation")
Run the code above in your browser using DataLab