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

Michael Sumner mdsumner at gmail.com
Sun Dec 12 00:09:34 CET 2010


You need to generate an index on the grid and a colour table to use.
See ?SGDF2PCT in the rgdal package, and the following should work:



   cols <- SGDF2PCT(pd)
     pd$idx <- cols$idx
   image(pd, col = cols$ct)

Also, this question is more appropriate for the R-Sig-Geo mailing list.

Cheers, Mike.

On Fri, Dec 10, 2010 at 9:20 PM,  <Tim.Haering at lwf.bayern.de> wrote:
> 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
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



-- 
Michael Sumner
Institute for Marine and Antarctic Studies, University of Tasmania
Hobart, Australia
e-mail: mdsumner at gmail.com



More information about the R-sig-Geo mailing list