[R-sig-Geo] RGB in image()

Tim.Haering at lwf.bayern.de Tim.Haering at lwf.bayern.de
Fri Dec 10 11:20:12 CET 2010


Hello,

I would like to plot a SpatialGridDataFrame using the image() function, but have problems with the colors. I want to create a transparent rgb image using three bands as red, green and blue and want to control the transparency with the alpha parameter.
I have a matrix "dist" containing my rgb values:
> head(dist)
          [,1]      [,2]      [,3]
[1,] 0.1895978 0.5196565 0.2907457
[2,] 0.2292516 0.5269284 0.2438200
[3,] 0.1514392 0.4990144 0.3495464
...
### create a color vector using rgb()
> cols <- apply(dist, 1, function(x) rgb(x[1], x[2], x[3], alpha = .2))

### rescale rgb values to c(0,255)
> red <- as.integer(RESCALE(dist[,1],0,255,min(dist[,1]), max(dist[,1])))
> green <- as.integer(RESCALE(dist[,2],0,255,min(dist[,2]), max(dist[,2])))
> blue <- as.integer(RESCALE(dist[,3],0,255,min(dist[,3]), max(dist[,3])))

### create a SGDF
> pd <- data.frame(RW = p$RW, HW = p$HW, red=red, green=green, blue=blue, cols)
> gridded(pd) = ~RW+HW
> pd <- as(pd, "SpatialGridDataFrame")

### using the red, green and blue parameters my image looks fine except that
### my colors aren`t transparent
> image(pd, red="red", green="green", blue="blue")

### If I use my color vector (containing transparent colors) the image looks
### speckled. 
> image(pd, col = cols)

Can somebody help me? Is there another way to make the color transparent?

TIM


------------------------------------- 
Tim Häring
Bavarian State Institute of Forestry
Department of Forest Ecology
Hans-Carl-von-Carlowitz-Platz 1
D-85354 Freising



More information about the R-sig-Geo mailing list