# NOT RUN {
set.seed(0)
r <- rast(nrow=10, ncol=10)
values(r) <- sample(3, ncell(r), replace=TRUE)
is.factor(r)
cls <- c("forest", "water", "urban")
# make the raster start at zero
x <- r - 1
levels(x) <- cls
names(x) <- "land cover"
is.factor(x)
x
plot(x, col=c("green", "blue", "light gray"))
text(x, digits=3, cex=.75, halo=TRUE)
# raster starts at 3
x <- r + 2
is.factor(x)
# approach 1
levels(x) <- c("", "", "", "forest", "water", "urban")
# approach 2, also showing the use of two categories
d <- data.frame(id=3:5, cover=cls, letters=letters[1:3])
levels(x) <- d
x
## switch categories
head(cats(x, 1))
# get current index
setCats(x, 1)
# set index
setCats(x, 1, index=3)
setCats(x, 1)
plot(x, col=c("green", "blue", "light gray"))
text(x, digits=3, cex=.75, halo=TRUE)
#setCats(x, 1, index=2)
#p <- as.polygons(x)
#plot(p, "cover", col=c("green", "blue", "light gray"))
# }
Run the code above in your browser using DataLab