htb.colors: Visually friendly color palette

View source: R/htb.colors.R

htb.colorsR Documentation

Visually friendly color palette

Description

Create a vector of n contiguous colors.

Usage

htb.colors(n, type = "rainbow")

Arguments

n

An integer. The number of colors (>= 1) to be in the palette.

type

A string. The type of the color set. Default rainbow produces rainbow-like colors. Other available types are bruna and imas.

Details

When visualizing data in a graphical plot, R's built-in grDevices::rainbow() function is one of the best and handiest tools to create colors to use. However, when creating a small number of (say, 4 or 5) colors for categorical data groups, the result sometimes contains indistinct colors such as highly bright cyan or yellow. htb.colors() deals with this problem, by using a predetermined set of rainbow-like, visually friendly colors. Those colors are carefully chosen with a wish that they be better distinguished even in case of achromatopsia. This may be especially useful when the number of colors are relatively few (e.g., < 10). When the number is large, the results of grDevices::rainbow() and htb.colors() will not differ so much, though the latter will be still a bit quiet (unvivid) compared with the former.

In case of conditions you want non-rainbow colors, htb.colors() can also produce different set of color types, which you might have happened to see elsewhere. Those colors are not cared for discriminability, so please be cautious.

Value

Strings. A vector of color codes in ⁠#xxxxxx⁠ format.

Examples

piecol <- function(x) {
  pie(rep(1, length(x)), labels = rep("", length(x)),
    col = x, border = NA, radius = 0.95)
}

par(mfrow = c(4, 5), mar = rep(0.1, 4))
for (f in c(grDevices::rainbow, htb.colors)) {
  sapply(X = c(1:9, 30), FUN = function(n) {
    piecol(f(n))
    text(0, 0, n, col = "white", font = 2)
  })
}

n <- c(10, 6, 12)
types <- c("rainbow", "bruna", "imas")
par(mfrow = c(2, 2))
mapply(FUN = function(z1, z2) {
  piecol(htb.colors(z1, type = z2))
  text(0, 0, z2, col = "white", font = 2)
}, n, types)


keimochizuki/htb documentation built on June 9, 2025, 10:03 p.m.