HI,
I got a question about how to assign color value in image() function. I thought the col option works as a vector with the same length of Z, with color of each cell in the matrix Z corresponding to the values in. However, it confused me with different result.
To make my question simple, let's say I want to make a color image for a vector x=1:4
with corresponding color for each cell.
x=1:4
image(1,1:length(x), matrix(x, nrow=1, ncol=length(x)), col=c("blue","red",'green','yellow'))
The output image looks like this:
However, if I use a different x, say
x=c(3,1,2,1)
image(1,1:length(x), matrix(x, nrow=1, ncol=length(x)), col=c("blue","red",'green','yellow'))
I expected the color (from bottom to up) order should be green-->blue-->red-->blue, however, I got a very different order:
I guess I misunderstand how to use the color option in the function. Can someone please help to advise? Thanks
-Xianjun
This is interesting. If you receive the Yellow color from the end:
it will color properly.
If you use 5 colors:
R will plot one color after other.
If you use 6 colors:
R will plot odd colors and then jump to last.
May be something with the algorithm?
Thanks for your comments, Deepak.
As my reply below, I guess I figured it out: We always get
min(x)=col[1]
,max(x)=col[last]
. For your case, then there are 6 colors, again, 1=blue, 3=grey, 2=green (since it's left close).