Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in guide_axis_logticks(negative.small) #6126

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* Better handling of the `guide_axis_logticks(negative.small)` parameter when
scale limits have small maximum (@teunbrand, #6121).
* Fixed bug where the `ggplot2::`-prefix did not work with `stage()`
(@teunbrand, #6104).
* New `get_labs()` function for retrieving completed plot labels
Expand Down
27 changes: 14 additions & 13 deletions R/guide-axis-logticks.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ NULL
#' keep the default `NULL` argument.
#' @param negative.small When the scale limits include 0 or negative numbers,
#' what should be the smallest absolute value that is marked with a tick?
#' If `NULL` (default), will be the smallest of 0.1 or 0.1 times the absolute
#' scale maximum.
#' @param short.theme A theme [element][element_line()] for customising the
#' display of the shortest ticks. Must be a line or blank element, and
#' it inherits from the `axis.minor.ticks` setting for the relevant position.
Expand Down Expand Up @@ -69,7 +71,7 @@ guide_axis_logticks <- function(
mid = 1.5,
short = 0.75,
prescale.base = NULL,
negative.small = 0.1,
negative.small = NULL,
short.theme = element_line(),
expanded = TRUE,
cap = "none",
Expand Down Expand Up @@ -187,28 +189,27 @@ GuideAxisLogticks <- ggproto(
# Reconstruct original range
limits <- transformation$inverse(scale$get_limits())
has_negatives <- any(limits <= 0)

if (!has_negatives) {
start <- floor(log10(min(limits))) - 1L
end <- ceiling(log10(max(limits))) + 1L
} else {
params$negative_small <- params$negative_small %||% 0.1
start <- floor(log10(abs(params$negative_small)))
end <- ceiling(log10(max(abs(limits)))) + 1L
if (has_negatives) {
large <- max(abs(limits))
small <- params$negative_small %||% min(c(1, large) * 0.1)
limits <- sort(c(small * 10, large))
}

start <- floor(log10(min(limits))) - 1L
end <- ceiling(log10(max(limits))) + 1L

# Calculate tick marks
tens <- 10^seq(start, end, by = 1)
tens <- 10^seq(start, end, by = 1L)
fives <- tens * 5
ones <- as.vector(outer(setdiff(2:9, 5), tens))

if (has_negatives) {
# Filter and mirror ticks around 0
tens <- tens[tens >= params$negative_small]
tens <- tens[tens >= small]
tens <- c(tens, -tens, 0)
fives <- fives[fives >= params$negative_small]
fives <- fives[fives >= small]
fives <- c(fives, -fives)
ones <- ones[ones >= params$negative_small]
ones <- ones[ones >= small]
Comment on lines +208 to +212
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is something unnerving about defining a variable in one if block (small), and then using it in another, even though the blocks have the same condition.

To be clear, it is perfectly valid, but I think it is a brittle structure

ones <- c(ones, -ones)
}

Expand Down
6 changes: 4 additions & 2 deletions man/guide_axis_logticks.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tests/testthat/test-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ test_that("guide_axis_logticks calculates appropriate ticks", {
expect_equal(unlog, c(-rev(outcome), 0, outcome))
expect_equal(key$.type, rep(c(1,2,3), c(7, 4, 28)))

# Test very small pseudo_log (#6121)
scale <- test_scale(transform_pseudo_log(sigma = 1e-5), c(0, 1e-10))
key <- train_guide(guide_axis_logticks(), scale)$logkey
expect_gte(nrow(key), 1)

# Test expanded argument
scale <- test_scale(transform_log10(), c(20, 900))
scale$continuous_range <- c(1, 3)
Expand Down
Loading