# Generate some example data
nPoints <- 400L
coordinates <- cbind(abs(rnorm(nPoints, 2, 3)),
abs(rnorm(nPoints, 1, 1.5)),
abs(rnorm(nPoints, 1, 0.5)))
# Set up plot
oPar <- par(mar = rep(0, 4))
TernaryPlot(axis.labels = seq(0, 10, by = 1))
# Colour background by density
ColourTernary(TernaryDensity(coordinates, resolution = 10L),
legend = TRUE, bty = "n", title = "Density")
# Plot points
TernaryPoints(coordinates, col = "red", pch = ".")
# Contour by density
TernaryDensityContour(coordinates, resolution = 30L)
# The following demonstrates the behaviour of the density estimates when
# points fall on boundaries of the triangular grid cells; text denotes the
# number of points within the cell, with cells straddling _n_ cells
# contributing 1/_n_ of a point to each cell straddled.
coordinates <- list(middle = c(1, 1, 1),
top = c(3, 0, 0),
belowTop = c(2, 1, 1),
leftSideSolid = c(9, 2, 9),
leftSideSolid2 = c(9.5, 2, 8.5),
right3way = c(1, 2, 0),
rightEdge = c(2.5, 0.5, 0),
leftBorder = c(1, 1, 4),
topBorder = c(2, 1, 3),
rightBorder = c(1, 2, 3)
)
par(mfrow = c(2, 2), mar = rep(0.2, 4))
TernaryPlot(grid.lines = 3, axis.labels = 1:3, point = "up")
values <- TernaryDensity(coordinates, resolution = 3L)
ColourTernary(values)
TernaryPoints(coordinates, col = "red")
text(values[1, ], values[2, ], paste(values[3, ], "/ 6"), cex = 0.8)
TernaryPlot(grid.lines = 3, axis.labels = 1:3, point = "right")
values <- TernaryDensity(coordinates, resolution = 3L)
ColourTernary(values)
TernaryPoints(coordinates, col = "red")
text(values[1, ], values[2, ], paste(values[3, ], "/ 6"), cex = 0.8)
TernaryPlot(grid.lines = 3, axis.labels = 1:3, point = "down")
values <- TernaryDensity(coordinates, resolution = 3L)
ColourTernary(values)
TernaryPoints(coordinates, col = "red")
text(values[1, ], values[2, ], paste(values[3, ], "/ 6"), cex = 0.8)
TernaryPlot(grid.lines = 3, axis.labels = 1:3, point = "left")
values <- TernaryDensity(coordinates, resolution = 3L)
ColourTernary(values)
TernaryPoints(coordinates, col = "red")
text(values[1, ], values[2, ], paste(values[3, ], "/ 6"), cex = 0.8)
TernaryDensityContour(t(vapply(coordinates, I, double(3L))),
resolution = 24L, tolerance = -0.02, col = "orange")
# Reset plotting parameters
par(oPar)
Run the code above in your browser using DataLab