Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color cells of a matrix with RGB

Is it possible to draw a color matrix using levelplot function in the R package lattice? I must color each cell with an RGB function.

The color matrix must be like below and i need to color each cell like rgb(1,0.8,0.9) etc..

Color matrix

I need this because i have to implement self-organizing-map algorithm for color classification. i am not allowed to use built-in kohonen/som functions or som classes.

like image 542
trood Avatar asked Sep 13 '25 20:09

trood


2 Answers

Have look at Murrel: (2011) Raster Images in R Graphics

It gives an excellent explanation how to do such things in base R or in grid.

like image 168
cbeleites unhappy with SX Avatar answered Sep 15 '25 12:09

cbeleites unhappy with SX


This shows you how the rgb colors are encoded in the result from rainbow() and how levelplot will handle that argument to col.regions:

str(  rainbow(100)) 
# chr [1:100] "#FF0000FF" "#FF0F00FF" "#FF1F00FF" "#FF2E00FF" "#FF3D00FF" ...

The red, green and blue arguments range across 0 to FF and the transparency value is FF, so this produces a single band across the range of values:

levelplot(matrix(1:100, 100), region=TRUE, col.regions=rainbow(100) )

And this gives you a (horrible) color matrix:

levelplot(matrix(1:10000, 100), region=TRUE, col.regions=rainbow(100) )
like image 39
IRTFM Avatar answered Sep 15 '25 10:09

IRTFM