| griddata | R Documentation |
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.
griddata(
x,
w = NULL,
n = 10,
min = NULL,
max = NULL,
type = c("counts", "density", "probability", "mean")
)
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, |
max |
optional scalar or D-element vector specifying the upper bound of the grid. If not given, |
type |
character string specifying the normalization of the output. Must be one of:
|
Returns a list of items
field |
D-dimensional array representing the value in each grid cell. See parameter |
grid |
List of D elements with the grid properties along each dimension: |
dV |
Single number representing the volume of the D-dimensional grid cells. |
Danail Obreschkow
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.