Assign labels and plotting colors to a categorical raster

set_rastlabels(in_rast, class_names = NULL, class_colors = NULL,
  verbose = TRUE)

Arguments

in_rast

a categorical Raster object or the path to a valid categorical raster file

class_names

character array optional array of names to be assigned to the raster classes (useful for plotting with plot_rast or plot_rast_gg) - see examples. Order of the class names should follow the numeric sorting of the values to which class names should correspond. If NULL, class names are assigned by converting the numeric values of the new raster to character, Default: NULL

class_colors

character array optional array of colors to be assigned to the raster classes (useful for plotting with plot_rast or plot_rast_gg) - see examples. Order of the class names should follow the numeric sorting of the values to which class colors should correspond. If NULL, class colors are assigned automatically using scales::hue_pal()(n_class), Default: NULL

verbose

logical if TRUE, extended processing information is sent to the console in the form of messages

Value

a 'Raster' object equal to in_rast to which a Raster Attribute table has been added (see raster::ratify)

Details

Simple wrapper around raster::ratify, providing the added functionality of specifying specific names for the classes.

See also

Examples

library(raster) library(dplyr) in_rast <- raster::raster(ncol = 5, nrow = 5) %>% raster::init("row") levels(in_rast)[[1]]
#> NULL
# categorize and assign standard class names cat_rast <- set_rastlabels(in_rast)
#> set_rastlabels --> categorizing `in_rast`
#> Warning: set_rastlabels --> class_names not provided or not matching the number of classes of in_rast. #> Class names will beignored!
levels(cat_rast)[[1]]
#> ID Class Color #> 1 1 1 #F8766D #> 2 2 2 #A3A500 #> 3 3 3 #00BF7D #> 4 4 4 #00B0F6 #> 5 5 5 #E76BF3
# categorize and assign custom class names cat_rast <- set_rastlabels(in_rast, class_names = c("Water", "Land", "Vegetation", "Urban", "Other"))
#> set_rastlabels --> categorizing `in_rast`
levels(cat_rast)[[1]]
#> ID Class Color #> 1 1 Water #F8766D #> 2 2 Land #A3A500 #> 3 3 Vegetation #00BF7D #> 4 4 Urban #00B0F6 #> 5 5 Other #E76BF3
#' # categorize and assign custom class names and colors cat_rast <- set_rastlabels(in_rast, class_names = c("Water", "Land", "Vegetation", "Urban", "Other"), class_colors = c("blue", "maroon", "green", "red", "black"))
#> set_rastlabels --> categorizing `in_rast`
levels(cat_rast)[[1]][1]
#> ID #> 1 1 #> 2 2 #> 3 3 #> 4 4 #> 5 5
# Class names are automatically recognized by [`plot_rast_gg`] plot_rast_gg(cat_rast, scalebar = FALSE)