build_palette | R Documentation |
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.
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)
col_list |
(Character) A vector of colours in RGB Hex format without transparency. |
n |
(Numeric or |
random |
(Logical) If |
spaced |
(Logical) If |
alpha |
(Numeric) A single decimal value that modifies the opacity of the colours.
|
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.
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.
Desi Quintans (http://www.desiquintans.com)
R Core Team
Adam Morse (http://mrmrs.cc/)
# 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"
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.