# 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)
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
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)
Colour vectors can be subset using [
.
To extract one or more colours use positive integers:
r
colour_vector[3]
To drop one or more colours use negative integers:
r
colour_vector[-3]
To move one or more colours extract, drop, and combine:
r
c(colour_vector[-3], colour_vector[3])
Colour palettes can be subset using [
, [[
, and $
.
To extract one or more colour palettes use [
:
r
colour_palette["egypt"]
To extract a single colour palette as a colour vector use [[
or $
:
```r colour_palette[["egypt"]]
colour_palette$egypt ```
To get names of colour palettes use names()
:
r
names(colour_palette)
plot()
is a nice way to showcase colour vectors and colour palettes. The appearance of the plot depends on the input.
Colour vectors are plotted as swatches:
r
plot(colour_vector)
Single colour palettes are plotted as swatches with a palette name overlay:
r
plot(colour_palette["egypt"])
Multiple colour palettes are plotted as faceted swatches with palette name titles:
r
plot(colour_palette)
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.
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.
Print colour vectors compactly:
r
options(
palettes.print_symbol = "circle_large",
palettes.print_hex = FALSE,
palettes.print_width = 5
)
colour_palette
r
options(op)
Mimic the appearance of a character vector:
r
options(
palettes.print_symbol = FALSE,
palettes.print_sep = ", ",
palettes.print_width = 5,
palettes.print_index = TRUE
)
colour_vector
r
options(op)
Set any of these options in your .Rprofile
dotfile to have them persist across R sessions on a global or per-project basis.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.