palettes

# Preview vignette with: devtools::build_rmd("vignettes/palettes.Rmd")
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

The goal of palettes is to provide methods for working with colour palettes for users and developers.

library(palettes)

Creating

pal_colour() is a nice way to create a colour vector. Colours can be a character vector of hexadecimal strings of the form "#RRGGBB" or "#RRGGBBAA", colour names from grDevices::colors(), or a positive integer that indexes into grDevices::palette(). By default, colour vectors are always printed as hex codes with colour previews.

colour_vector <- pal_colour(
  c("#a00e00", "#d04e00", "#f6c200", "#0086a8", "#132b69")
)

colour_vector

pal_palette() is a nice way to create named colour palettes.

colour_palette <- pal_palette(
  egypt = c("#dd5129", "#0f7ba2", "#43b284", "#fab255"),
  java  = c("#663171", "#cf3a36", "#ea7428", "#e2998a", "#0c7156")
)

colour_palette

Casting and coercion

To compliment pal_colour(), palettes provides as_colour() to cast objects into colour vectors.

colour_strings <- c("orange", "purple")
as_colour(colour_strings)

To compliment pal_palette(), palettes provides as_palette() to cast objects into colour palettes.

colour_list <- list(OrPu = c("orange", "purple"))
as_palette(colour_list)

Colour vectors and colour palettes can also be coerced into a tibble with as_tibble(). See vignette("tibble", package = "tibble") for an overview of tibbles.

as_tibble(colour_vector)

as_tibble(colour_palette)

Subsetting

Colour vectors can be subset using [.

Colour palettes can be subset using [, [[, and $.

Plotting

plot() is a nice way to showcase colour vectors and colour palettes. The appearance of the plot depends on the input.

To interpolate or change the direction of colours in a plot, use the optional n, direction, space, or interpolate arguments.

plot(colour_vector, n = 7, direction = -1, interpolate = "linear")

All plots are ggplot2 objects and can be customized using any of the standard ggplot2 methods. See the ggplot2 customizing FAQ for some common examples.

Printing

op <- options()

The printing behaviour of colour vectors can be adjusted using a variety of global options. See help("palettes-options") for a list of all the available options and their default values.

For example, to change the symbol used for colour previews, set the palettes.print_symbol option.

options(palettes.print_symbol = "square")
colour_vector
options(op)

Set multiple options together for unique printing styles.

Set any of these options in your .Rprofile dotfile to have them persist across R sessions on a global or per-project basis.



Try the palettes package in your browser

Any scripts or data that you put into this service are public.

palettes documentation built on Sept. 11, 2024, 5:57 p.m.