`R/Contours.R`

`TernaryDensityContour.Rd`

Use two-dimensional kernel density estimation to plot contours of point density.

```
TernaryDensityContour(
coordinates,
bandwidth,
resolution = 25L,
tolerance = -0.2/resolution,
edgeCorrection = TRUE,
direction = getOption("ternDirection", 1L),
...
)
```

- coordinates
A list, matrix, data.frame or vector in which each element (or row) specifies the three coordinates of a point in ternary space.

- bandwidth
Vector of bandwidths for x and y directions. Defaults to normal reference bandwidth (see

`MASS::bandwidth.nrd`

). A scalar value will be taken to apply to both directions.- resolution
The number of triangles whose base should lie on the longest axis of the triangle. Higher numbers will result in smaller subdivisions and smoother colour gradients, but at a computational cost.

- tolerance
Numeric specifying how close to the margins the contours should be plotted, as a fraction of the size of the triangle. Negative values will cause contour lines to extend beyond the margins of the plot.

- edgeCorrection
Logical specifying whether to correct for edge effects (see details).

- direction
(optional) Integer specifying the direction that the current ternary plot should point: 1, up; 2, right; 3, down; 4, left.

- ...
Further parameters to pass to

`contour()`

.

This function is modelled on `MASS::kde2d()`

, which uses
"an axis-aligned bivariate normal kernel, evaluated on a square grid".

This is to say, values are calculated on a square grid, and contours fitted
between these points. This produces a couple of artefacts.
Firstly, contours may not extend beyond the outermost point within the
diagram, which may fall some distance from the margin of the plot if a
low `resolution`

is used. Setting a negative `tolerance`

parameter allows
these contours to extend closer to (or beyond) the margin of the plot.

Individual points cannot fall outside the margins of the ternary diagram,
but their associated kernels can. In order to sample regions of the kernels
that have "bled" outside the ternary diagram, each point's value is
calculated by summing the point density at that point and at equivalent
points outside the ternary diagram, "reflected" across the margin of
the plot (see function `ReflectedEquivalents`

). This correction can be
disabled by setting the `edgeCorrection`

parameter to `FALSE`

.

A model based on a triangular grid may be more appropriate in certain situations, but is non-trivial to implement; if this distinction is important to you, please let the maintainers known by opening a Github issue.

Other contour plotting functions:
`ColourTernary()`

,
`TernaryContour()`

,
`TernaryPointValues()`

```
TernaryPlot(axis.labels = seq(0, 10, by = 1))
nPoints <- 400L
coordinates <- cbind(abs(rnorm(nPoints, 2, 3)),
abs(rnorm(nPoints, 1, 1.5)),
abs(rnorm(nPoints, 1, 0.5)))
ColourTernary(TernaryDensity(coordinates, resolution = 10L))
TernaryPoints(coordinates, col = "red", pch = ".")
TernaryDensityContour(coordinates, resolution = 30L)
```