griddata: Distribute a point set onto a regular grid

View source: R/griddata.R

griddataR Documentation

Distribute a point set onto a regular grid

Description

Distributes a set of points in D dimensions onto a regular, D-dimensional grid, using a fast nearest-neighbor algorithm. Weights can be used optionally.

Usage

griddata(
  x,
  w = NULL,
  n = 10,
  min = NULL,
  max = NULL,
  type = c("counts", "density", "probability", "mean")
)

Arguments

x

N-element vector (if D=1) or N-by-D matrix (if D>1), giving the Cartesian coordinates of N points in D dimensions.

w

optional N-element vector with weights.

n

scalar or D-element vector specifying the number of equally spaced grid cells along each dimension.

min

optional scalar or D-element vector specifying the lower bound of the grid. If not given, min is adjusted to the range of x.

max

optional scalar or D-element vector specifying the upper bound of the grid. If not given, max is adjusted to the range of x.

type

character string specifying the normalization of the output. Must be one of:

  • "counts": returns the number of points in each cell, or the sum of weights if w is given. Thus the total number of points (or total mass, if weights are given) is sum(field).

  • "density": returns the density, such that the total number of points (or total mass, if weights are given) is sum(field) * dV.

  • "probability": returns a probability density, such that sum(field) * dV = 1.

  • "mean": returns the mean weight in each cell. This option requires w to be given. Cells with no data are assigned NA.

Value

Returns a list of items

field

D-dimensional array representing the value in each grid cell. See parameter type for more details.

grid

List of D elements with the grid properties along each dimension: n = number of grid cells; mid = n-vector of mid-cell coordinates; breaks = (n+1)-vector of cell edges; min, max = considered range; delta = cell width.

dV

Single number representing the volume of the D-dimensional grid cells.

Author(s)

Danail Obreschkow

Examples

# On some machines data.table does not perform well in multi-threading mode.
# The following command limits this to one thread as a precaution, but
# may not be needed on most machines.
data.table::setDTthreads(1L)

# Distribute 1-dimensional data onto a regular grid
npoints = 1e4
x = rnorm(npoints)
g = griddata(x,min=-3,max=3,n=100,type='probability')
curve(dnorm(x),-3,3)
points(g$grid$mid,g$field,pch=16)

# Distribute 2-dimensional data onto a regular grid
x = runif(100,max=2)
y = runif(100)
g = griddata(cbind(x,y),min=c(0,0),max=c(2,1),n=c(20,10))
image(g$grid[[1]]$breaks,g$grid[[2]]$breaks,g$field,
      asp=1,col=grey.colors(100,0,1),xlab='x',ylab='y')
points(x,y,col='red',pch=16)

# ... same with weights
w = runif(100)
g = griddata(cbind(x,y),w,min=c(0,0),max=c(2,1),n=c(20,10))
image(g$grid[[1]]$breaks,g$grid[[2]]$breaks,g$field,
      asp=1,col=grey.colors(100,0,1),xlab='x',ylab='y')
points(x,y,col='red',pch=16,cex=w)

# ... mean weight in each cell (dark red, where no data)
par(bg = "darkred")
g = griddata(cbind(x,y),w,min=c(0,0),max=c(2,1),n=c(20,10),type='mean')
image(g$grid[[1]]$breaks,g$grid[[2]]$breaks,g$field,
      asp=1,col=grey.colors(100,0,1),xlab='x',ylab='y')
points(x,y,col='red',pch=16,cex=w)


cooltools documentation built on Sept. 11, 2026, 5:06 p.m.