colorRamp2: A faster implementation of grDevices::colorRamp for linear...

Description Usage Arguments Examples

View source: R/color-palettes.R

Description

A faster implementation of grDevices::colorRamp for linear interpolation.

Usage

1
colorRamp2(x, alpha = TRUE, thresholds = NULL)

Arguments

x

A vector of colors.

alpha

Logical scalar. When TRUE This implementation of colorRamp can be 2 or more times faster than the grDevices version. It is intended for consecutive calls (i.e. in a loop) to improve performance. It is equivalent to the linear interpolation of the function colorRamp.

thresholds

A numeric vector of length length(x). Optional threshold levels so that the mixing can be different that even.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Creating a function for 2 colors
myf <- colorRamp2(c("black", "steelblue"))
f   <- colorRamp(c("black", "steelblue"))

plot.new()
plot.window(xlim = c(0,2), ylim = c(1, 11))

# These should be the same colors
rect(
  xleft   = 0,
  xright  = 1,
  ybottom = 1:10,
  ytop    = 2:11,
  col = rgb(myf((1:10)/10), maxColorValue = 255)
  )
rect(
  xleft   = 1,
  xright  = 2,
  ybottom = 1:10,
  ytop    = 2:11,
  col = rgb(f((1:10)/10), maxColorValue = 255)
)

# Another example setting different thresholds
myf  <- colorRamp2(c("black", "steelblue"))
myf2 <- colorRamp2(c("black", "steelblue"), thresholds=c(0, .7))

plot.new()
plot.window(xlim = c(0,2), ylim = c(1, 11))

# These should be the same colors
rect(
  xleft   = 0,
  xright  = 1,
  ybottom = 1:10,
  ytop    = 2:11,
  col = rgb(myf((1:10)/10), maxColorValue = 255)
  )
rect(
  xleft   = 1,
  xright  = 2,
  ybottom = 1:10,
  ytop    = 2:11,
  col = rgb(myf2((1:10)/10), maxColorValue = 255)
)


## Not run: 
# Ho much better?
n <- 20
microbenchmark::microbenchmark(
  myramp = myf((1:n)/n),
  rramp  = f((1:n)/n), times = 1e4,
  unit = "relative"
)

## End(Not run)

USCbiostats/polygons documentation built on May 27, 2021, 2:34 a.m.