1

Is there a way how to program the colors in the image function in such a way, that parts of the image will show transparent colors? The transparent colors can be assigned to specific values in the z matrix, but I need different color transparency for the same values in z, but different values of x and y depending on a fourth variable.

 x = 1:10
 y = 1:5
 z = matrix(sample(0:2, size = 50, replace = TRUE), ncol = 5)
 
 image(x = x, y = y, z = z, col = hcl.colors(3, "viridis"))

enter image description here

But let's say I have a vector of alpha values specifying transparency as tr = c(rep(0.5, 10), rep(1, 40). What I want is an image that will look like this using the same z matrix as above:

enter image description here

I can obtain the solution specifying positions for the transparent colors and building the image sequentially.

image(x = x, y = y, z = matrix(NA, 10, 5))
image(x = 1:2, y = 1:5, z = z[1:2, ], col = hcl.colors(3, "viridis", alpha = 0.5), add = TRUE)
image(x = 3:10, y = 1:5, z = z[3:10, ], col = hcl.colors(3, "viridis"), add = TRUE)

While this is a working solution, it becomes tedious. Is there a comprehensive option how to code color transparency in image() with values in a vector?

nya
  • 2,138
  • 15
  • 29

0 Answers0