Graphics in color

Title: Standardized procedures to work with colors in graphics

Synopsis: This document is aimed at helping to work with colors in graphics

colorRamp

# colorRamp
# [,1] [,2] [,3] corresponds to [Red] [Blue] [Green]

pal <- colorRamp(c("red", "blue"))
pal(0)
##      [,1] [,2] [,3]
## [1,]  255    0    0
pal(1)
##      [,1] [,2] [,3]
## [1,]    0    0  255
pal(0.5)
##       [,1] [,2]  [,3]
## [1,] 127.5    0 127.5

colorRamp

# colorRamp

pal(seq(0, 1, len = 10))
##            [,1] [,2]      [,3]
##  [1,] 255.00000    0   0.00000
##  [2,] 226.66667    0  28.33333
##  [3,] 198.33333    0  56.66667
##  [4,] 170.00000    0  85.00000
##  [5,] 141.66667    0 113.33333
##  [6,] 113.33333    0 141.66667
##  [7,]  85.00000    0 170.00000
##  [8,]  56.66667    0 198.33333
##  [9,]  28.33333    0 226.66667
## [10,]   0.00000    0 255.00000

colorRampPalette

# colorRampPalette

pal <- colorRampPalette(c("red", "yellow"))
pal(2)
## [1] "#FF0000" "#FFFF00"
pal(10)
##  [1] "#FF0000" "#FF1C00" "#FF3800" "#FF5500" "#FF7100" "#FF8D00" "#FFAA00"
##  [8] "#FFC600" "#FFE200" "#FFFF00"

RColorBrewer and colorRampPalette

# RColorBrewer and colorRampPalette

library(RColorBrewer)
cols <- brewer.pal(3, "BuGn")
cols
## [1] "#E5F5F9" "#99D8C9" "#2CA25F"
pal <- colorRampPalette(cols)
image(volcano, col = pal(20))

The smoothScatter function

# The smoothScatter function

x = rnorm(10000)
y = rnorm(10000)
smoothScatter(x,y)
## KernSmooth 2.23 loaded
## Copyright M. P. Wand 1997-2009

Scatterplot with no transparency

# Scatterplot with no transparency

x = rnorm(10000)
y = rnorm(10000)
plot(x,y, pch=19)

Scatterplot with transparency

# Scatterplot with transparency

x = rnorm(10000)
y = rnorm(10000)
plot(x,y, col=rgb(0,0,0,0.2), pch=19)

Grouping by colour

plot(mtcars$drat, col=mtcars$cyl)