If you're working with "real" data for which the grid intervals and sequence cannot be guaranteed to be increasing or unique (hopefully the (x,y,z)
combinations are unique at least, even if these triples are duplicated), I would recommend the akima
package for interpolating from an irregular grid to a regular one.
Using your definition of data
:
library(akima)
im <- with(data,interp(x,y,z))
with(im,image(x,y,z))
And this should work not only with image
but similar functions as well.
Note that the default grid to which your data is mapped to by akima::interp
is defined by 40 equal intervals spanning the range of x
and y
values:
> formals(akima::interp)[c("xo","yo")]
$xo
seq(min(x), max(x), length = 40)
$yo
seq(min(y), max(y), length = 40)
But of course, this can be overridden by passing arguments xo
and yo
to akima::interp
.