Visualizing sequences

… using (predefined) color palettes for setting colors.

Click here to get instructions…
# 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+

 

Defining the sequence color palette

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.

 

Choosing colors with {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:

col1 <- brewer.pal(3, "Blues")[2:3]   # Single states
col2 <- brewer.pal(3, "Greens")[2:3]  # LAT states
col3 <- brewer.pal(3, "Oranges")[2:3] # Cohabitation states
col4 <- brewer.pal(7, "RdPu")[4:6] # Marriage states

# define complete color palette
colspace1 <- c(col1, col2, col3, col4)

The resulting color palette can be inspected using the swatchplot function of the {colorspace} package.

swatchplot(colspace1)

Choosing colors with {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}:

col1 <- sequential_hcl(5, palette = "Blues")[3:2]
col2 <- sequential_hcl(5, palette = "Greens")[2:1]
col3 <- sequential_hcl(5, palette = "Oranges")[3:2]
col4 <- sequential_hcl(5, palette = "magenta")[3:1]

colspace2 <- c(col1, col2, col3, col4)

swatchplot(colspace2)

Comparison of the two different color palettes

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.

Zeileis, A., Fisher, J. C., Hornik, K., Ihaka, R., McWhite, C. D., Murrell, P., Stauffer, R., & Wilke, C. O. (2020). Colorspace: A Toolbox for Manipulating and Assessing Colors and Palettes. Journal of Statistical Software, 96(1), 1–49. https://doi.org/10.18637/jss.v096.i01
Zeileis, A., Hornik, K., & Murrell, P. (2009). Escaping RGBland: Selecting colors for statistical graphics. Computational Statistics & Data Analysis, 53(9), 3259–3270. https://doi.org/10.1016/j.csda.2008.11.033

References

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Reuse

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 ...".