Title: | Methods for Colour Vectors and Colour Palettes |
---|---|
Description: | Provides a comprehensive library for colour vectors and colour palettes using a new family of colour classes (palettes_colour and palettes_palette) that always print as hex codes with colour previews. Capabilities include: formatting, casting and coercion, extraction and updating of components, plotting, colour mixing arithmetic, and colour interpolation. |
Authors: | Michael McCarthy [aut, cre, cph] |
Maintainer: | Michael McCarthy <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.1.9000 |
Built: | 2024-12-17 05:13:19 UTC |
Source: | https://github.com/mccarthy-m-g/palettes |
as_tibble()
turns an existing colour vector or colour palette into a
so-called tibble, a data frame with class tbl_df
.
## S3 method for class 'palettes_colour' as_tibble(x, ...) ## S3 method for class 'palettes_palette' as_tibble(x, ...)
## S3 method for class 'palettes_colour' as_tibble(x, ...) ## S3 method for class 'palettes_palette' as_tibble(x, ...)
x |
An object of class |
... |
Not used. |
A tibble. The output has the following properties:
For objects of class palettes_colour
: A tibble with
column colour
containing the colour vector.
For objects of class palettes_palette
: A tibble with
columns palette
and colour
containing palette names and colour vectors.
x <- pal_colour(c("#663171", "#EA7428", "#0C7156")) as_tibble(x) y <- pal_palette( Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"), Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156") ) as_tibble(y)
x <- pal_colour(c("#663171", "#EA7428", "#0C7156")) as_tibble(x) y <- pal_palette( Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"), Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156") ) as_tibble(y)
Discrete, sequential, and diverging palettes created by cartographers.
discrete_palettes sequential_palettes diverging_palettes
discrete_palettes sequential_palettes diverging_palettes
discrete_palettes
An object of class palettes_palette
with 7
colour palettes. Use names(discrete_palettes)
to return all palette names.
sequential_palettes
An object of class palettes_palette
with 19
colour palettes. Use names(sequential_palettes)
to return all palette names.
diverging_palettes
An object of class palettes_palette
with 7
colour palettes. Use names(diverging_palettes)
to return all palette names.
https://github.com/CartoDB/CartoColor/
# Get all palettes by name. names(discrete_palettes) names(sequential_palettes) names(diverging_palettes) # Plot all palettes. plot(discrete_palettes) plot(sequential_palettes) plot(diverging_palettes)
# Get all palettes by name. names(discrete_palettes) names(sequential_palettes) names(diverging_palettes) # Plot all palettes. plot(discrete_palettes) plot(sequential_palettes) plot(diverging_palettes)
These binary operators mix colour vectors with arithmetic operators.
## S3 method for class 'palettes_colour' e1 + e2
## S3 method for class 'palettes_colour' e1 + e2
e1 , e2
|
Colour vectors of class |
The binary operators return colour vectors of class
palettes_colour
containing the result of the element by
element operations. If involving a zero-length vector the result has length
zero. Otherwise, the elements of shorter vectors are recycled as necessary.
The +
operator is for additive colour mixing.
x <- pal_colour("red") y <- pal_colour("blue") x + y
x <- pal_colour("red") y <- pal_colour("blue") x + y
These functions mix colour vectors with math functions.
## S3 method for class 'palettes_colour' sum(..., na.rm = FALSE) ## S3 method for class 'palettes_colour' cumsum(x)
## S3 method for class 'palettes_colour' sum(..., na.rm = FALSE) ## S3 method for class 'palettes_colour' cumsum(x)
... |
Colour vectors of class |
na.rm |
Whether to include missing values. Either |
x |
An object of class |
These functions return colour vectors of class
palettes_colour
:
sum()
returns the sum of all the colours present in its arguments with
additive colour mixing.
cumsum()
returns a vector whose elements are the cumulative sums of the
elements of the argument with additive colour mixing.
x <- pal_colour(c("red", "blue")) sum(x) x <- pal_colour(c("red", "blue", "yellow")) cumsum(x)
x <- pal_colour(c("red", "blue")) sum(x) x <- pal_colour(c("red", "blue", "yellow")) cumsum(x)
List the symbols available to use in colour previews.
list_colour_symbols()
list_colour_symbols()
By default, Unicode characters are used for symbols in colour previews in UTF-8 supported outputs. They automatically fall back to ASCII characters when the output does not support them.
To change the symbol used for colour previews, set the palettes.print_symbol
option to a symbol name listed in list_colour_symbols()
.
This function is called for its side effects and has no return value.
help("palettes-options")
, cli::is_utf8_output()
list_colour_symbols()
list_colour_symbols()
Palettes inspired by works at the Metropolitan Museum of Art in New York. Pieces selected come from various time periods, regions, and mediums.
met_palettes met_palettes_a11y
met_palettes met_palettes_a11y
met_palettes
An object of class palettes_palette
with 56 colour
palettes. Use names(met_palettes)
to return all palette names.
met_palettes_a11y
An object of class palettes_palette
limited to
24 colourblind accessible palettes. All colours
in each palette are distinguishable with deuteranopia, protanopia, and
tritanopia. Use names(met_palettes_a11y)
to return all palette names.
https://github.com/BlakeRMills/MetBrewer
pal_palette()
, pal_colour()
, MetBrewer::met.brewer()
# Get all palettes by name. names(met_palettes) # Plot all palettes. plot(met_palettes)
# Get all palettes by name. names(met_palettes) # Plot all palettes. plot(met_palettes)
Dimmed pastel palettes inspired by the Arctic and Canadian wilderness.
nord_palettes
nord_palettes
nord_palettes
An object of class palettes_palette
with 16
colour palettes. Use names(nord_palettes)
to return all palette names.
https://github.com/jkaupp/nord
pal_palette()
, pal_colour()
, nord::nord()
# Get all palettes by name. names(nord_palettes) # Plot all palettes. plot(nord_palettes)
# Get all palettes by name. names(nord_palettes) # Plot all palettes. plot(nord_palettes)
This creates a character vector that represents colours so when it is printed, colours will be formatted as hexadecimal strings.
pal_colour(x = character()) is_colour(x) as_colour(x) ## Default S3 method: as_colour(x) ## S3 method for class 'palettes_palette' as_colour(x)
pal_colour(x = character()) is_colour(x) as_colour(x) ## Default S3 method: as_colour(x) ## S3 method for class 'palettes_palette' as_colour(x)
x |
|
Colours can be specified using either:
Hexadecimal strings of the form "#RRGGBB"
or "#RRGGBBAA"
Colour names from grDevices::colors()
Positive integers i
that index into grDevices::palette()
[i]
An S3 vector of class palettes_colour
.
pal_colour(c("darkred", "#0F7BA2")) is_colour("darkred") is_colour(pal_colour("darkred")) as_colour("#0F7BA2")
pal_colour(c("darkred", "#0F7BA2")) is_colour("darkred") is_colour(pal_colour("darkred")) as_colour("#0F7BA2")
Conveniently maps data values (numeric or factor/character) to colours according to a given colour vector or colour palette.
pal_numeric( palette, domain, na.color = "#808080", alpha = FALSE, reverse = FALSE ) pal_bin( palette, domain, bins = 7, pretty = TRUE, na.color = "#808080", alpha = FALSE, reverse = FALSE, right = FALSE ) pal_quantile( palette, domain, n = 4, probs = seq(0, 1, length.out = n + 1), na.color = "#808080", alpha = FALSE, reverse = FALSE, right = FALSE ) pal_factor( palette, domain, levels = NULL, ordered = FALSE, na.color = "#808080", alpha = FALSE, reverse = FALSE )
pal_numeric( palette, domain, na.color = "#808080", alpha = FALSE, reverse = FALSE ) pal_bin( palette, domain, bins = 7, pretty = TRUE, na.color = "#808080", alpha = FALSE, reverse = FALSE, right = FALSE ) pal_quantile( palette, domain, n = 4, probs = seq(0, 1, length.out = n + 1), na.color = "#808080", alpha = FALSE, reverse = FALSE, right = FALSE ) pal_factor( palette, domain, levels = NULL, ordered = FALSE, na.color = "#808080", alpha = FALSE, reverse = FALSE )
palette |
An object of class |
domain |
The possible values that can be mapped. For If |
na.color |
The colour to return for |
alpha |
Whether alpha channels should be respected or ignored. If |
reverse |
Whether the colours in |
bins |
Either a numeric vector of two or more unique cut points or a single number (greater than or equal to 2) giving the number of intervals into which the domain values are to be cut. |
pretty |
Whether to use the function |
right |
parameter supplied to |
n |
Number of equal-size quantiles desired. For more precise control,
use the |
probs |
See |
levels |
An alternate way of specifying levels; if specified, domain is ignored |
ordered |
If |
pal_numeric
is a simple linear mapping from continuous numeric
data to an interpolated palette.
pal_bin
also maps continuous numeric data, but performs
binning based on value (see the base::cut()
function). pal_bin
defaults for the cut
function are include.lowest = TRUE
and
right = FALSE
.
pal_quantile
similarly bins numeric data, but via the
stats::quantile()
function.
pal_factor
maps factors to colours. If the palette is
discrete and has a different number of colours than the number of factors,
interpolation is used.
A function that takes a single parameter x
; when called with a
vector of numbers (except for pal_factor
, which expects
factors/characters), #RRGGBB colour strings are returned (unless
alpha = TRUE
in which case #RRGGBBAA may also be possible).
pal <- pal_bin(met_palettes$Tam, domain = 0:100) plot(as_colour(pal(sort(runif(16, 0, 100))))) # Exponential distribution, mapped continuously pal <- pal_numeric(met_palettes$Tam, domain = NULL) plot(as_colour(pal(sort(rexp(16))))) # Exponential distribution, mapped by interval pal <- pal_bin(met_palettes$Tam, domain = NULL, bins = 4) plot(as_colour(pal(sort(rexp(16))))) # Exponential distribution, mapped by quantile pal <- pal_quantile(met_palettes$Tam, domain = NULL) plot(as_colour(pal(sort(rexp(16))))) # Categorical data; by default, the values being coloured span the gamut... pal <- pal_factor(met_palettes$Java, domain = NULL) plot(as_colour(pal(LETTERS[1:5]))) # ...unless the data is a factor, without droplevels... pal <- pal_factor(met_palettes$Java, domain = NULL) plot(as_colour(pal(factor(LETTERS[1:5], levels = LETTERS)))) # ...or the domain is stated explicitly. pal <- pal_factor(met_palettes$Java, domain = NULL, levels = LETTERS) plot(as_colour(pal(LETTERS[1:5])))
pal <- pal_bin(met_palettes$Tam, domain = 0:100) plot(as_colour(pal(sort(runif(16, 0, 100))))) # Exponential distribution, mapped continuously pal <- pal_numeric(met_palettes$Tam, domain = NULL) plot(as_colour(pal(sort(rexp(16))))) # Exponential distribution, mapped by interval pal <- pal_bin(met_palettes$Tam, domain = NULL, bins = 4) plot(as_colour(pal(sort(rexp(16))))) # Exponential distribution, mapped by quantile pal <- pal_quantile(met_palettes$Tam, domain = NULL) plot(as_colour(pal(sort(rexp(16))))) # Categorical data; by default, the values being coloured span the gamut... pal <- pal_factor(met_palettes$Java, domain = NULL) plot(as_colour(pal(LETTERS[1:5]))) # ...unless the data is a factor, without droplevels... pal <- pal_factor(met_palettes$Java, domain = NULL) plot(as_colour(pal(factor(LETTERS[1:5], levels = LETTERS)))) # ...or the domain is stated explicitly. pal <- pal_factor(met_palettes$Java, domain = NULL, levels = LETTERS) plot(as_colour(pal(LETTERS[1:5])))
This creates a list of colour vectors.
pal_palette(...) is_palette(x) as_palette(x)
pal_palette(...) is_palette(x) as_palette(x)
... |
|
x |
|
Colours can be specified using either:
Hexadecimal strings of the form "#RRGGBB"
or "#RRGGBBAA"
Colour names from grDevices::colors()
Positive integers i
that index into grDevices::palette()
[i]
An S3 list of class palettes_palette
.
pal_palette( Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"), Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156") ) x <- list( Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"), Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156") ) as_palette(x)
pal_palette( Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"), Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156") ) x <- list( Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"), Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156") ) as_palette(x)
Interpolate the set of colours in palettes_palette
or
palettes_colour
objects to create new colour palettes.
pal_ramp( palette, n = NULL, direction = 1, space = "lab", interpolate = c("linear", "spline") ) ## S3 method for class 'palettes_colour' pal_ramp( palette, n = NULL, direction = 1, space = "lab", interpolate = c("linear", "spline") ) ## S3 method for class 'palettes_palette' pal_ramp( palette, n = NULL, direction = 1, space = "lab", interpolate = c("linear", "spline") )
pal_ramp( palette, n = NULL, direction = 1, space = "lab", interpolate = c("linear", "spline") ) ## S3 method for class 'palettes_colour' pal_ramp( palette, n = NULL, direction = 1, space = "lab", interpolate = c("linear", "spline") ) ## S3 method for class 'palettes_palette' pal_ramp( palette, n = NULL, direction = 1, space = "lab", interpolate = c("linear", "spline") )
palette |
An object of class |
n |
An integer specifying the number of colours to return. |
direction |
Sets the order of colours in the scale. If 1, the default, colours are ordered from first to last. If -1, the order of colours is reversed. |
space |
The colour space to interpolate in. One of: |
interpolate |
The interpolation method. Either "linear" (default) or "spline". |
An object of the same type as palette
. The output has the following properties:
For objects of class palettes_colour
: A colour vector
with n
colours.
For objects of class palettes_palette
: Colour palettes
with n
colours in each palette.
# The class returned after interpolation matches the input class. x <- pal_colour(c("darkslateblue", "cornflowerblue", "slategray1")) y <- pal_palette(blues = x) class(pal_ramp(x)) class(pal_ramp(y)) # Choose between linear and spline interpolation. pal_ramp(x, n = 7, interpolate = "linear") pal_ramp(x, n = 7, interpolate = "spline") # Palettes will have the same length after interpolation, regardless of the # number of colours in the original palette. z <- pal_palette( Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"), Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156") ) pal_ramp(z, n = 5)
# The class returned after interpolation matches the input class. x <- pal_colour(c("darkslateblue", "cornflowerblue", "slategray1")) y <- pal_palette(blues = x) class(pal_ramp(x)) class(pal_ramp(y)) # Choose between linear and spline interpolation. pal_ramp(x, n = 7, interpolate = "linear") pal_ramp(x, n = 7, interpolate = "spline") # Palettes will have the same length after interpolation, regardless of the # number of colours in the original palette. z <- pal_palette( Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"), Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156") ) pal_ramp(z, n = 5)
Options that adjust the behaviour of the palettes package.
These options can be set via options()
and queried via getOption()
.
palettes.print_symbol
:Character string setting the symbol used for colour previews. See
list_colour_symbols()
for a list of symbol choices. Defaults to
"circle_small"
. Set to FALSE
to disable printing symbols.
palettes.print_hex
:Logical setting whether to print hex codes in colour previews. Defaults to
TRUE
.
palettes.print_alpha
:Logical setting whether to print the hex code alpha channel in colour
previews. Defaults to FALSE
. Colours without an alpha channel will be
assumed to be full opacity.
palettes.print_sep
:Character string to separate colours by in colour previews. Defaults to ""
.
palettes.print_width
:Integer setting the maximum number of colours on a line in colour previews.
Defaults to 1
.
palettes.print_index
:Logical setting whether to print the index of the first colour on each line
in colour previews. Defaults to FALSE
.
To disable formatting in colour previews set both palettes.print_symbol
and
palettes.print_hex
to FALSE
.
options( palettes.print_symbol = "square", palettes.print_hex = FALSE, palettes.print_sep = " ", palettes.print_width = 3, palettes.print_index = TRUE ) met_palettes$Cross
options( palettes.print_symbol = "square", palettes.print_hex = FALSE, palettes.print_sep = " ", palettes.print_width = 3, palettes.print_index = TRUE ) met_palettes$Cross
Palettes inspired by the Palmer penguins.
penguin_palettes
penguin_palettes
penguin_palettes
An object of class palettes_palette
with 6
colour palettes. Use names(penguin_palettes)
to return all palette names.
https://github.com/allisonhorst/palmerpenguins/
# Get all palettes by name. names(penguin_palettes) # Plot all palettes. plot(penguin_palettes)
# Get all palettes by name. names(penguin_palettes) # Plot all palettes. plot(penguin_palettes)
A colourblind accessible palette for visualizing performance.
performance_palettes
performance_palettes
performance_palettes
An object of class palettes_palette
with 1
colour palettes. All colours in each palette are distinguishable with
deuteranopia, protanopia, and tritanopia. Use names(performance_palettes)
to return all palette names.
https://github.com/easystats/performance/
# Get all palettes by name. names(performance_palettes) # Plot all palettes. plot(performance_palettes)
# Get all palettes by name. names(performance_palettes) # Plot all palettes. plot(performance_palettes)
Plot colour vectors and colour palettes as swatches.
## S3 method for class 'palettes_colour' plot( x, n = NULL, direction = 1, space = "lab", interpolate = c("linear", "spline"), ... ) ## S3 method for class 'palettes_palette' plot( x, n = NULL, direction = 1, space = "lab", interpolate = c("linear", "spline"), ... )
## S3 method for class 'palettes_colour' plot( x, n = NULL, direction = 1, space = "lab", interpolate = c("linear", "spline"), ... ) ## S3 method for class 'palettes_palette' plot( x, n = NULL, direction = 1, space = "lab", interpolate = c("linear", "spline"), ... )
x |
An object of class |
n |
An integer specifying the number of colours to return. |
direction |
Sets the order of colours in the scale. If 1, the default, colours are ordered from first to last. If -1, the order of colours is reversed. |
space |
The colour space to interpolate in. One of: |
interpolate |
The interpolation method. Either "linear" (default) or "spline". |
... |
Not used. |
A ggplot2 object. The output has the following properties:
For objects of class palettes_colour
: A plot of colour
swatches.
For objects of class palettes_palette
with one palette:
A plot of colour swatches with the palette name spanned across the swatches.
For objects of class palettes_palette
with more than one
palette: A faceted plot of colour swatches with palette names as facet
titles.
pal_colour()
, pal_palette()
, pal_ramp()
# Objects of class `palettes_colour` are plotted as swatches. x <- pal_colour(c("darkslateblue", "cornflowerblue", "slategray1")) plot(x) # Objects of class `palettes_palette` with one palette are plotted with # the palette name spanned across the swatches. y <- pal_palette(Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255")) plot(y) # Objects of class `palettes_palette` with multiple palettes are faceted. z <- pal_palette( Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"), Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156") ) plot(z) # Colours can also be interpolated. plot(x, n = 5) plot(y, n = 5) plot(z, n = 5)
# Objects of class `palettes_colour` are plotted as swatches. x <- pal_colour(c("darkslateblue", "cornflowerblue", "slategray1")) plot(x) # Objects of class `palettes_palette` with one palette are plotted with # the palette name spanned across the swatches. y <- pal_palette(Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255")) plot(y) # Objects of class `palettes_palette` with multiple palettes are faceted. z <- pal_palette( Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"), Java = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156") ) plot(z) # Colours can also be interpolated. plot(x, n = 5) plot(y, n = 5) plot(z, n = 5)
Palettes inspired by Jake Lawlor's photos of the dreamiest, most colourful, PNW-iest places in Washington State.
pnw_palettes
pnw_palettes
pnw_palettes
An object of class palettes_palette
with 14 colour
palettes. Use names(pnw_palettes)
to return all palette names.
https://github.com/jakelawlor/PNWColors
pal_palette()
, pal_colour()
, PNWColors::pnw_palette()
# Get all palettes by name. names(pnw_palettes) # Plot all palettes. plot(pnw_palettes)
# Get all palettes by name. names(pnw_palettes) # Plot all palettes. plot(pnw_palettes)
Create discrete, continuous, and binned colour scales from colour vectors and colour palettes.
scale_colour_palette_d(palette, direction = 1, ...) scale_fill_palette_d(palette, direction = 1, ...) scale_colour_palette_c(palette, direction = 1, ...) scale_fill_palette_c(palette, direction = 1, ...) scale_colour_palette_b(palette, direction = 1, ...) scale_fill_palette_b(palette, direction = 1, ...)
scale_colour_palette_d(palette, direction = 1, ...) scale_fill_palette_d(palette, direction = 1, ...) scale_colour_palette_c(palette, direction = 1, ...) scale_fill_palette_c(palette, direction = 1, ...) scale_colour_palette_b(palette, direction = 1, ...) scale_fill_palette_b(palette, direction = 1, ...)
palette |
An object of class |
direction |
Sets the order of colours in the scale. If 1, the default, colours are ordered from first to last. If -1, the order of colours is reversed. |
... |
Other arguments passed on to |
A scale function that controls the mapping between data and colour or fill aesthetics in a ggplot2 plot.
library(ggplot2) # Use palette_d with discrete data discrete_pal <- pal_colour(c("#663171", "#EA7428", "#0C7156")) ggplot(mtcars, aes(wt, mpg, colour = as.factor(cyl))) + geom_point(size = 3) + scale_colour_palette_d(discrete_pal) # Use palette_c with continuous data continuous_pal <- pal_colour(c("#3C0D03", "#E67424", "#F5C34D")) ggplot(mtcars, aes(wt, mpg, colour = mpg)) + geom_point(size = 3) + scale_colour_palette_c(continuous_pal) # Use palette_b to bin continuous data before mapping ggplot(mtcars, aes(wt, mpg, colour = mpg)) + geom_point(size = 3) + scale_colour_palette_b(continuous_pal)
library(ggplot2) # Use palette_d with discrete data discrete_pal <- pal_colour(c("#663171", "#EA7428", "#0C7156")) ggplot(mtcars, aes(wt, mpg, colour = as.factor(cyl))) + geom_point(size = 3) + scale_colour_palette_d(discrete_pal) # Use palette_c with continuous data continuous_pal <- pal_colour(c("#3C0D03", "#E67424", "#F5C34D")) ggplot(mtcars, aes(wt, mpg, colour = mpg)) + geom_point(size = 3) + scale_colour_palette_c(continuous_pal) # Use palette_b to bin continuous data before mapping ggplot(mtcars, aes(wt, mpg, colour = mpg)) + geom_point(size = 3) + scale_colour_palette_b(continuous_pal)
Colourblind accessible palettes that are perceptually uniform in both colour and black-and-white.
viridis_palettes
viridis_palettes
viridis_palettes
An object of class palettes_palette
with 8
colour palettes. All colours in each palette are distinguishable with
deuteranopia, protanopia, and tritanopia. Use names(viridis_palettes)
to
return all palette names.
https://github.com/sjmgarnier/viridisLite
pal_palette()
, pal_colour()
, viridisLite::viridis()
# Get all palettes by name. names(viridis_palettes) # Plot all palettes. plot(viridis_palettes, n = 256)
# Get all palettes by name. names(viridis_palettes) # Plot all palettes. plot(viridis_palettes, n = 256)