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 vectors of all values of a, b and
# c at which it will be evaluated.
# Instead of
BadMax <- function (a, b, c) {
max(a, b, c) # Not vectorized
# Will return the single maximum of ALL a, b and c coordinates
}
# Use
GoodMax <- function (a, b, c) {
pmax(a, b, c) # Vectorized
# Will return the maximum of each trio of a, b and c coordinates
}
TernaryPlot(alab = "a", blab = "b", clab = "c")
ColourTernary(TernaryPointValues(GoodMax))
TernaryContour(GoodMax)
# When a vectorized version of a function is not available, you will need to
# apply the function to each combination of a, b and c in turn:
GeneralMax <- function (a, b, c) {
abc.matrix <- rbind(a, b, c) # Matrix where each column gives an a,b,c trio
apply(abc.matrix, 2, max) # Apply non-vectorized function to each trio
# Returns a vector with the maximum value of a,b,c at each coordinate.
}
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", xpd = NA), # Tweak legend display
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)
Run the code above in your browser using DataLab