SpatRaster color tablesSome categorical SpatRaster objects may have an associated color table.
These functions generate scales and color vectors based on the color table
from terra::coltab() associated with a SpatRaster.
You can also get a vector of colors named with the corresponding factor with
get_coltab_pal().
Additional arguments ... are passed to
ggplot2::discrete_scale().
tidyterra documents only a selection of these additional
arguments, so check ggplot2::discrete_scale() to see the full range of
arguments accepted.
scale_fill_coltab(
data,
...,
alpha = NA,
na.translate = FALSE,
na.value = "transparent",
drop = TRUE
)scale_colour_coltab(
data,
...,
alpha = NA,
na.translate = FALSE,
na.value = "transparent",
drop = TRUE
)
get_coltab_pal(x)
The corresponding ggplot2 layer with the values applied to the
fill/colour aesthetics.
A SpatRaster with one or more color tables.
See terra::has.colors().
Arguments passed on to ggplot2::discrete_scale
breaksOne of:
NULL for no breaks
waiver() for the default breaks (the scale limits)
A character vector of breaks
A function that takes the limits as input and returns breaks as output. Also accepts rlang lambda function notation.
minor_breaksOne of:
NULL for no minor breaks
waiver() for the default breaks (none for discrete, one minor break
between each major break for continuous)
A numeric vector of positions
A function that given the limits returns a vector of minor breaks. Also accepts rlang lambda function notation. When the function has two arguments, it will be given the limits and major break positions.
labelsOne of the options below. Please note that when labels is a
vector, it is highly recommended to also set the breaks argument as a
vector to protect against unintended mismatches.
NULL for no labels
waiver() for the default labels computed by the
transformation object
A character vector giving labels (must be same length as breaks)
An expression vector (must be the same length as breaks). See ?plotmath for details.
A function that takes the breaks as input and returns labels as output. Also accepts rlang lambda function notation.
limitsOne of:
NULL to use the default scale values
A character vector that defines possible values of the scale and their order
A function that accepts the existing (automatic) values and returns new ones. Also accepts rlang lambda function notation.
expandFor position scales, a vector of range expansion constants used to add some
padding around the data to ensure that they are placed some distance
away from the axes. Use the convenience function expansion()
to generate the values for the expand argument. The defaults are to
expand the scale by 5% on each side for continuous variables, and by
0.6 units on each side for discrete variables.
The alpha transparency: could be NA or a number in [0,1].
See argument alpha in scale_fill_terrain_d().
Logical. If TRUE, remove NA values from the legend.
The default is TRUE.
Missing values will be replaced with this value. By default,
tidyterra uses na.value = "transparent" so cells with NA are
not filled. See also
#120.
Logical. If TRUE, omit unused factor levels from the scale.
The default (TRUE) removes unused factors.
terra::coltab(), ggplot2::discrete_scale(),
ggplot2::scale_fill_manual().
Other color scales, palettes and hypsometric tints:
scale_cross_blended,
scale_grass,
scale_hypso,
scale_princess,
scale_terrain,
scale_whitebox,
scale_wiki
library(terra)
# Geological Eras
# Spanish Geological Survey (IGME)
r <- rast(system.file("extdata/cyl_era.tif", package = "tidyterra"))
plot(r)
# Get the color table palette.
coltab_pal <- get_coltab_pal(r)
coltab_pal
# \donttest{
# With ggplot2 + tidyterra
library(ggplot2)
gg <- ggplot() +
geom_spatraster(data = r)
# Default plot
gg
# With color tables
gg +
scale_fill_coltab(data = r)
# }
Run the code above in your browser using DataLab