grid_bars: Calculate polygons for a binned-categorical bar plot

View source: R/grid_bars.R

grid_barsR Documentation

Calculate polygons for a binned-categorical bar plot

Description

Take gridded data with a categorical variable and calculate polygons for a binned categorical bar plot

Usage

grid_bars(x, FUN = sum, yscale = 1)

Arguments

x

data.frame with first 2 columns as x/y axis points for gridded data, a 3rd categorical column, and a 4th column of values to aggregate

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.

yscale

Numeric, Multiplier for bar heights

Value

A data.frame of polygon vertex coordinates and corresponding fields

Examples

d = faithful
d$cat = sample(c('A','A','A','A','B','B','C'), nrow(d), rep=TRUE)
d$size = round(10 * rlnorm(nrow(d)))

gridded = grid_points(d[,1:2], z=d[,c('size')], grp=d[,c('cat')], nx = 20, FUN = mean)

polys = grid_bars(gridded)

require(ggplot2)
ggplot(polys) + geom_polygon(aes(x, y, group = id, fill = grp))

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