1
votes

To highlight the difference between two identically sized matrices I would like to show the two superimposed in a semitransparent way using shades of red for the one matrix and shades of green for the other one (yielding yellow where they are identical) in R.

To display just one matrix I have

library(grDevices)
matr=replicate(10, rnorm(20,mean=0.5,sd=0.1))    
colpalette=colorRampPalette(c("black", "red")) 
image(matr^0.2,col = colpalette(1000),useRaster=T)

Does any one have any idea how I should adapt this to show two matrices matr1 and matr2 superimposed in red/green?

Also, what would be the best way to have a bit of control over the brightness & contrast of the resulting image? Are there better ways than the power transform I am using now?

cheers, Tom

1
rasterImage will take a matrix of hex values or a 3d array of rgb values directly, use that - image does not expose that option. see also SGDF2PCT in sp for a palette building approach for image, remember that transparency support depends on the device, not sure if rasterImage provides itmdsumner
@mdsumner -- sp:::SGDF2PCT finds nothing. Did you mean some other package?Josh O'Brien
@JoshO'Brien rgdal is the package I believe.thelatemail
@thelatemail -- So it is! Thanks to both of you.Josh O'Brien

1 Answers

0
votes

Ha just found an easy solution by first calculating the log2(difference) between the two matrices and plotting that using a palette with a break at zero. That makes sense, right?

library(grDevices)
matr1=replicate(10, rnorm(20,mean=0.5,sd=0.1)) 
matr2=replicate(10, rnorm(20,mean=0.5,sd=0.1))
matrdiff=log2(matr1/matr2)
nbcolors=1000
colpalette=colorRampPalette(c("red","yellow","green"))(nbcolors)
breaks = c(seq(min(matrdiff), 0, length.out=nbcolors/2), 0, 
           seq(0,max(matrdiff), length.out=nbcolors/2))
image(matrdiff,col=colpalette,breaks=breaks,useRaster=T)