April, 2018

Palette

R colors

    plot(1:20, y=rep(0,20), col = 1:20, cex = 2, pch = 20 )

R colors

  • Hexadecimal notation is used (0 ~ 9, A ~ F)
  • Basically Red, Green and Blue colors are used and they are represented by two hexadecimal characters. That is, each basic color has 256 levels (#rrggbb)
  • \(256 \times 256 \times 256 = 2^24\) colors can be displayed: 24 bit colors.
  • Moreoever, 256 opacity levels are added into 24bit colors such that 32 bit rgb color system is completed. (##rrggbbaa)

colors()

  • Basic colors
head(colors())
## [1] "white"         "aliceblue"     "antiquewhite"  "antiquewhite1"
## [5] "antiquewhite2" "antiquewhite3"
tail(colors())
## [1] "yellow"      "yellow1"     "yellow2"     "yellow3"     "yellow4"    
## [6] "yellowgreen"

colors()

    mycol = colors()
    plot(1:80, y=rep(1,80), col = mycol[1:80], cex = 2, pch = 20, 
         ylim = c(0,1) )
    points(1:80, y=rep(0.5,80), col = mycol[81:160], cex = 2, pch = 20 )
    points(1:80, y=rep(0,80), col = mycol[161:240], cex = 2, pch = 20 )

colors()

mycol = colors()
image(matrix(1:25^2,25,25), col = mycol)

rgb function

  • rgb(red, green, blue, maxColorValue, alpha)
  • red, green, blue : by default 0 ~ 1.
  • 256 scale (0-255) is available by setting maxColorValue = 255.
  • alpha : opacity
rgb(10, 4, 23, maxColorValue = 255, alpha = 10)
## [1] "#0A04170A"
col2rgb('lightblue')
##       [,1]
## red    173
## green  216
## blue   230

hue color system

  • colors are displayed on a cone.
  • The outter colors has higher saturations.
  • As going up, the brightness is increasing.

hcl function

  • hcl(h, c, l, alpha)
  • h: colors, c: saturation, l: brightness, alpha: opacity
  • h\(\in [0,360]\): 0~120(red), 120~240(green), 240~360(blue)
  • l \(\in [0, 100]\), alpha \(\in [0,1]\)
  • range of c depends on the value of h and l.
hcl(h = 0, c = 35, l = 85, alpha = 0.1)
## [1] "#FFC5D01A"

hsv function

  • hsv(h, s, v, alpha)
  • h: color, s: satulation, v: brightness, alpha: opacity
  • h, s, v, alpha \(\in [0,1]\)
hsv(0.3, 0.5, 0.1, alpha = 0.4)
## [1] "#0F1A0D66"

grDevices, colorRamps Packages

  • cm.colors, topo.colors, terrain.colors, heat.colors, rainbow
  • cm, topo, terrain, heat : n (number of colors), alpha (opacity)
heat.colors(4, alpha = 1)
## [1] "#FF0000FF" "#FF8000FF" "#FFFF00FF" "#FFFF80FF"
topo.colors(4, alpha = 1)
## [1] "#4C00FFFF" "#00E5FFFF" "#00FF4DFF" "#FFFF00FF"

grDevices, colorRamps Packages

x <- 10*(1:nrow(volcano))
y <- 10*(1:ncol(volcano))
image(x, y, volcano, col = heat.colors(20, alpha = 1), axes = FALSE)
contour(x, y, volcano, levels = seq(90, 200, by = 5),
        add = TRUE, col = 'white')

grDevices, colorRamps Packages

x <- 10*(1:nrow(volcano))
y <- 10*(1:ncol(volcano))
image(x, y, volcano, col = topo.colors(20, alpha = 1), axes = FALSE)
contour(x, y, volcano, levels = seq(90, 200, by = 5),
        add = TRUE, col = 'white')

grDevices, colorRamps Packages

  • rainbow colors adopts hsv color system.
  • n (number of colors), s (saturation), v (brightness), start (staring value of h), end (ending value of h), alpha (opacity)
  • the range of start, end: 0 ~ 1
rainbow(5, s = 0.4, v = 0.3, start = 0, end = 0.05, alpha = 1)
## [1] "#4D2E2EFF" "#4D302EFF" "#4D322EFF" "#4D352EFF" "#4D372EFF"

RColorBrewer package

  • Form this package we can define various palettes available in R
  • sequential palettes : Blues, BuGn, BuPu, …, YlOrBr, YlOrRd
library(RColorBrewer)
brewer.pal(4, 'Blues')
## [1] "#EFF3FF" "#BDD7E7" "#6BAED6" "#2171B5"

RColorBrewer package

x <- 10*(1:nrow(volcano))
y <- 10*(1:ncol(volcano))
image(x, y, volcano, col = brewer.pal(9, 'Blues'), axes = FALSE)

RColorBrewer package

  • Diverging palettes : BrBG, PiYG, .., RdYlGn, Spectral
  • Middle colors are bright and the starting and ending colors are dark.
library(RColorBrewer)
brewer.pal(4, 'BrBG')
## [1] "#A6611A" "#DFC27D" "#80CDC1" "#018571"

RColorBrewer package

  • display.brewer.all()
  • This function shows palettes available in RColorBrewer package.
library(RColorBrewer)
display.brewer.all()

colorspace package

  • Color palette based on hcl, hsv color system.
  • diverge_hcv, diverge_hsl, terrain_hcl, sequential_hcl, rainbow_hcl, …
library(colorspace)
## Warning: package 'colorspace' was built under R version 3.4.1
diverge_hcl(7, h = c(246, 40), c = 96, l = c(65, 90))
## [1] "#1FA4FF" "#97BFF3" "#CAD6E9" "#E2E2E2" "#E7D1C5" "#E9B18B" "#E28912"

colorspace package

library(colorspace)
pal = choose_palette()
  • Interactive tool to make color palettes
  • options are referred in RColorBrewer.

colorspace package

  • Default automatically chooses color, saturation, brightness.
  • There are various options for hcl or hsv

colorspace package

  • Check the examples.