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()
.
seecol()
is a general-purpose tool for seeing (or visualizing) colors or color palettes. The seecol()
function takes two main arguments:
pal
provides either one or multiple color palettes (with a default of pal = "all"
);n
specifies the number of desired colors (with a default of n = "all"
).Based on the input of pal
, the seecol()
function distinguishes between two modes:
list
-object).usecol()
allows using colors or color palettes (e.g., when creating visualizations) without showing its details. The usecol()
function also takes arguments for conveniently manipulating color palettes:
pal
provides either one or multiple color palettes (with a default of pal = pal_unikn
);n
specifies the number of desired colors (with a default of n = "all"
);alpha
adjusts the opacity of all colors in pal
(e.g., alpha = .50
for medium transparency).Three additional functions provide general functionality for finding colors and defining color palettes:
simcol()
allows finding similar colors (given a target color, a set of candidate colors, and some tolerance threshold(s));
grepal()
allows searching the names of color palettes (i.e., a vector of color names or data frame of named colors) for a pattern
and returns those colors that match the pattern (or regular expression);
newpal()
allows defining new color palettes (usually as data frames with dedicated color names).
Finally, two more functions serve in auxiliary roles:
ac()
is a flexible wrapper for adjustcolor()
that allows setting color transparency;
shades_of()
provides a color vector that consists of n
shades of some color.
The rest of this vignette provides examples of using and details on these functions. (See the Color recipes vignette for more examples of solving color-related tasks.)
seecol()
The behavior of the seecol()
function distinguishes between two modes and depends on the input to its initial pal
argument. It either shows (A) the details of a single color palette, or (B) allows comparing multiple color palettes. The next two sections will address both modes in turn.
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
The detailed overview of a color palette provides us with
When a color palette contains too many colors, the HEX and RGB values are no longer printed. However, setting hex
and rgb
to TRUE will force them to be shown.
Note that seecol()
also returns the color palette that is being shown. Thus, a typical workflow comprises both seeing a particular color palette and saving it (for storing and applying it later):
<- seecol(pal_unikn_light) # view details of AND save a color palette my_pal
Due to saving the color palette (here to my_pal
) we can later use it in a visualization:
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:
seecol(pal_bordeaux)
When the pal
argument of seecol()
specifies (a list of) multiple color palettes, the function 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")
seecol("unikn_basic")
Note, that pal_unikn_web
and pal_unikn_ppt
are almost identical, but differ in how vibrant their colors are.
seecol("pair_all")
seecol("pref_all")
seecol("grad_all")
seecol()
The seecol()
function provides some aesthetic parameters for adjusting how color palettes are plotted:
col_brd
allows specifying the color of box borders (if shown. Default: col_brd = NULL
);lwd_brd
allows specifying the line width of box borders (if shown. Default: lwd_brd = NULL
);title
allows replacing the default title with a custom title.Examples:
seecol("grad_all", col_brd = "black", lwd_brd = 1, title = "Color gradients (with black borders)")
seecol(pal_seegruen, col_brd = "white", lwd_brd = 10, title = "A color palette (with white borders)")
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(pal_seegruen)
seecol("pal_seegruen")
seecol("seegruen")
seecol(seegruen) # issues a warning, but works
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:
n
smaller than the length of the color palette;n
greater than the length of the color palette.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).
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))
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))
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:
<- seecol(pal_unikn_pair, n = 10) my_pair
# Create data:
<- matrix(sample(5:10, size = 10, replace = TRUE), ncol = 5)
dat
# Plot in my_pair colors:
barplot(dat, beside = TRUE, col = my_pair)
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:
seecol(pal_unikn, alpha = 0.5)
Setting opacity for a custom color palette:
<- usecol(c("steelblue", "gold", "firebrick", "forestgreen"), alpha = 2/3)
four_cols
seecol(four_cols, title = "Four named colors with added transparency")
Setting opacity for comparing of multiple color palettes:
seecol("grad", alpha = 0.67, title = "Seeing color palettes with added transparency")
The ac()
function provides a flexible wrapper around the adjustcolor()
function of the grDevices package. Its key functionality is that it allows for vectorized col
and alpha
arguments:
<- c("black", "firebrick", "forestgreen", "gold", "steelblue")
my_cols seecol(ac(my_cols, alpha = c(rep(.25, 5), rep(.75, 5))))
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:
<- c(rev(pal_seeblau), "white", pal_bordeaux)
pal1 <- usecol(c(Karpfenblau, Seeblau, "gold"), n = 10)
pal2
# 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 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:
seecol(list(my_pair, pal1, pal2, pal_unikn, pal_unikn_pair),
pal_names = c("my_pair", "blue_bord", "blue_yell"),
title = "Labeling custom color palettes")
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
:
seecol(list(my_pair, pal1, pal2, pal_unikn, pal_unikn_pair),
pal_names = c("my_pair", "blue_bord", "blue_yell", "blue_black", "mix_pair"),
title = "Comparing and labeling custom color palettes")
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)
simcol()
Assuming that our favorite color is "deeppink"
, a good question is: How can we find similar colors? Given some target color, the simcol()
function searches through a set of colors to find and return visually similar ones:
simcol("deeppink")
#> deeppink deeppink2 maroon1 maroon2 violetred1 violetred2
#> "deeppink" "deeppink2" "maroon1" "maroon2" "violetred1" "violetred2"
By default, simcol()
searches though all named R colors of colors()
(of the grDevices package), but adjusting the col_candidates
and tol
arguments allows more fine-grained searches:
simcol("deepskyblue", col_candidates = pal_unikn, tol = c(50, 50, 100))
#> deepskyblue seeblau5 seeblau4 seeblau3
#> "deepskyblue" "#008ECE" "#00A9E0" "#59C7EB"
grepal()
A common situation in R is that we want some particular shade of color (e.g., some sort of purple), but also know that the color named “purple” is not the one we want. Instead, we would like to see all colors that contain the keyword “purple” in its name. This is what grepal()
is made for:
grepal("purple") # get & see 10 names of colors() with "purple" in their name
#> [1] "mediumpurple" "mediumpurple1" "mediumpurple2" "mediumpurple3"
#> [5] "mediumpurple4" "purple" "purple1" "purple2"
#> [9] "purple3" "purple4"
Note that the grepal()
allows for regular expressions:
length(grepal("gr(a|e)y", plot = FALSE)) # shades of "gray" or "grey"
#> [1] 224
length(grepal("^gr(a|e)y", plot = FALSE)) # shades starting with "gray" or "grey"
#> [1] 204
length(grepal("^gr(a|e)y$", plot = FALSE)) # shades starting and ending with "gray" or "grey"
#> [1] 2
By default, grepal()
searches the vector of named colors x = colors()
(provided by grDevices) and plots its results (as a side effect). However, it also allows for searching color palettes provided as data frames (with color names) and for suppressing the visualization (by setting plot = FALSE
):
grepal("see", pal_unikn) # finding "see" in (the names of) pal_unikn (as df)
#> seeblau5 seeblau4 seeblau3 seeblau2 seeblau1 seegrau1 seegrau2 seegrau3
#> 1 #008ECE #00A9E0 #59C7EB #A6E1F4 #CCEEF9 #E5E5E5 #CCCCCC #999999
#> seegrau4
#> 1 #666666
grepal("blau", pal_unikn_pref, plot = FALSE) # finding "blau" in pal_unikn_pref
#> Seeblau Karpfenblau
#> 1 #59C7EB #3E5496
shades_of()
We have seen that the main usecol()
function allows squeezing and stretching color palettes and thus creating complex color gradients. An even simpler way for creating color gradients is provided by the shades_of()
function:
seecol(shades_of(n = 7, col_1 = Karpfenblau), title = "Shades of Karpfenblau")
Internally, shades_of()
is merely a convenient wrapper for a special usecol()
function. The limitation of shades_of()
is that it only allows creating bi-polar color palettes (i.e., gradients between two colors), whereas usecol()
allows creating color gradients between an arbitrary number of colors. Thus, the following two palettes are identical:
<- usecol(c("deeppink", "gold"), 5)
shady_1 <- shades_of(5, "deeppink", "gold")
shady_2 all.equal(shady_1, shady_2)
#> [1] TRUE
seecol(shady_2, title = "A bi-polar color gradient")
newpal()
Having created, combined or found all those beautiful colors, we may wish to define a new color palette. Defining a new named color palette allows to consistently access and apply colors (e.g., to a series of visualizations in a report or publication). The newpal()
function makes it easy to define color palettes:
<- c("#000000", "#dd0000", "#ffce00") # source: www.schemecolor.com
col_flag
<- newpal(col = col_flag,
flag_de 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.
This was a quick tour through the color functions contained in the unikn package. We hope that they enable you to easily create, find, and apply beautiful colors — may your life be graphic and full of brilliant, vivid and flamboyant visualizations!
The following versions of unikn and corresponding resources are currently available:
Type: | Version: | URL: |
---|---|---|
A. unikn (R package): | Release version | https://CRAN.R-project.org/package=unikn |
Development version | https://github.com/hneth/unikn/ | |
B. Online documentation: | Release version | https://hneth.github.io/unikn/ |
Development version | https://hneth.github.io/unikn/dev/ |
The following vignettes provide instructions and examples for using the unikn colors, color palettes, and color functions:
Nr. | Vignette | Content |
---|---|---|
1. | Colors | Colors and color functions |
2. | Color recipes | Recipes for color-related tasks |
3. | Institutional colors | Creating color palettes for other institutions |
4. | Text | Text boxes and decorations |