diff --git a/NEWS.md b/NEWS.md index 2c6fba4..ba0a681 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # isopleuros 1.2.0.9000 ## Enhancements -* Allow to center and scale label positions. +* Allow to center and scale label coordinates. +* Generate diagnostic messages if coordinates are not centered/scaled when they should be. # isopleuros 1.2.0 ## Enhancements diff --git a/R/coordinates.R b/R/coordinates.R index 373f81c..500b17d 100644 --- a/R/coordinates.R +++ b/R/coordinates.R @@ -15,6 +15,8 @@ setMethod( n <- length(x) assert_length(y, n) assert_length(z, n) + assert_center(center) + assert_scale(scale) ## Missing values if (missing) { diff --git a/R/isopleuros-internal.R b/R/isopleuros-internal.R index f4324fc..ec1231d 100644 --- a/R/isopleuros-internal.R +++ b/R/isopleuros-internal.R @@ -63,3 +63,21 @@ assert_length <- function(x, expected) { } invisible(x) } + +assert_center <- function(x, current = getOption("isopleuros.center")) { + ok <- isTRUE(x) || is.numeric(x) + if (!ok && is.numeric(current) && !all(current == 1)) { + msg <- "The current plot has been centered, but your data doesn't seem to be." + message(msg) + } + invisible(x) +} + +assert_scale <- function(x, current = getOption("isopleuros.scale")) { + ok <- isTRUE(x) || is.numeric(x) + if (!ok && is.numeric(current) && !all(current == 1)) { + msg <- "The current plot has been scaled, but your data doesn't seem to be." + message(msg) + } + invisible(x) +} diff --git a/R/ternary_grid.R b/R/ternary_grid.R index 5f76dbd..46825ea 100644 --- a/R/ternary_grid.R +++ b/R/ternary_grid.R @@ -54,8 +54,8 @@ ternary_grid <- function(primary = NULL, secondary = NULL, for (i in x) { start <- matrix(data = c(i, 0, 1 - i, 1 - i, i, 0, 0, 1 - i, i), ncol = 3) end <- matrix(data = c(i, 1 - i, 0, 0, i, 1 - i, 1 - i, 0, i), ncol = 3) - start <- coordinates_ternary(start) - end <- coordinates_ternary(end) + start <- list(x = start[, 2] + start[, 3] / 2, y = start[, 3] * sqrt(3) / 2) + end <- list(x = end[, 2] + end[, 3] / 2, y = end[, 3] * sqrt(3) / 2) mapply( FUN = function(x_from, x_to, y_from, y_to, n, center, scale) { diff --git a/R/ternary_plot.R b/R/ternary_plot.R index 3474629..24bf2c3 100644 --- a/R/ternary_plot.R +++ b/R/ternary_plot.R @@ -48,6 +48,10 @@ setMethod( } graphics::plot.window(xlim = lim$x, ylim = lim$y, asp = 1) + ## Reset center and scale + options(isopleuros.center = NULL) + options(isopleuros.scale = NULL) + ## Compute ternary coordinates pt <- coordinates_ternary(x = x, y = y, z = z, center = center, scale = scale) diff --git a/inst/tinytest/test_scale.R b/inst/tinytest/test_scale.R index c4a7258..ea9d5b9 100644 --- a/inst/tinytest/test_scale.R +++ b/inst/tinytest/test_scale.R @@ -1,3 +1,15 @@ +ternary_plot(lava, center = TRUE, scale = FALSE) +expect_message(ternary_points(lava), "The current plot has been centered") + +ternary_plot(lava, center = FALSE, scale = TRUE) +expect_message(ternary_points(lava), "The current plot has been scaled") + +ternary_plot(lava, center = TRUE, scale = TRUE) +expect_message(ternary_points(lava)) + +ternary_plot(lava, center = FALSE, scale = FALSE) +expect_silent(ternary_points(lava)) + if (at_home()) { using("tinysnapshot") options(tinysnapshot_device = "svglite")