… using (predefined) color palettes for setting colors.
readme.html
and run
2-0_ChapterSetup.R
. This will create
2-0_ChapterSetup.RData
in the sub folder
data/R
. This file contains the data required to produce the
state distribution plot shown below.LoadInstallPackages.R
# assuming you are working within .Rproj environment
library(here)
# install (if necessary) and load other required packages
source(here("source", "load_libraries.R"))
# load environment generated in "2-0_ChapterSetup.R"
load(here("data", "R", "2-0_ChapterSetup.RData"))
The community of
developers provides you with plenty of tools for finding suitable colors
for sequence visualization. We illustrate how to use {RColorBrewer}
(which is also used by
TraMiner::seqdef
) and {colorspace}
to choose colors. If you
are using R Studio you also might want to consider the very helpful
package {colourpicker}
.
For a brief introduction to “Selecting Colors for Statistical Graphics” we recommend the paper by Zeileis et al. 2009 (2009). For a more detailed discussion of color palettes based on the HCL (Hue-Chroma-Luminance) color space using R we refer to Zeileis et al. 2020 (2020).
For color palette enthusiast we also highly recommend the comprehensive list of color palettes in R as well as
the very handy {paletteer}
package by Emil
Hvitfeldt.
In our example presented below we want to find a suitable color palette for visualizing sequences with the following state space (alphabet):
State | Short Label |
---|---|
Single, no child | S |
Single, child(ren) | Sc |
LAT, no child | LAT |
LAT, child(ren) | LATc |
Cohabiting, no child | COH |
Cohabiting, child(ren) | COHc |
Married, no child | MAR |
Married, 1 child | MARc1 |
Married, 2+ children | MARc2+ |
The state space is a combination of partnership status and fertility. For each partnership state we assign a unique color using pre-defined color palettes.
In order to indicate differences in fertility within partnership states we increase the chroma of the respective color.
{RColorBrewer}
Typing display.brewer.all()
provides you with an
overview of the pre-defined color palettes available in the {RColorBrewer}
package.
Knowing the names of the palettes we want to use
("Blues", "Greens", "Oranges", "RdPu"
) we can continue by
choosing specific colors from these palettes. Usually this is an
iterative process which involves inspecting multiple palettes.
For example, {RColorBrewer}
gives us different sets
of Blues when we inspect sequential palettes of different length. It is
up to you to decide which colors fit your purposes best and to extract
them for your sequence color palette.
display.brewer.pal(3, "Blues")
display.brewer.pal(7, "Blues")
This is the code for extracting the colors and creating a vector that contains the colors in the desired order:
<- brewer.pal(3, "Blues")[2:3] # Single states
col1 <- brewer.pal(3, "Greens")[2:3] # LAT states
col2 <- brewer.pal(3, "Oranges")[2:3] # Cohabitation states
col3 <- brewer.pal(7, "RdPu")[4:6] # Marriage states
col4
# define complete color palette
<- c(col1, col2, col3, col4) colspace1
The resulting color palette can be inspected using the
swatchplot
function of the {colorspace}
package.
swatchplot(colspace1)
{colorspace}
Like in the previous step we start by plotting the pre-defined color palettes available in the package:
hcl_palettes(plot = TRUE)
Again, we continue with inspecting the suitable palettes.
swatchplot(sequential_hcl(5, palette = "Blues"))
Note that the resulting color vector starts with the darkest blue,
whereas our intended color palette should start with a light blue.
Accordingly, the colors must be extracted in reverse order (as in the
code below) or one could change the order of the vector by adding the
option rev = TRUE
when running
sequential_hcl
.
This is our final choice using the palettes provided by {colorspace}
:
<- sequential_hcl(5, palette = "Blues")[3:2]
col1 <- sequential_hcl(5, palette = "Greens")[2:1]
col2 <- sequential_hcl(5, palette = "Oranges")[3:2]
col3 <- sequential_hcl(5, palette = "magenta")[3:1]
col4
<- c(col1, col2, col3, col4)
colspace2
swatchplot(colspace2)
Both approaches produce satisfactory results:
swatchplot("RColorBrewer" = colspace1,
"colorspace" = colspace2)
For our final decision we compare the palettes by inspecting the
state distribution plots of our example data using the
seqdplot
function (see Chapter
2.4.1). We prefer the solution obtained by {colorspace}
and will proceed with the
respective color palette.
If you see mistakes or want to suggest changes, please create an issue on the source repository.
Text and figures are licensed under Creative Commons Attribution CC BY-NC 4.0. Source code is available at https://github.com/sa-book/sa-book.github.io, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".