From a8b3285030c44667327bc2e176d1a006aba5b77e Mon Sep 17 00:00:00 2001 From: ms609 <1695515+ms609@users.noreply.github.com> Date: Mon, 5 Aug 2024 12:29:37 +0100 Subject: [PATCH] 2.3.2.9000: plot c(0,0,0) at origin --- DESCRIPTION | 2 +- NEWS.md | 3 +++ R/Coordinates.R | 8 ++++++-- inst/TernaryApp | 2 +- tests/testthat/test-Coordinates.R | 4 ++++ 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index bc0d44a8..37bceee1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: Ternary -Version: 2.3.2 +Version: 2.3.2.9000 Title: Create Ternary and Holdridge Plots Description: Plots ternary diagrams (simplex plots / Gibbs triangles) and Holdridge life zone plots using the diff --git a/NEWS.md b/NEWS.md index 61e3c412..9b3d1b9a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,6 @@ +# Ternary v2.3.2.9000 (development) +- Support plotting of `c(0, 0, 0)`, at origin. + # Ternary v2.3.2 (2024-07-25) - Document reading data into R in vignette for new users. - Provide package anchors in documentation pages. diff --git a/R/Coordinates.R b/R/Coordinates.R index b39e4307..3074822d 100644 --- a/R/Coordinates.R +++ b/R/Coordinates.R @@ -83,13 +83,17 @@ TernaryToXY.numeric <- function( c(3, 2, 1), c(2, 3, 1) )] + if (!any(as.logical(abc))) { + # abc == c(0, 0, 0) + abc <- c(1, 1, 1) + } - x_deviation <- abc[3] / sum(abc) + x_deviation <- abc[[3]] / sum(abc) if (x_deviation == 1) { x <- cos(pi / 6) y <- 0 } else { - y_deviation <- (abc[1] - abc[2]) / sum(abc[1:2]) + y_deviation <- (abc[[1]] - abc[[2]]) / sum(abc[1:2]) x <- x_deviation * cos(pi / 6) y <- y_deviation * (1 - x_deviation) / 2 } diff --git a/inst/TernaryApp b/inst/TernaryApp index 5ad7e38f..e4d78431 160000 --- a/inst/TernaryApp +++ b/inst/TernaryApp @@ -1 +1 @@ -Subproject commit 5ad7e38ff61c806873adcf1e5ecfa570f1ee8751 +Subproject commit e4d78431d8bee248f229573e6d2010921cd31421 diff --git a/tests/testthat/test-Coordinates.R b/tests/testthat/test-Coordinates.R index 4fed92de..36da2487 100644 --- a/tests/testthat/test-Coordinates.R +++ b/tests/testthat/test-Coordinates.R @@ -141,6 +141,10 @@ test_that("OutsidePlot() works", { expect_true(OutsidePlot(0, 0.8, tolerance = 0.05)) }) +test_that("Corner cases are correct", { + expect_equal(TernaryToXY(c(0, 0, 0)), TernaryToXY(c(2, 2, 2))) +}) + test_that("Errors are handled nicely", { expect_error(TernaryCoords(1:3, direction = 5)) expect_error(TernaryXRange(direction = 5))