Draws contour lines to depict the value of a function in ternary space.
TernaryContour(
Func,
resolution = 96L,
direction = getOption("ternDirection", 1L),
region = getOption("ternRegion", ternRegionDefault),
within = NULL,
filled = FALSE,
legend,
legend... = list(),
nlevels = 10,
levels = pretty(zlim, nlevels),
zlim,
color.palette = function(n) hcl.colors(n, palette = "viridis", alpha = 0.6),
fill.col = color.palette(length(levels) - 1),
func... = list(),
...
)
Function taking vectors of coordinates a
, b
and c
, which
returns a numeric vector whose value at each coordinate will be depicted.
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.
(optional) Integer specifying the direction that the current ternary plot should point: 1, up; 2, right; 3, down; 4, left.
(optional) Named list of length two specifying the the
min
imum and max
imum values of each ternary axis to be drawn
(e.g. list(min = c(40, 0, 0), max = c(100, 60, 60)
);
or a set of coordinates in a format accepted by TernaryPoints()
.
The plotted region will correspond to the smallest equilateral triangle
that encompasses the specified ranges or coordinates.
List or matrix of x, y coordinates within which contours
should be evaluated, in any format supported by
xy.coords(x = within)
.
If NULL
, defaults to a region slightly smaller than the ternary plot.
The $hull
entry generated by TriangleInHull()
may also be used.
Logical; if TRUE
, contours will be filled
(using .filled.contour()
.).
Character vector specifying annotations for colour scale.
If not provided, no colour legend is displayed.
Specify TRUE
to generate automatically, or a single integer to generate
legend
annotations.
List of additional parameters to send to
SpectrumLegend()
.
parameters to pass to
contour()
.
parameters to pass to
.filled.contour()
.
Sent as col
parameter to
.filled.contour()
.
Computed from color.palette
if not specified.
List of additional parameters to send to Func()
.
TernaryContour()
invisibly returns a list containing:
x
,y
: the Cartesian coordinates of each evaluated point;
z
: The value of Func()
at each coordinate.
Other contour plotting functions:
ColourTernary()
,
TernaryDensityContour()
,
TernaryPointValues()
FunctionToContour <- function (a, b, c) {
a - c + (4 * a * b) + (27 * a * b * c)
}
# Set up plot
originalPar <- par(mar = rep(0, 4))
TernaryPlot(alab = "a", blab = "b", clab = "c")
values <- TernaryPointValues(FunctionToContour, resolution = 24L)
ColourTernary(
values,
legend = signif(seq(max(values), min(values), length.out = 4), 2),
bty = "n"
)
TernaryContour(FunctionToContour, resolution = 36L)
# Note that FunctionToContour is sent a vector.
# Instead of
BadMax <- function (a, b, c) {
max(a, b, c)
}
# Use
GoodMax <- function (a, b, c) {
pmax(a, b, c)
}
TernaryPlot(alab = "a", blab = "b", clab = "c")
ColourTernary(TernaryPointValues(GoodMax))
TernaryContour(GoodMax)
# Or, for a generalizable example,
GeneralMax <- function (a, b, c) {
apply(rbind(a, b, c), 2, max)
}
TernaryPlot(alab = "a", blab = "b", clab = "c")
# Fill the contour areas, rather than using tiles
TernaryContour(GeneralMax, filled = TRUE,
legend = c("Max", "...", "Min"), legend... = list(bty = "n"),
fill.col = hcl.colors(14, palette = "viridis", alpha = 0.6))
# Re-draw edges of plot triangle over fill
TernaryPolygon(diag(3))
# Restore plotting parameters
par(originalPar)