val2col: Convert values to color levels

View source: R/val2col.R

val2colR Documentation

Convert values to color levels

Description

The val2col function converts a vector of values("z") to a vector of color levels. One must define the number of colors. The limits of the color scale ("zlim") or the break points for the color changes("breaks") can also be defined. When breaks and zlim are defined, breaks overrides zlim. All arguments are similar to those in the image function.

Usage

val2col(z, zlim, col = hcl.colors(12, "YlOrRd", rev = TRUE), breaks)

Arguments

z

A vector of values (default is 12 colors from the hcl.colors palette).

zlim

Limits of the color scale values.

col

Vector of color values

breaks

Break points for color changes. If breaks is specified then zlim is unused and the algorithm used follows cut, so intervals are closed on the right and open on the left except for the lowest interval which is closed at both ends.

Examples

set.seed(1)
n <- 250
x <- seq(n)
y <- rnorm(n)

# Use all levels, evenly distributed breaks
Col <- val2col(y, col=rainbow(20))
plot(x,y, pch=21, bg=Col)

# Use limits, evenly distributed breaks
pal <- colorRampPalette(c("blue", "cyan", "yellow", "red"))
Col <- val2col(y, zlim=c(-1,1), col=pal(20))
plot(x,y, pch=21, bg=Col)
abline(h=c(-1,1), col=8, lty=2)

# Use custom breaks (break vector must have one more break than color)
Col <- val2col(y, col=topo.colors(6), breaks=c(-Inf, -2, -1, 0, 1, 2, Inf))
plot(x,y, pch=21, bg=Col)
abline(h=c(-Inf, -2, -1, 0, 1, 2, Inf), col=8, lty=2)

# by default, image() and val2col() use the same palette
grd <- expand.grid(x = seq(0, 1, length.out = nrow(volcano)), 
  y = seq(0, 1, length.out = ncol(volcano)))
grd$z <- c(volcano)
image(volcano)
points(grd, pch = 20, col = val2col(grd$z), cex = 1)


marchtaylor/sinkr documentation built on July 4, 2022, 5:48 p.m.