Contour Polygons From a Density Grid
Source:R/contour_polygons_from_grid.R
dot-contour.polygons.from.grid.RdConverts a pre-computed 2D density grid (as returned by
AutoSpectralRcpp::fast_kde2d_cpp() or MASS::kde2d()) directly into
filled contour polygons using isoband::isobands(), ready to hand to
ggplot2::geom_polygon(). This bypasses ggplot2::stat_contour_filled(),
which would otherwise re-derive the same grid from a long-format melt of
the density matrix before running the identical isobanding step.
Arguments
- x
Numeric vector of grid coordinates along the matrix rows of
z.- y
Numeric vector of grid coordinates along the matrix columns of
z.- z
Numeric matrix of density values, indexed
[x, y](rows correspond tox, columns toy) – the orientation returned by bothfast_kde2d_cpp()andMASS::kde2d(). Transposed internally to matchisoband::isobands()'s[y, x]convention.- breaks
Numeric vector of band boundaries; consecutive pairs define each filled band, matching the
breaksargument ofggplot2::geom_contour_filled().
Value
A data frame with columns x, y, subgroup (isoband's own
per-piece/hole identifier, for the subgroup aesthetic) and level (an
ordered factor spanning every band implied by breaks, low to high, so
colour-to-band mapping stays fixed regardless of which bands are empty in
a given plot). Pass as:
geom_polygon(data = ..., aes(x, y, group = level, subgroup = subgroup, fill = level)). Returns NULL if breaks has fewer than two elements or
every band is empty.