[R] image of expand.grid

Uwe Ligges ligges at statistik.uni-dortmund.de
Mon May 5 10:00:09 CEST 2003


juli g. pausas wrote:
>  Uwe Ligges wrote:
> 
>> juli g. pausas wrote:
>>
>>> Hi all,
>>> It is not clear to me why this cannot be ploted with image. Any help?
>>>
>>>  > g <- data.frame(expand.grid(x= 1:5, y= 1:5), z= rnorm(25))
>>>  > image(g)
>>> Error in image.default(g) : increasing x and y values expected 
>>
>>
>>
>> The error message states: "increasing x and y values expected".
>> image() is really clever and you don't need expand.grid() in this case 
>> (in principle, image() does it itself):
>>
>>  g <- list(x = 1:5, y = 1:5, z = matrix(rnorm(25), 5))
>>  image(g) 
> 
> 
> 
> Uwe,
> Thanks for your reply.
> I undertand that if z is a matrix, then x and y are not needed:
>    image( matrix(rnorm(25), 5) )
> 
> My question was, if I've got 'x', 'y' and 'z', where x and y are the 
> coordinates of the z values, can I use image to ploted them?
> Perhaps a more realistic example would be, when the grid is incomplete 
> such as in h:
> 
> g <- data.frame(expand.grid(x= 1:5, y= 1:5), z= rnorm(25))
> h <- g[sample(nrow(g), 20), ]    # remove some cells
> 
>  From the help pages I thought it was possible:
> 
> Arguments:
>     x,y: locations of grid lines at which the values in `z' are
>          measured.  These must be in (strictly) ascending order. By ...
> 
> this is why my suggestion was a list with x, y, and z of the same length 
> (coordinates and z-values). But I suppose I misunderstood the help, and 
> it is  not possible to use image() in this case, even with the complete 
> coordinates (g).
> I have found an alternative way,  using as.image() in the 'fields' library:
> 
> image(as.image(h$z, data.frame(x= h$x, y= h$y), ncol=5, nrow=5))
> 
> cheers
> Juli

 From ?image:

   z  a matrix containing .....

So z must be a matrix, even if used as a component of a list.
NAs are allowed in z.

Uwe Ligges




More information about the R-help mailing list