[R] colored rasterImage()

Ben Tupper ben.bighair at gmail.com
Fri Oct 1 12:49:13 CEST 2010


Doh! Of course!

Thanks for this. I convinced myself that the input image to  
rasterImage() had to be scaled 0-1. I think I started down that path  
by trying the following with as.raster() ...

 > m <- matrix(data = rep(seq(0,3, length = nx), ny), ncol = nx, nrow  
= ny, byrow = TRUE)
 > r <- as.raster(m)
Error in rgb(tx, tx, tx, max = max) :
   color intensity 1.0303, not in [0,1]

And my next step was to follow the examples for rasterImage() which  
use 0-1.  I'm glad to be straightened out.

Thanks,
Ben



On Oct 1, 2010, at 12:26 AM, Michael Sumner wrote:

> Use the matrix of colours directly:
>
> nx <- 100
> ny <- 100
> m <- matrix(data = rep(seq(0,1, length = nx), ny), ncol = nx, nrow =
> ny, byrow = TRUE)
> plot(1:nx, 1:ny, type = "n")
> my.color <- matrix(data = (rainbow(100))[m*100], ncol = nx, nrow = ny,
> byrow = TRUE)
> rasterImage(my.color, 1, 1, nx, ny)
>
>
> Also, you might want to check out image.SpatialGridDataFrame in
> package sp that provides a wrapper around rasterImage for
> SpatialGrids.
>
> I'm not aware of other examples, but I have not looked hard since the
> first release of rasterImage.
>
> library(sp)
> sp:::image.SpatialGridDataFrame
>
> Cheers, Mike.
>
> On Fri, Oct 1, 2010 at 12:50 PM, Ben Tupper <ben.bighair at gmail.com>  
> wrote:
>> Hello,
>>
>> I have been exploring the possibility to transition some code that  
>> currently
>> uses image() to use the new rasterImage().  To date, I haven't been  
>> able to
>> specify a color look-up strategy that works.  For example...
>>
>> nx <- 100
>> ny <- 100
>> m <- matrix(data = rep(seq(0,1, length = nx), ny), ncol = nx, nrow  
>> = ny,
>> byrow = TRUE)
>> plot(1:nx, 1:ny, type = "n")
>> my.color <- matrix(data = (rainbow(100))[m*100], ncol = nx, nrow =  
>> ny, byrow
>> = TRUE)
>> rasterImage(m, 1, 1, nx, ny, col = my.color)
>>
>> This yields a greyscale image. Is it possible to create colored  
>> images using
>> rasterImage, and, if so, what is the correct way to generate a  
>> colored image
>> using rasterImage?
>>
>> Also, is there a vignette or tutorial on using the new  
>> rasterImage() and
>> related functions?
>>
>> Thanks!
>> Ben Tupper
>> Bigelow Laboratory for Ocean Science
>> www.bigelow.org
>>
>>> R.Version()
>> $platform
>> [1] "i386-apple-darwin9.8.0"
>>
>> $arch
>> [1] "i386"
>>
>> $os
>> [1] "darwin9.8.0"
>>
>> $system
>> [1] "i386, darwin9.8.0"
>>
>> $status
>> [1] ""
>>
>> $major
>> [1] "2"
>>
>> $minor
>> [1] "11.1"
>>
>> $year
>> [1] "2010"
>>
>> $month
>> [1] "05"
>>
>> $day
>> [1] "31"
>>
>> $`svn rev`
>> [1] "52157"
>>
>> $language
>> [1] "R"
>>
>> $version.string
>> [1] "R version 2.11.1 (2010-05-31)"
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> -- 
> Michael Sumner
> Institute for Marine and Antarctic Studies, University of Tasmania
> Hobart, Australia
> e-mail: mdsumner at gmail.com



More information about the R-help mailing list