Colors and color functions


2020 12 18


This vignette explains the colors, color palettes, and color-related functions provided by the unikn package. (See the vignettes on color recipes and institutional colors for more specialized tasks and the vignette on text for information on text boxes and decorations.)

Please install and/or load the unikn package to get started:

# install.packages('unikn')  # install unikn from CRAN client
library('unikn')             # loads the package


The unikn package provides some colors (e.g., Seeblau) and color palettes (e.g., pal_unikn). However, its functionality mainly comes from color-related functions that are useful beyond the dedicated colors of this package.

The package provides two main functions for interacting with color palettes: seecol() and usecol().

  1. seecol() is a general-purpose tool for viewing (or seeing) color palettes. The seecol() function takes two main arguments:

    1. pal provides either one or multiple color palettes (with a default of pal = "all");
    2. n specifies the number of desired colors (with a default of n = "all").

Based on the setting of pal, the seecol() function distinguishes between two modes:

* _comparing_ a set of multiple color palettes (by passing a keyword or `list`-object);  
* viewing the _details_ of a single color palette (by providing only one palette).   
  1. usecol() allows using a color palette (e.g., when creating visualizations) without showing its details. The usecol() function also takes arguments for conveniently manipulating color palettes:

    1. pal provides either one or multiple color palettes (with a default of pal = pal_unikn);
    2. n specifies the number of desired colors (with a default of n = "all");
    3. alpha adjusts the opacity of all colors in pal (e.g., alpha = .50 for medium transparency).

Two additional functions serve in auxiliary roles:

  1. newpal() allows defining new color palettes (typically as data frames with dedicated color names).

  2. grepal() searches a color palette x (i.e., a vector of color names or data frame of named colors) for a pattern and returns those that match the pattern (or regular expression).

Viewing colors and color palettes with seecol()

The behavior of the seecol() function depends on the inputs to its pal argument. It either allows comparing multiple color palettes or shows the details of a single color palette.

Viewing and comparing available color palettes

When the pal argument specifies (a list of) multiple color palettes, seecol() plots a vector for each palette to allow comparing these palettes. Some special keywords within the unikn package denote sets of color palettes: "unikn_all", "unikn_basic", pair_all", "pref_all" and "grad_all". Calling seecol with pal set to these keywords allows comparing pre-defined sets of the color palettes:

Viewing all available color palettes:

  1. three basic color palettes:

Note, that pal_unikn_web and pal_unikn_ppt are almost identical, but differ in how vibrant their colors are.

  1. three paired color palettes:

  1. all preferred colors from the spectrum and their respective gradients:

  1. only the pre-defined color gradients:

Viewing and saving a palette

When the pal argument of the seecol() function specifies a single color palette, the function plots a more detailed view of this particular color palette:

The detailed overview of a single color palette provides us with

A typical workflow comprises first seeing a color palette, then saving it, and using it in a plot.

After saving the color palette you can use the palette object in your plot:

Note that seecol() invisibly returns the color palette.
Thus, the following will only plot the palette without doing anything else with the color vector:

Other parameters of seecol()

The seecol() function provides some aesthetic parameters for adjusting how color palettes are plotted:


Using a color palette with usecol() (without seeing it)

The usecol() function allows directly using a color palette in a plot (i.e., without first viewing it). usecol() corresponds to seecol() by taking the same main arguments (pal and n). However, as its purpose is using the colors specified by pal, rather than plotting (or seeing) them, its pal argument typically contains only one color palette:

# Using a color palette:
barplot(1/sqrt(1:11), col = usecol(pal_unikn))

Note that the seecol() and usecol() functions are both quite permissive with respect to specifying their pal argument: A particular color palette (e.g., pal_seegruen) can not only be displayed by providing it (as an object) but also by providing its name (i.e., "pal_seegruen") or even an incomplete object name or name (i.e., "seegruen" or seegruen). Hence, the following expressions all yield the same result:

seecol(seegruen)  # issues a warning, but works 

Customizing color palettes

Both the seecol() and the usecol() functions allow flexible on-the-fly customizations of color palettes.

Specifying a value for the n argument of seecol() an usecol() allows:

Passing a vector of colors and/or color palettes allows users to create and view their own color palettes.

Finally, specifying a value for alpha (in a range from 0 to 1) allows controlling the transparency of the color palette(s), with higher values for alpha corresponding to higher transparency (i.e., lower opacity).

Selecting subsets

Using only a subset of colors:

Importantly, when using pre-defined color palettes of unikn but a value of n that is smaller than the length of the current color palette, usecol and seecol select a predefined subset of colors:

Extending color palettes

For values of n that are larger than the number of available colors in pal, the specified color palette is extended using ColorRampPalette:

When using a color palette:

Mixing color palettes

By passing a vector to pal, we can concatenate 2 color palettes and connect them with a color (here: "white") as the midpoint of a new color palette:

We can combine a set of colors and extend this palette by specifying an n argument that is larger than the length of the specified palette:

These custom palettes can easily be used in a plot. For instance, we can define and use a subset of the pal_unikn_pair palette as follows:

Controlling transparency

Both seecol() and usecol() accept an alpha argument (in a range from 0 to 1) for controlling the transparency of color palettes, with higher values for alpha corresponding to lower transparency (i.e., higher opacity).

Displaying a specific color palette at a medium opacity/transparency:

Setting opacity for a custom color palette:

Setting opacity for comparing of multiple color palettes:

Creating and comparing custom palettes

Suppose we want to compare a newly created color palette to existing color palettes. To achieve this, advanced users can use the seecol() function for displaying and comparing different custom palettes. When provided with a list of color palettes as the input to its pal argument, seecol() will show a comparison of the inputs:

Note that unknown color palettes are named pal_\(n\), in increasing order. Palettes known to seecol() are labeled by their respective names.

Labeling only custom palettes works by setting the pal_names argument to a character vector of appropriate length:

If the pal_names argument is specified and corresponds to the length of all color palettes, the default names of all color palettes are overwritten by pal_names:

As before, we can use the n argument for obtaining shorter subsets of color palettes:

or larger values of n for extending color palettes:

Defining new color palettes with newpal()

The newpal() function makes it easy to define new named color palettes:

col_flag <- c("#000000", "#dd0000", "#ffce00")  # source:

flag_de  <- newpal(col = col_flag,
                   names = c("black", "red", "gold"))

seecol(flag_de, title = "Defining a flag_de color palette")

By default, newpal() returns the new color palette as a data frame. Setting as_df = FALSE returns a (named) vector.

Searching for color names with grepal()

A common situation in R is that we want some particular shade of color (e.g., some sort of red), but also know that the color named “red” is not the one we want. Instead, we would like to see all colors that contain “red” in its name. This is what grepal() is made for:

grepal("red")  # get 27 names of colors() with "red" in their name
#>  [1] "darkred"         "indianred"       "indianred1"      "indianred2"     
#>  [5] "indianred3"      "indianred4"      "mediumvioletred" "orangered"      
#>  [9] "orangered1"      "orangered2"      "orangered3"      "orangered4"     
#> [13] "palevioletred"   "palevioletred1"  "palevioletred2"  "palevioletred3" 
#> [17] "palevioletred4"  "red"             "red1"            "red2"           
#> [21] "red3"            "red4"            "violetred"       "violetred1"     
#> [25] "violetred2"      "violetred3"      "violetred4"

seecol(grepal("red"))  # see all colors() with "red" in their name