build_palette: Build a palette of colours from a list of hex codes

View source: R/plot_tools.R

build_paletteR Documentation

Build a palette of colours from a list of hex codes

Description

build_palette() is a function that takes a vector of hex colours and lets you randomise and return subsets of those colours. All of the other palette_...() functions documented in the Functions section below use pre-compiled lists of hex colours (i.e. the col_list argument is already provided). All functions work identically and return the same kind of data.

Usage

build_palette(col_list, n = NULL, random = FALSE, spaced = FALSE, alpha = NULL)

palette_builtin(n = NULL, random = FALSE, spaced = FALSE, alpha = NULL)

palette_distinct(n = NULL, random = FALSE, spaced = FALSE, alpha = NULL)

palette_mrmrs(n = NULL, random = FALSE, spaced = FALSE, alpha = NULL)

palette_picked(n = NULL, random = FALSE, spaced = FALSE, alpha = NULL)

palette_distant(n = NULL, random = FALSE, spaced = FALSE, alpha = NULL)

Arguments

col_list

(Character) A vector of colours in RGB Hex format without transparency.

n

(Numeric or NULL) The number of colours to return. If NULL, return all of the colours in col_list. If n is greater than the length of the colour list, triggers a warning and returns all of the colours.

random

(Logical) If TRUE, colours will be randomly selected. If FALSE, they will be drawn from col_list in order.

spaced

(Logical) If TRUE, the n chosen colours will be distributed throughout the col_list, e.g. 3 colours from a list of 16 will return 1, 8, and 16th colours. If FALSE (by default), colours will not be spaced out.

alpha

(Numeric) A single decimal value that modifies the opacity of the colours. alpha = 0.65 makes all of the colours 65 percent opaque (35 percent transparent). If alpha = NULL, no alpha channel is added and the colours are 100 percent opaque.

Value

A character vector of hex colours. If alpha = NULL, the colours will be in RGB Hex format (e.g. #FFFF00). If alpha is not NULL, the colours will be in RGBA Hex format (e.g. #FFFF00CB). If the col_list was a named vector, the names will be preserved.

Functions

  • palette_builtin(): R's 502 built-in distinct named colours in hex form (the same ones that would be output by colours(distinct = TRUE). The biggest advantage of accessing R's colours with this function is that you can add transparency to them.

  • palette_distinct(): Tatarize's 1,022 visually-distinct colours (https://stackoverflow.com/a/12224359/5578429). Many of these are not colorblind safe, and many of them have low contrast or are very similar (but should still be different enough to discriminate when they are side-by-side).

  • palette_mrmrs(): Adam Morse's 16 web-safe colours (https://clrs.cc). Nicer replacements for the standard browser colours, with good contrast and readability even when overplotted.

  • palette_picked(): Desi's 14 hand-picked colours from the palette_distinct() range. The palette created by desiderata::palette_distinct() has a lot of colours that are either so dark or so light that it's difficult to differentiate them next to each other. In addition, many of the colours are affected by adjacency effects where putting an intermediate colour between them makes them look the same. I went through the preview plots manually, randomising the order of colours and deleting colours that were visually similar until I ended up with a list of colours that were easy to differentiate.

  • palette_distant(): 48 distant colours from all palettes. This palette is made by converting the hex colours from the other palettes to RGB and HSV, rounding those values to the same range (e.g. rounding RGB values to the nearest 50), and then keeping the unique values only. This is the same way that colours(distinct = TRUE) defines its distinct colours.

Authors

  • R Core Team

Examples

# To see all of the colours (ordered left-to-right and top-to-bottom):
show_colours(palette_builtin())

# To get the first 4 colours:
palette_builtin(4)

#>     white     aliceblue  antiquewhite    antiquewhite1
#> "#FFFFFF"     "#F0F8FF"     "#FAEBD7"        "#FFEFDB"

# To pick 4 colours randomly:
palette_builtin(4, random = TRUE)

#>    gray52       coral4    darkorchid2      orchid4
#> "#858585"    "#8B3E2F"      "#B23AEE"    "#8B4789"

# To pick 4 colours distributed evenly throughout the colour list:
palette_builtin(4, spaced = TRUE)

#>     white        gray32    mediumpurple3       yellow4
#> "#FFFFFF"     "#525252"        "#8968CD"     "#8B8B00"

# To make the colours 75 percent opaque (note that all args can work together):
palette_builtin(4, random = TRUE, spaced = TRUE, alpha = 0.75)

#>      gray35          gray7          plum2     peachpuff3
#> "#595959BF"    "#121212BF"    "#EEAEEEBF"    "#CDAF95BF"

# ------------

# To use your own colour list, use build_palette():
build_palette(c("#000000", "#FFFF00", "#1CE6FF", "#FF34FF", "#FF4A46"), alpha = 0.5)

#> [1] "#00000080" "#FFFF0080" "#1CE6FF80" "#FF34FF80" "#FF4A4680"


DesiQuintans/desiderata documentation built on April 9, 2023, 5:43 a.m.