transparent function

Share:

Description

This function takes a standard color as an argument and returns a transparent version of that color

Usage

1
transparent(orig.col = "red", trans.val = 1, maxColorValue = 255)

Arguments

orig.col

The original color to be made transparent. Can be specified as a string or a vector of rgb values

trans.val

A number in the interval [0, 1] indicating how transparent to make the color.

maxColorValue

The maximum color value (only used when orig.col is an rgb vector)

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
# Diagram of some examples
plot(1, ylim = c(0, 1), xlim = c(0, 12), bty = "n",
xaxt = "n", yaxt = "n", ylab = "", xlab = "", type = "na")


text(6, .9, "transparent('red', trans.val = x)")
points(x = 1:11, y = rep(.8, 11), pch = 16,
col = transparent("red", seq(0, 1, .1)), cex = 2)
text(x = 1:11, y = rep(.85, 11), seq(0, 1, .1))

text(6, .7, "transparent('red', trans.val = x)")
points(x = 1:11, y = rep(.6, 11), pch = 16,
col = transparent("blue", seq(0, 1, .1)), cex = 2)
text(x = 1:11, y = rep(.65, 11), seq(0, 1, .1))

text(6, .5, "transparent('forestgreen', trans.val = x)")
points(x = 1:11, y = rep(.4, 11), pch = 16,
col = transparent("forestgreen", seq(0, 1, .1)), cex = 2)
text(x = 1:11, y = rep(.45, 11), seq(0, 1, .1))

text(6, .3, "transparent('orchid1', trans.val = x)")
points(x = 1:11, y = rep(.2, 11), pch = 16,
col = transparent("orchid1", seq(0, 1, .1)), cex = 2)
text(x = 1:11, y = rep(.25, 11), seq(0, 1, .1))


# Scatterplot with transparent colors

a.x <- rnorm(100, mean = 0, sd = 1)
a.y <- a.x + rnorm(100, mean = 0, sd = 1)

par(mfrow = c(3, 3))

for(trans.val.i in seq(0, .1, length.out = 9)) {

 plot(a.x, a.y, pch = 16, col = transparent("blue", trans.val.i), cex = 1.5,
      xlim = c(-5, 5), ylim = c(-5, 5), xlab = "x", ylab = "y",
      main = paste("trans.val = ", round(trans.val.i, 2), sep = ""))

}