grid_points: Aggregate point data to a grid

grid_pointsR Documentation

Aggregate point data to a grid

Description

Aggregate points to a custom grid

Usage

grid_points(x, y = NULL, z = NULL, grp = NULL, nx = 50, ny = NULL,
  FUN = length)

Arguments

x

Numeric vector or 2 column matrix or data.frame of data points to plot

y

Numeric vector, y coordinates matching x (if a vector)

z

Numeric vector or multi-column matrix or data.frame of variables to be aggregated

grp

Vector, matrix or dataframe of additional variables to aggregate by

FUN

Character string/vector of in-built R aggregation function(s) or a callback function compatible with aggregate()'s FUN argument, to aggregate data input as 'z'. E.g. "min", "max", "mean", "median". For frequency counts use "length" (which will also work if no input for 'z' is provided). A string vector can be passed to apply multiple functions to apply seperately to vector 'z' or individually to the columns of a matrix/data.frame.

nx/ny

Numeric, values for the grid dimensions

Value

A data.frame of grid-jittered point coordinates with point counts and/or data aggregated accordinaly

Examples

# dataset
d = faithful
d$cat = sample(c('A','A','A','A','B','B','C'), nrow(d), rep=TRUE)
d$size = round(10 * rlnorm(nrow(d)))
d$temp = round(100 * rlnorm(nrow(d)))
d$tempcat = c('cool','hot')[cut(d$temp, quantile(d$temp,c(0,.5,1)))]
View(d)

# point counts
d_ag = grid_points(d, nx = 20, FUN=length)
plot(d[,1:2], pch=16, cex=.3, col='red')
symbols(d_ag[,1:2], squares=sqrt(d_ag$n), inches=.3, add=TRUE)

# summarise variables
grid_points(d[,1:2], z=d$temp, nx = 5, FUN=mean)
grid_points(d[,1:2], z=d$temp, nx = 5, FUN=c('min','max','median','length'))
grid_points(d[,1:2], z=d[,c('size','temp')], nx = 5, FUN = mean)
grid_points(d[,1:2], z=d[,c('size','temp')], nx = 5, FUN = c('mean','median'))
grid_points(d[,1:2], z=d[,c('size','temp')], nx = 5, FUN = c('mean','median','sum')) # error

# aggregating by additional variables to grid
grid_points(d[,1:2], z=d$size, grp=d[,c('cat','tempcat')], nx = 5, FUN = mean)
grid_points(d[,1:2], z=d[,c('size','temp')], grp=d[,c('cat','tempcat')], nx = 5, FUN = mean)

# most prevalent sub-category in each grid cell
topcase = function(v){
  counts = plyr::count(v)
  counts = counts[order(counts$freq, decreasing=TRUE),]
  counts[1,1]
}
d_ag = grid_points(d[,1:2], nx = 10, z=d$cat, FUN=topcase)
plot(d[,1:2], pch='.')
text(d_ag[,1:2], labels=d_ag$z, cex=.7, col='red')

geotheory/gridPoints documentation built on March 25, 2022, 5 p.m.