How to overlay levels in R?

In R, let's say you want to present statistics on the background of an image, for example, in medical imaging

example produced with Mango

Each layer represents a different matrix. Here is an example that doesn't make sense:

  • volcano Gray
  • B<- (volcano>160) * rnorm(prod(dim(volcano)) in the colors of heat.

How would you go?

+3
source share
1 answer

A function imagecan do this, just set the transparency of the second set of colors and overlay it with add=TRUE. Any missing values ​​will be completely transparent, and you can set the alpha level for other colors (of course, your graphics device must support partial transparency for this to work). Here is a quick example:

A <- t(volcano)[ncol(volcano):1,]
B <- (A>160) * rnorm(prod(dim(A)) )

B[ A < 120 ] <- NA  # show some complete transparency

image(A, col=grey( seq(0,1,length.out=12) ) )  # initial plot
hc <- sub('FF$','77',heat.colors(12)) # convert heat colors to have an alpha
image(B, add=TRUE, col=hc)  # overlay the new plot
+3
source

All Articles