| Title: | Colour Palettes for Data |
|---|---|
| Description: | Colour palettes for data, based on some well known public data sets. Includes helper functions to map absolute values to known palettes, and capture the work of image colour mapping as raster data sets. |
| Authors: | Michael D. Sumner [aut, cre, cph], Abigael Proctor [ctb] (Named the package), Tomas Remenyi [ctb] (Provided colours for element_pal), R Core Team and contributors worldwide [ctb] (source code of image.default) |
| Maintainer: | Michael D. Sumner <[email protected]> |
| License: | GPL-3 |
| Version: | 0.5.0 |
| Built: | 2026-10-04 09:55:40 UTC |
| Source: | https://github.com/AustralianAntarcticDivision/palr |
Deep bathymetry colours.
bathy_deep_pal(x, palette = FALSE, alpha = 1, ...) bathyDeepPal(x, palette = FALSE, alpha = 1, ...)bathy_deep_pal(x, palette = FALSE, alpha = 1, ...) bathyDeepPal(x, palette = FALSE, alpha = 1, ...)
x |
a vector of data values or a single num (n) |
palette |
logical, if |
alpha |
value in 0,1 to specify opacity |
... |
currently ignored |
Colour ramp suitable for deep waters (-5500) to sea level. The palette functions operate in 3 modes: 1) n colours - Pal(6) - returns 6 colours from the palette 2) data - Pal(c(-4000, -2000, -100)) - return colours for 3 depths 3) palette - Pal(palette = TRUE) - return the full palette and breaks Derived from maps created in Matlab by Emmanuel Laurenceau.
colours, palette, or function, see Details
plot(1:15, pch = 19, cex = 4, col = bathy_deep_pal(15))plot(1:15, pch = 19, cex = 4, col = bathy_deep_pal(15))
Ocean colour palette for chlorophyll-a.
chl_pal(x, palette = FALSE, alpha = 1) chlPal(x, palette = FALSE, alpha = 1, ...)chl_pal(x, palette = FALSE, alpha = 1) chlPal(x, palette = FALSE, alpha = 1, ...)
x |
a vector of data values or a single number |
palette |
logical, if |
alpha |
value in 0,1 to specify opacity |
... |
currently unused |
Flexible control of the chlorophyll-a palette. If x is a
single number, the function returns that many colours evenly
spaced from the palette. If x is a vector of multiple
values the palette is queried for colours matching those values,
and these are returned. If x is missing and palette
is FALSE then a function is returned that will generate n
evenly spaced colours from the palette, as per
colorRampPalette.
colours, palette, or function, see Details
Derived from a file once found at 'http://oceancolor.gsfc.nasa.gov/DOCS/palette_chl_etc.txt'
## just get a small number of evenly space colours plot(1:10, col = chl_pal(10)) ## store the full palette and work with values and colours pal <- chl_pal(palette = TRUE) ## the standard full palette image(chl, breaks = pal$breaks, col = pal$cols[-1])## just get a small number of evenly space colours plot(1:10, col = chl_pal(10)) ## store the full palette and work with values and colours pal <- chl_pal(palette = TRUE) ## the standard full palette image(chl, breaks = pal$breaks, col = pal$cols[-1])
Create colours from colour names in one easy step.
col2hex(x, alpha = 1)col2hex(x, alpha = 1)
x |
vector of colour names or hex strings |
alpha |
optional transparency value in [0,1], can be per colour in |
character string of hex colours
col2hex(c("aliceblue", "firebrick"), alpha = c(1, .5)) col2hex(c("#FFFFFF", "#123456FF"), alpha = 0.1)col2hex(c("aliceblue", "firebrick"), alpha = c(1, .5)) col2hex(c("#FFFFFF", "#123456FF"), alpha = 0.1)
Scales input data to the palette, so that colour is mapped linearly to the range of values.
d_pal(x, pal = hcl.colors(84)) data_pal(x, pal = hcl.colors(84))d_pal(x, pal = hcl.colors(84)) data_pal(x, pal = hcl.colors(84))
x |
data vector, maybe be numeric or character |
pal |
palette, may be colours or a function |
Default palette 'pal' is the 'viridis' colours of [grDevices::hcl.colors()], and may be literal colour values or a function.
[data_pal()] is an alias of [d_pal()].
character string of hex colours
plot(1:100, col = d_pal(1:100)) plot(1:100, col = d_pal(1:100, chl_pal))plot(1:100, col = d_pal(1:100)) plot(1:100, col = d_pal(1:100, chl_pal))
From AAD underway facility (DiRT).
dirty_pal(x, palette = FALSE, alpha = 1)dirty_pal(x, palette = FALSE, alpha = 1)
x |
a vector of data values or a single num (n) |
palette |
logical, if |
alpha |
0 to 1 for transparency |
The palette functions operate in 3 modes: 1) n colours - Pal(6) - returns 6 colours from the palette 2) data - Pal(c(-4000, -2000, 100)) - return colours for 3 elevations 3) palette - Pal(palette = TRUE) - return the full palette and breaks
The palette spans -8000 to 1000 metres, values outside that range clamp to the end colours.
colours, palette, or function, see Details
dirty_pal(seq(-8000, 0, length.out = 10))dirty_pal(seq(-8000, 0, length.out = 10))
Colours for sea ice.
ice_pal(x, palette = FALSE, alpha = 1, ..., amsre = FALSE) icePal(x, palette = FALSE, alpha = 1, ...)ice_pal(x, palette = FALSE, alpha = 1, ..., amsre = FALSE) icePal(x, palette = FALSE, alpha = 1, ...)
x |
a vector of data values or a single num (n) |
palette |
logical, if |
alpha |
value in 0,1 to specify opacity |
... |
currently ignored |
amsre |
use old AMSRE colours ('FALSE' by default) |
The palette functions operate in 3 modes: 1) n colours - Pal(6) - returns 6 colours from the palette 2) data - Pal(c(10, 50, 100)) - return colours for 3 ice concentrations 3) palette - Pal(palette = TRUE) - return the full palette and breaks
colours, palette, or function, see Details
amsre colours derived from 'http://www.iup.uni-bremen.de/seaice/amsr/'., nsidc colours extracted in data-raw/.
if (requireNamespace("raster")) { nsidcfile <- system.file("extdata", "nt_20140320_f17_v01_s.bin", package = "palr", mustWork = TRUE) r <- raster::raster(nsidcfile) icp <- ice_pal(palette = TRUE) ## The AMSR colours raster::plot(r, col = icp$col, zlim = range(icp$breaks), main = sprintf("NSIDC ice %s", format(raster::getZ(r)))) }if (requireNamespace("raster")) { nsidcfile <- system.file("extdata", "nt_20140320_f17_v01_s.bin", package = "palr", mustWork = TRUE) r <- raster::raster(nsidcfile) icp <- ice_pal(palette = TRUE) ## The AMSR colours raster::plot(r, col = icp$col, zlim = range(icp$breaks), main = sprintf("NSIDC ice %s", format(raster::getZ(r)))) }
'image_hex()' turns numeric, integer, raw, logical, or character image data into a character matrix of hex colours, ready for [graphics::rasterImage()], writing to PNG/GeoTIFF, or use as a texture. This is the colour engine shared with the 'ximage' package.
image_hex( x, col = NULL, breaks = NULL, zlim = NULL, alpha = NULL, na.col = "transparent" )image_hex( x, col = NULL, breaks = NULL, zlim = NULL, alpha = NULL, na.col = "transparent" )
x |
matrix or array of image data (see Details) |
col |
colours to map to, a vector or a function |
breaks |
numeric breakpoints for the colours |
zlim |
absolute data range for the palette (single-band only) |
alpha |
opacity multiplier in [0, 1] |
na.col |
colour for missing values (default "transparent") |
Supported inputs are a matrix (palette mapping via 'col', 'breaks', 'zlim'), a 3D array with 1 band (treated as a matrix), 2 bands (grey + alpha), 3 bands (RGB), or 4 bands (RGBA), a raw matrix or array (converted to integer), and a character matrix of colours (passed through, with NA replaced by 'na.col').
Behaviour guarantees:
Non-finite values (NA, NaN, Inf) always map to 'na.col'. Colour scaling for multi-band data is autodetected per image: data within [0, 1] is used as-is, within [0, 255] is divided by 255, anything else is rescaled by the finite range of the colour bands jointly (so hue relationships are preserved); an alpha band is scaled independently. Constant (zero-range) data maps to the middle of the palette. 'zlim' anchors the palette to an absolute range and values outside it map to 'na.col'; 'zlim' is ignored with a warning for multi-band input. 'alpha' is a constant (or recycled) opacity multiplier in [0, 1] applied on top of any existing alpha.
Use the stretch functions ([stretch_linear()] and friends) to normalize high dynamic range data before colour mapping.
character matrix of hex colours
m <- image_hex(volcano) dim(m) ## rgb array in 0, 255 arr <- array(c(volcano, volcano / 2, 255 - volcano), c(dim(volcano), 3L)) m <- image_hex(arr) ## plot it plot(NA, xlim = c(0, 1), ylim = c(0, 1), asp = 1) rasterImage(image_hex(volcano, col = grDevices::hcl.colors(64)), 0, 0, 1, 1)m <- image_hex(volcano) dim(m) ## rgb array in 0, 255 arr <- array(c(volcano, volcano / 2, 255 - volcano), c(dim(volcano), 3L)) m <- image_hex(arr) ## plot it plot(NA, xlim = c(0, 1), ylim = c(0, 1), asp = 1) rasterImage(image_hex(volcano, col = grDevices::hcl.colors(64)), 0, 0, 1, 1)
If no 'col' is provided, the default image palette is used. The density can be controlled with 'n' and the mapping with the optional 'breaks'. If 'breaks' is included as well as 'n', 'n' is ignored.
image_pal(x, col, ..., breaks = NULL, n = NULL, zlim = NULL) image_raster(x, col, ..., breaks = NULL, n = NULL, zlim = NULL) image_stars(x, col, ..., breaks = NULL, n = NULL, zlim = NULL)image_pal(x, col, ..., breaks = NULL, n = NULL, zlim = NULL) image_raster(x, col, ..., breaks = NULL, n = NULL, zlim = NULL) image_stars(x, col, ..., breaks = NULL, n = NULL, zlim = NULL)
x |
numeric values, raster object (single layer only) or stars object (single variable, 2D array only) |
col |
function to generate colours, or a vector of hex colours |
... |
ignored |
breaks |
optionally used to specify colour mapping |
n |
optionally used to specify density of colours from 'col' (ignored if 'breaks' is set) |
zlim |
numeric range to clamp values to an absolute scale (ignored if 'breaks' is set) |
The function 'image_pal()' only returns hex character string colours. The function 'image_raster()' will map a raster of numeric values to an RGB 3-layer (channel) raster brick, and 'image_stars()' similarly for a 3-dimensional stars object.
Please note that the expansion to 3-channels is a fairly wasteful thing to do, the overall data is expanded from a single layer to three but this faciliates a specific task of creating textures for 3D mapping, and this is the only way to do it currently. It's also useful in other situations, for controlling exactly the kind of plots we can achieve and for exporting to image formats such as 'GeoTIFF' or 'PNG'.
for 'image_pal()' a vector of hex colours, for 'image_raster' and 'image_stars' a raster or stars object with 3 channel RGB (range 0,255)
vals <- sort(rnorm(100)) cols <- image_pal(vals, zlim = c(-2.4, .5)) plot(vals, col = cols); abline(h = .5) points(vals, pch = ".") ## zlim excluded some of the rangevals <- sort(rnorm(100)) cols <- image_pal(vals, zlim = c(-2.4, .5)) plot(vals, col = cols); abline(h = .5) points(vals, pch = ".") ## zlim excluded some of the range
Create a time-indexed colour map, useful for maintaining an absolute scale across time series as a function of date-time.
mk_timePal(x, col)mk_timePal(x, col)
x |
date-times |
col |
colours, can be a function or an actual set of colours |
function of date-time
dts <- seq(as.Date("1749-01-01"), by = "1 month", length.out = length(sunspots)) d <- data.frame(date = dts, sunspots = as.vector(t(sunspots))) tpal <- mk_timePal(d$date, col = sst_pal(50)) par(mfrow = c(2, 1)) plot(sunspots ~ date, col = tpal(date), data = d) ## colours maintained by absolute date plot(sunspots ~ date, col = tpal(date), data = d[1500:1800, ], cex = 2) ## we can now insert new points and maintain this colour ramp d2 <- data.frame(date = seq(min(d$date), max(d$date), by = "5 days")) d2$sunspots <- approxfun(d$date, d$sunspots)(d2$date) points(sunspots ~ date, col = tpal(date), data = d2, pch = 19, cex = 0.5)dts <- seq(as.Date("1749-01-01"), by = "1 month", length.out = length(sunspots)) d <- data.frame(date = dts, sunspots = as.vector(t(sunspots))) tpal <- mk_timePal(d$date, col = sst_pal(50)) par(mfrow = c(2, 1)) plot(sunspots ~ date, col = tpal(date), data = d) ## colours maintained by absolute date plot(sunspots ~ date, col = tpal(date), data = d[1500:1800, ], cex = 2) ## we can now insert new points and maintain this colour ramp d2 <- data.frame(date = seq(min(d$date), max(d$date), by = "5 days")) d2$sunspots <- approxfun(d$date, d$sunspots)(d2$date) points(sunspots ~ date, col = tpal(date), data = d2, pch = 19, cex = 0.5)
SST example raster data set, at 0.25 degree resolution for global coverage in "longitude180/latitude".
Small sample of 'VIIRS' daily chlorophyll-a 2026-06-15 from near Tasmania.
Created using script in data-raw/ using 'raadtools' package.
Reynolds, et al.(2007) Daily High-resolution Blended Analyses. Available from 'NOAA' search for 'OISST'.
Climatology is based on 1971-2000 OI.v2 SST, Satellite data: Navy NOAA19 METOP AVHRR, Ice data: #' NCEP ice Source: NOAA/National Climatic Data Center.
dim(oisst) class(oisst) image(oisst, useRaster = TRUE) pal <- chl_pal(palette = TRUE) image(t(chl[nrow(chl):1, ]), col = pal$cols[-1], breaks = pal$breaks)dim(oisst) class(oisst) image(oisst, useRaster = TRUE) pal <- chl_pal(palette = TRUE) image(t(chl[nrow(chl):1, ]), col = pal$cols[-1], breaks = pal$breaks)
palr: colours for data
Maintainer: Michael D. Sumner [email protected] [copyright holder]
Authors:
Michael D. Sumner [email protected] [copyright holder]
Other contributors:
Abigael Proctor (Named the package) [contributor]
Tomas Remenyi (Provided colours for element_pal) [contributor]
R Core Team and contributors worldwide (source code of image.default) [contributor]
Useful links:
Report bugs at https://github.com/AustralianAntarcticDivision/palr/issues
SST colours
sst_pal(x, palette = FALSE, alpha = 1, ...) sstPal(x, palette = FALSE, alpha = 1, ...)sst_pal(x, palette = FALSE, alpha = 1, ...) sstPal(x, palette = FALSE, alpha = 1, ...)
x |
a vector of data values or a single number |
palette |
logical, if |
alpha |
value in 0,1 to specify opacity |
... |
currently ignored |
colours, palette, or function, see Details
Derived from a file once found at 'http://oceancolor.gsfc.nasa.gov/DOCS/palette_sst.txt'
data(oisst) sstcols <- sst_pal(palette = TRUE) image(oisst, col = sstcols$cols, zlim = range(sstcols$breaks))data(oisst) sstcols <- sst_pal(palette = TRUE) image(oisst, col = sstcols$cols, zlim = range(sstcols$breaks))
A family of contrast-stretch functions that rescale numeric vectors, matrices, or arrays to the unit interval. These are the data-normalization step that naturally precedes colour mapping with [d_pal()], [image_pal()], or [image_hex()]. Dimensions of the input are preserved, so a stretched matrix can go straight to an image plot.
stretch_linear(x, low = 0.02, high = 0.98, lim = NULL) stretch_log(x, low = 0.02, high = 0.98, offset = NULL, lim = NULL) stretch_sqrt(x, low = 0.02, high = 0.98, offset = NULL, lim = NULL) stretch_histeq(x, n = 256)stretch_linear(x, low = 0.02, high = 0.98, lim = NULL) stretch_log(x, low = 0.02, high = 0.98, offset = NULL, lim = NULL) stretch_sqrt(x, low = 0.02, high = 0.98, offset = NULL, lim = NULL) stretch_histeq(x, n = 256)
x |
numeric vector, matrix, or array (NA values are preserved) |
low, high
|
quantile probabilities for the clip bounds (default 2nd and 98th percentiles), ignored if 'lim' is set |
lim |
optional absolute clip bounds in data units, a range like 'c(0, 3000)' (for 'stretch_log()' and 'stretch_sqrt()' this is given on the original data scale and transformed internally) |
offset |
numeric offset added before the log or sqrt transform. If 'NULL' (default), auto-detected so that all values are non-negative. |
n |
number of bins for histogram equalisation (default 256) |
'stretch_linear()' clips to quantile bounds then linearly rescales, this is the workhorse default.
'stretch_log()' applies 'log1p' after shifting values to be non-negative, then linearly rescales. Useful for data with high dynamic range (e.g. overview-resolution satellite imagery where integer counts span several orders of magnitude).
'stretch_sqrt()' applies square-root after shifting, gentler compression than log, a good middle ground.
'stretch_histeq()' maps values to their empirical quantile rank, producing an approximately uniform distribution. Good for scenes with detail in both deep shadow and bright areas simultaneously.
By default the clip bounds are quantiles of the data itself, so the result is scene-dependent. Use 'lim' to give absolute bounds in data units instead (for example 'lim = c(0, 3000)' for Sentinel-2 surface reflectance), which fixes the mapping so that it is stable across tiles and timesteps (no seams in mosaics, no flicker in animations). When 'lim' is set the quantile arguments are ignored.
Values that stretch outside [0, 1] are clamped. Non-finite input values (NA, NaN) are returned as NA; constant input maps to 0.5.
For multi-band data (e.g. RGB), apply per-band for maximum per-channel contrast, or pool all bands into one vector for a joint stretch that preserves inter-band luminance relationships.
numeric vector, matrix, or array matching the input, with values in [0, 1] (NA where input was not finite)
worked examples here sentinel2_stretch
x <- sort(rnorm(200)) plot(x, stretch_linear(x), pch = ".") plot(x, stretch_sqrt(x), pch = ".") ## high dynamic range data benefits from log stretch x <- c(rexp(500, 0.01), rexp(500, 1)) plot(x, stretch_linear(x), pch = ".", main = "linear") plot(x, stretch_log(x), pch = ".", main = "log") ## histogram equalisation plot(x, stretch_histeq(x), pch = ".", main = "histeq") ## absolute bounds, stable across scenes stretch_linear(c(-100, 0, 1500, 3000, 9000), lim = c(0, 3000)) ## feed into colour mapping plot(1:200, col = d_pal(stretch_linear(sort(rnorm(200)))), pch = 19)x <- sort(rnorm(200)) plot(x, stretch_linear(x), pch = ".") plot(x, stretch_sqrt(x), pch = ".") ## high dynamic range data benefits from log stretch x <- c(rexp(500, 0.01), rexp(500, 1)) plot(x, stretch_linear(x), pch = ".", main = "linear") plot(x, stretch_log(x), pch = ".", main = "log") ## histogram equalisation plot(x, stretch_histeq(x), pch = ".", main = "histeq") ## absolute bounds, stable across scenes stretch_linear(c(-100, 0, 1500, 3000, 9000), lim = c(0, 3000)) ## feed into colour mapping plot(1:200, col = d_pal(stretch_linear(sort(rnorm(200)))), pch = 19)