Skip to content

Commit

Permalink
declare trans as function rather than strings
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand committed Oct 28, 2023
1 parent 5b3271f commit 665e718
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions tests/testthat/test-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ test_that("guide_colourbar warns about discrete scales", {

test_that("guide_axis_logticks calculates appropriate ticks", {

test_scale <- function(trans = "identity", limits = c(NA, NA)) {
test_scale <- function(trans = identity_trans(), limits = c(NA, NA)) {
scale <- scale_x_continuous(trans = trans)
scale$train(scale$transform(limits))
view_scale_primary(scale)
Expand All @@ -362,28 +362,28 @@ test_that("guide_axis_logticks calculates appropriate ticks", {
outcome <- c((1:10)*10, (2:10)*100)

# Test the classic log10 transformation
scale <- test_scale("log10", c(10, 1000))
scale <- test_scale(log10_trans(), c(10, 1000))
key <- train_guide(guide, scale)$logkey

expect_equal(sort(key$x), log10(outcome))
expect_equal(key$.type, rep(c(1,2,3), c(3, 2, 14)))

# Test compound transformation
scale <- test_scale(c("log10", "reverse"), c(10, 1000))
scale <- test_scale(compose_trans(log10_trans(), reverse_trans()), c(10, 1000))
key <- train_guide(guide, scale)$logkey

expect_equal(sort(key$x), -log10(rev(outcome)))

# Test transformation with negatives
scale <- test_scale("pseudo_log", c(-1000, 1000))
scale <- test_scale(pseudo_log_trans(), c(-1000, 1000))
key <- train_guide(guide, scale)$logkey

unlog <- sort(pseudo_log_trans()$inverse(key$x))
expect_equal(unlog, c(-rev(outcome), 0, outcome))
expect_equal(key$.type, rep(c(1,2,3), c(7, 4, 28)))

# Test expanded argument
scale <- test_scale("log10", c(20, 900))
scale <- test_scale(log10_trans(), c(20, 900))
scale$continuous_range <- c(1, 3)

guide <- guide_axis_logticks(expanded = TRUE)
Expand All @@ -404,7 +404,7 @@ test_that("guide_axis_logticks calculates appropriate ticks", {
expect_equal(sort(key$x), log2(outcome))

# Should warn when scale also has transformation
scale <- test_scale("log10", limits = c(10, 1000))
scale <- test_scale(log10_trans(), limits = c(10, 1000))
expect_snapshot_warning(train_guide(guide, scale)$logkey)

})
Expand Down Expand Up @@ -618,12 +618,12 @@ test_that("logticks look as they should", {

p <- ggplot(data.frame(x = c(-100, 100), y = c(10, 1000)), aes(x, y)) +
geom_point() +
scale_y_continuous(trans = c("log10", "reverse"),
scale_y_continuous(trans = compose_trans(log10_trans(), reverse_trans()),
expand = expansion(add = 0.5)) +
scale_x_continuous(
breaks = c(-100, -10, -1, 0, 1, 10, 100)
) +
coord_trans(x = "pseudo_log") +
coord_trans(x = pseudo_log_trans()) +
theme_test() +
theme(axis.line = element_line(colour = "black"),
panel.border = element_blank(),
Expand Down

0 comments on commit 665e718

Please sign in to comment.