Description Usage Arguments Value Examples

Aggregate points to a custom grid

1 2 |

`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 |

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | ```
# 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')
``` |

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.