Learn R Programming

basetheme (version 0.1.3)

num2col: Numbers to Colors

Description

Assigns colors to the provided vector of numbers.

Usage

num2col(x, pal, ref = range(x, na.rm = TRUE), NAcol)

Value

a vector of colors for each element in x or, when x is missing, a function.

Arguments

x

numeric vector (factors are transformed to numeric)

pal

colors used to build the palette (defaults to colors set by theme)

ref

reference for assigning colors (defaults to the range of x)

NAcol

color to be used for NA values (defaults to color set by theme)

Author

Karolis Koncevičius

Details

This function interpolates a given set of colors to a numeric vector. Main use case is in turning numbers into colors for plots, especially when different ranges of x have to be colored differently.

Color of NA values and values outside of ref range can be set using NAcol argument. Set this to NA to omit the display of such values.

In case only a single color is provided - it is expanded by using tints and shades.

When x is not specified - a function that generates colors based on pal and ref is returned.

See Also

lab2col

Examples

Run this code
 # color numbers by y-axos
 plot(mtcars$hp, mtcars$mpg, col=num2col(mtcars$mpg), pch=19)

 # color only a certain range
 plot(mtcars$hp, mtcars$mpg, col=num2col(mtcars$mpg, ref=c(20, 35)), pch=19)

 # hide the out of range values
 plot(mtcars$hp, mtcars$mpg, col=num2col(mtcars$mpg, ref=c(20,35), NAcol=NA))

 # iris example
 pairs(iris[,-5], col=num2col(iris$Sepal.Length))

 # same butusing a prepared coloring function (for values in range 0-10)
 color <- num2col(ref=c(0,10))
 pairs(iris[,-5], col=color(iris$Sepal.Length))

Run the code above in your browser using DataLab