Learn R Programming

scales (version 1.4.0)

new_continuous_palette: Constructors for palettes

Description

These constructor functions attach metadata to palette functions. This metadata can be used in testing or coercion.

Usage

new_continuous_palette(fun, type, na_safe = NA)

new_discrete_palette(fun, type, nlevels = NA)

is_pal(x)

is_continuous_pal(x)

is_discrete_pal(x)

is_colour_pal(x)

is_numeric_pal(x)

palette_nlevels(pal)

palette_na_safe(pal)

palette_type(pal)

as_discrete_pal(x, ...)

as_continuous_pal(x, ...)

Value

For new_continuous_palette(), new_discret_palette(), as_discrete_pal()

and as_continuous_pal(): a function of class pal_continuous or pal_discrete. For is_pal(), is_continuous_pal(), is_discret_pal(), is_colour_pal(), or is_numeric_pal(): a logical value of length 1. For palette_nlevels() a single integer. For palette_na_safe() a boolean. For palette_type() a string.

Arguments

fun

A function to serve as a palette. For continuous palettes, these typically take vectors of numeric values between (0, 1) and return a vector of equal length. For discrete palettes, these typically take a scalar integer and return a vector of that length.

type

A string giving the type of return values. Some example strings include "colour", "numeric", "linetype" or "shape".

na_safe

A boolean indicating whether NA values are translated to palette values (TRUE) or are kept as NA (FALSE). Applies to continuous palettes.

nlevels

An integer giving the number of distinct palette values that can be returned by the discrete palette.

x

An object to test or coerce.

pal

A palette to retrieve properties from.

...

Additional arguments. Currently not in use.

See Also

palette recommendations

Examples

Run this code
# Creating a new discrete palette
new_discrete_palette(
  fun = grDevices::terrain.colors,
  type = "colour", nlevels = 255
)

# Creating a new continuous palette
new_continuous_palette(
  fun = function(x) rescale(x, to = c(1, 0)),
  type = "numeric", na_safe = FALSE
)

# Testing palette properties
is_continuous_pal(pal_seq_gradient())
is_discrete_pal(pal_viridis())
is_numeric_pal(pal_area())
is_colour_pal(pal_manual(c("red", "green")))
is_pal(transform_log10())

# Extracting properties
palette_nlevels(pal_viridis())
palette_na_safe(colour_ramp(c("red", "green"), na.color = "grey50"))
palette_type(pal_shape())

# Switching discrete to continuous
pal <- as_continuous_pal(pal_viridis())
show_col(pal(c(0, 0.1, 0.2, 0.4, 1)))

# Switching continuous to discrete
pal <- as_discrete_pal(pal_div_gradient())
show_col(pal(9))

Run the code above in your browser using DataLab