Colors and color palettes


2019 09 24


This vignette explains which colors and color palettes are provided by unikn and how they can be accessed and used. (See 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')             # load the package


There are 2 main functions to interact with the color palettes in the unikn package: seecol() and usecol().

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

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

Based on the setting of pal, the seecol function distinguishes between 2 modes:

  1. The usecol() function allows using a color palette without showing its details.

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:

seecol("unikn_all")  # same as seecol("all")

  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:

seecol(pal_unikn)  # view details of pal_unikn 

In the detailed overview, we see

A typical workflow may include seeing a color palette, saving it, and using it in a plot.

my_pal <- seecol(pal_unikn_light)  # view details of AND save pal_unikn_light to my_pal  

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

barplot(1/sqrt(1:10), col = my_pal)  # use my_pal in a 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 a few aesthetic parameters for adjusting how color palettes are plotted:


seecol("grad_all", col_brd = "black", lwd_brd = 2, title = "Color gradients with black borders")

seecol(pal_seegruen, col_brd = "white", lwd_brd = 10, title = "A color palette with white borders")

Using a 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 2 main arguments (pal and n). However, as its purpose is using the colors specified by pal, rather than plotting (or seeing) them, the pal argument typically contains only 1 color palette:

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

Note that the both the seecol and the usecol function are 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 its incomplete object name or name (i.e., "seegruen" or seegruen). Hence, the following all yield the same result:

seecol(seegruen)  # issues a warning

Customizing color palettes

Both the seecol() and the usecol() functions allow a flexible on-the-fly customization 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 you creating and viewing your own 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:

seecol("unikn_all", n = 4)

seecol(pal_unikn, 4)

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:

barplot(1/sqrt(1:2), col = usecol(pal_seeblau, n = 2))
barplot(1/sqrt(1:3), col = usecol(pal_seeblau, n = 3))

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:

seecol("all", n = 12)

seecol(pal_seeblau, 12)

When using a color palette:

barplot(1/sqrt(1:11), col = usecol(pal_bordeaux, n = 11))

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:

seecol(pal = c(rev(pal_petrol), "white", pal_bordeaux))

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:

seecol(pal = usecol(c(Karpfenblau, Seeblau, "gold"), n = 10))

# Note, that redundant use of seecol and usecol shows HEX values as names.
# seecol(pal = c(Karpfenblau, Seeblau, "gold"), n = 10)  # would work, but show no intermediate names

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:

my_pair <- seecol(pal_unikn_pair, n = 10)

# Create data: 
dat <- matrix(sample(5:10, size = 10, replace = TRUE), ncol = 5)

# Plot in my_pair colors:
barplot(dat, beside = TRUE, col = my_pair)

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 higher transparency (i.e., lower opacity).

Displaying a specific color palette at an opacity of 0.5:

seecol(pal_unikn, alpha = 0.5)

Setting opacity for comparing of multiple color palettes:

seecol("grad", alpha = 0.67)

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:

# Define 2 palettes: 
pal1 <- c(rev(pal_seeblau), "white", pal_bordeaux)
pal2 <- usecol(c(Karpfenblau, Seeblau, "gold"), n = 10)

# Show the my_pair palette from above, the 2 palettes just defined, and 2 pre-defined palettes:  
seecol(list(my_pair, pal1, pal2, pal_unikn, pal_unikn_pair))

Note that unknown color palettes will be named paln in increasing order. Palettes known to seecol() will be shown with their respective names.

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

seecol(list(my_pair, pal1, pal2, pal_unikn, pal_unikn_pair), n = 5)

or larger values of n for extending color palettes:

seecol(list(my_pair, pal1, pal2, pal_unikn, pal_unikn_pair), n = 15)


The following examples illustrate how plotting functions in R can use the unikn color palettes and the seecol() and usecol() functions.

  1. Using the plot function of base R for a scatterplot:
plot(x = runif(99), y = runif(99), "p", pch = 16, cex = 4, 
     col = usecol(pal_unikn, alpha = .50),  # with transparency
     main = "99 transparent dots", axes = FALSE, xlab = NA, ylab = NA)