Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Aug 23, 2023
1 parent 1ec4519 commit 2aa06ec
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 19 deletions.
4 changes: 2 additions & 2 deletions R/biplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ viz_biplot <- function(coord_row, coord_col, rows = TRUE, columns = TRUE,
graphics::plot.new()

## Set plotting coordinates
xlim <- range(coord_row$x, coord_col$x)
ylim <- range(coord_row$y, coord_col$y)
xlim <- range(coord_row$x, coord_col$x, na.rm = TRUE, finite = TRUE)
ylim <- range(coord_row$y, coord_col$y, na.rm = TRUE, finite = TRUE)
graphics::plot.window(xlim = xlim, ylim = ylim, asp = 1)

## Evaluate pre-plot expressions
Expand Down
4 changes: 1 addition & 3 deletions R/dimensio-internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
if (!is.null(x)) x else y
}

`%notin%` <- Negate(`%in%`)

recycle <- function(x, n, verbose = getOption("dimensio.verbose")) {
if (length(x) == n) return(x)
if (length(x) >= n) return(x[seq_len(n)])

if (verbose) {
arg <- deparse(substitute(x))
Expand Down
49 changes: 35 additions & 14 deletions R/viz_coordinates.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ setMethod(
graphics::plot.new()

## Set plotting coordinates
xlim <- if (is_scaled(x)) c(-1, 1) else range(coord$x)
ylim <- if (is_scaled(x)) c(-1, 1) else range(coord$y)
xlim <- range(coord$x, na.rm = TRUE, finite = TRUE)
if (is_scaled(x)) xlim <- c(-1, 1)
ylim <- range(coord$y, na.rm = TRUE, finite = TRUE)
if (is_scaled(x)) ylim <- c(-1, 1)
graphics::plot.window(xlim = xlim, ylim = ylim, asp = 1)

## Evaluate pre-plot expressions
Expand Down Expand Up @@ -211,8 +213,8 @@ viz_points <- function(x, margin, axes, active = TRUE, sup = TRUE, labels = FALS
graphics::plot.new()

## Set plotting coordinates
xlim <- range(coord$x, na.rm = TRUE)
ylim <- range(coord$y, na.rm = TRUE)
xlim <- range(coord$x, na.rm = TRUE, finite = TRUE)
ylim <- range(coord$y, na.rm = TRUE, finite = TRUE)
graphics::plot.window(xlim = xlim, ylim = ylim, asp = 1)

## Evaluate pre-plot expressions
Expand Down Expand Up @@ -368,10 +370,20 @@ prepare_param <- function(x, map_color = NULL, map_shape = NULL,
data.frame(col = col, pch = pch, cex = cex, lty = lty, lwd = lwd)
}

scale_range <- function(x, to = c(0, 1), from = range(x, na.rm = TRUE)) {
#' Rescale Continuous Vector
#'
#' Rescales continuous vector to have specified minimum and maximum.
#' @param x A continuous `vector` of values to manipulate.
#' @param to A length-two [`numeric`] vector specifying the output range.
#' @param from A length-two [`numeric`] vector specifying the input range.
#' @return A [`numeric`] vector.
#' @keywords internal
#' @noRd
scale_range <- function(x, to = c(0, 1), from = range(x, na.rm = TRUE, finite = TRUE)) {
(x - from[1]) / diff(from) * diff(to) + to[1]
}
scale_color <- function(x, n, col = NULL, alpha = FALSE) {

scale_color <- function(x, n = length(x), col = NULL, alpha = FALSE) {
if (is.null(x)) {
col <- rep_len(col %||% graphics::par("col"), n)
return(col)
Expand All @@ -384,6 +396,8 @@ scale_color <- function(x, n, col = NULL, alpha = FALSE) {
x <- scale_range(x) # Rescale to [0,1]
col <- grDevices::colorRamp(col)(x)
col <- grDevices::rgb(col[, 1], col[, 2], col[, 3], maxColorValue = 255)
## Alpha transparency
if (alpha) col <- grDevices::adjustcolor(col, alpha.f = alpha)
} else {
## Discrete scale
n_col <- length(unique(x))
Expand All @@ -393,27 +407,34 @@ scale_color <- function(x, n, col = NULL, alpha = FALSE) {
col <- col[as.factor(x)]
}

## Alpha transparency
if (alpha) col <- grDevices::adjustcolor(col, alpha.f = alpha)

col
}
scale_symbole <- function(x, n, symb = NULL, default = 1) {
scale_symbole <- function(x, n = length(x), symb = NULL, default = 1) {
if (is.null(x)) {
symb <- rep_len(symb %||% default, n)
return(symb)
}

if (is.double(x)) { # Continuous scale
warning("Continuous value supplied to discrete scale.", call. = FALSE)
}

n_symb <- length(unique(x))
if (is.null(symb)) symb <- seq_len(n_symb)
symb <- recycle(symb, n_symb)
symb <- symb[as.factor(x)]
symb
}
scale_size <- function(x, n, size = NULL, default = 1) {
size <- size %||% default
if (is.null(x)) return(rep_len(size, n))
scale_size <- function(x, n = length(x), size = NULL, default = 1) {
if (is.null(x)) {
size <- rep_len(size %||% default, n)
return(size)
}

if (!is.double(x)) { # Discrete scale
warning("Discrete value supplied to continuous scale.", call. = FALSE)
}

size <- size + x / max(x)
if (is.null(size)) size <- default + x / max(x)
size
}
95 changes: 95 additions & 0 deletions inst/tinytest/test_scale.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
xcont <- seq(1, 2, by = 0.2)
xint <- c(1L, 2L, 3L, 4L, 5L)
xcha <- c("A", "B", "C", "D", "E")

# Rescale continuous vector ====================================================
expect_identical(dimensio:::scale_range(5:10), c(0, 0.2, 0.4, 0.6, 0.8, 1))

# Color scale ==================================================================
expect_identical(
dimensio:::scale_color(x = NULL, n = 5, col = "red"),
c("red", "red", "red", "red", "red")
)

## Continuous scale ------------------------------------------------------------
expect_length(dimensio:::scale_color(xcont), 6L)

## Default palette
expect_identical(
dimensio:::scale_color(xcont),
c("#FFFFC8", "#FAE092", "#F6B024", "#EF6F00", "#C92700", "#7D0025")
)
## Custom palette
expect_identical(
dimensio:::scale_color(xcont, col = grDevices::hcl.colors(12, "BluGrn")),
c("#14505C", "#247172", "#3D9287", "#5EB395", "#90CFA4", "#C7E5BE")
)
## Alpha transparency
expect_identical(
dimensio:::scale_color(xcont, col = grDevices::hcl.colors(12, "BluGrn"), alpha = TRUE),
c("#14505CFF", "#247172FF", "#3D9287FF", "#5EB395FF", "#90CFA4FF", "#C7E5BEFF")
)

## Discrete scale --------------------------------------------------------------
expect_length(dimensio:::scale_color(xint), 5L)
expect_identical(dimensio:::scale_color(xint), dimensio:::scale_color(xcha))

## Default palette
expect_identical(
dimensio:::scale_color(xint),
c("#4B0055", "#00588B", "#009B95", "#53CC67", "#FDE333")
)
## Custom palette
expect_equivalent(
dimensio:::scale_color(xint, col = grDevices::palette.colors(9, "Okabe-Ito")),
c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442")
)

## Recycle
expect_message(dimensio:::scale_color(xint, col = "black"))
expect_identical(
suppressMessages(dimensio:::scale_color(xint, col = "black")),
c("black", "black", "black", "black", "black")
)

# Shape scale ==================================================================
expect_identical(
dimensio:::scale_symbole(x = NULL, n = 5, symb = NULL, default = 16),
c(16, 16, 16, 16, 16)
)

expect_length(dimensio:::scale_symbole(xint), 5L)
expect_identical(dimensio:::scale_symbole(xint), dimensio:::scale_symbole(xcha))

expect_identical(dimensio:::scale_symbole(xint, symb = NULL, default = 1), 1:5)
expect_identical(dimensio:::scale_symbole(xint, symb = 15:19, default = 1), 15:19)

expect_message(dimensio:::scale_symbole(xint, symb = 16))
expect_identical(
suppressMessages(dimensio:::scale_symbole(xint, symb = 16)),
c(16, 16, 16, 16, 16)
)

expect_warning(
dimensio:::scale_symbole(xcont),
"Continuous value supplied to discrete scale."
)

# Size scale ===================================================================
expect_identical(
dimensio:::scale_size(x = NULL, n = 5, size = NULL, default = 2),
c(2, 2, 2, 2, 2)
)

expect_length(dimensio:::scale_size(xcont), 6L)

expect_identical(
dimensio:::scale_size(xcont, size = NULL, default = 1),
c(1.5, 1.6, 1.7, 1.8, 1.9, 2)
)
expect_identical(dimensio:::scale_size(xcont, size = 1:6, default = 1), 1:6)

expect_warning(
dimensio:::scale_size(xint),
"Discrete value supplied to continuous scale."
)

0 comments on commit 2aa06ec

Please sign in to comment.