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:
The unikn package provides some colors (e.g.,
Seeblau
) and color palettes (e.g., pal_unikn
).
However, its functionality is mainly based on color-related functions
that are useful beyond the colors and palettes 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 (inspecting 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 = "unikn_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).Two additional functions allow finding colors by similarity or name:
simcol()
allows finding similar
colors (given a target color, a set of candidate colors, and
some tolerance threshold(s));
grepal()
allows finding colors with
particular names (i.e., colors whose names match some
pattern
or regular expression);
Finally, some auxiliary functions support specific color functions:
ac()
adjusts color
transparency;
shades_of()
allows creating linear color
gradients;
newpal()
allows defining new color
palettes (as vectors or data frames with dedicated color
names); and
demopal()
allows illustrating color
palettes for different types of visualizations.
The rest of this vignette provides examples of and some details on using 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 an
individual 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:
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):
Due to saving the color palette (here to my_pal
) we can
later use it in a visualization:
Note that seecol()
invisibly returns the color
palette.
Thus, the following will plot the palette pal_bordeaux
without doing anything else with it:
but the following would both plot and assign the palette to
my_pal
:
The second mode of seecol()
is invoked by providing (a
list of) multiple color palettes to its pal
argument.
In this case, the function allows comparing these palettes by plotting a
color vector for each palette. Some special keywords within the
unikn package denote sets of color palettes:
"unikn_all"
, "unikn_basic"
,
pair_all"
, "pref_all"
and
"grad_all"
refer to University of Konstanz color
palettes.Calling seecol
with pal
set to these
keywords allows comparing pre-defined sets of the color palettes:
Viewing the uni.kn color palettes:
Note, that pal_unikn_web
and pal_unikn_ppt
are almost identical, but differ in how vibrant their colors are.
See the vignette on Institutional colors for creating color palettes for other institutions.
seecol()
argumentsThe 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
);main
and sub
allow replacing the default
titles with custom titles.Examples:
usecol()
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:
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:
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:
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:
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:
For values of n
that are larger than the number of
available colors in pal
, the specified color palette is
extended using ColorRampPalette
:
Both seecol()
and usecol()
allow extending
or truncating color palettes to a desired number n
of
colors. For instance:
pal_seeblau
(with
n = 8
colors):pal_bordeaux
(with
n = 3
colors):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:
# 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)
Creating linear color gradients is also supported by the
shades_of()
function (see below).
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:
four_cols <- usecol(c("steelblue", "gold", "firebrick", "forestgreen"), alpha = 2/3)
seecol(four_cols, main = "Four named colors with added transparency")
Setting opacity for comparing of multiple color 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 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"),
main = "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"),
main = "Comparing and labeling custom color palettes")
As before, we can use lower values of n
for
truncating/obtaining shorter subsets of color palettes:
or higher values of n
for extending color palettes:
Two familiar color search tasks are addressed by the
simcol()
and grepal()
functions:
simcol()
allows searching for colors that are similar
to a given target colorgrepal()
allows searching for colors whose names match
some patternsimcol()
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", plot = FALSE)
#> 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 for more fine-grained searches:
grepal()
We often search for some particular color hue (e.g., some sort of
purple), but also know that the particular 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. The grepal()
function addresses this need: