Skip to content

use approx instead of nearest match when placing secondary ticks #4857

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

Merged
merged 3 commits into from
Jun 15, 2022
Merged
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* Secondary axis ticks are now positioned more precisely, removing small visual
artefacts with alignment between grid and ticks (@thomasp85, #3576)

* Improve `stat_function` documentation regarding `xlim` argument. (@92amartins, #4474)

* `qplot()` is now formally deprecated (@yutannihilation, #3956).
Expand Down
16 changes: 12 additions & 4 deletions R/axis-secondary.R
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
# Create mapping between primary and secondary range
full_range <- self$transform_range(old_range)

# Remove duplicates in the expanded area of the range that can arise if
# the transformation is non-monotonic in the expansion. The split ensures
# the middle duplicated are kept
duplicates <- c(
!duplicated(full_range[seq_len(self$detail/2)], fromLast = TRUE),
!duplicated(full_range[-seq_len(self$detail/2)])
)
old_range <- old_range[duplicates]
full_range <- full_range[duplicates]

# Get break info for the secondary axis
new_range <- range(full_range, na.rm = TRUE)

Expand All @@ -226,8 +236,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,

# Map the break values back to their correct position on the primary scale
if (!is.null(range_info$major_source)) {
old_val <- lapply(range_info$major_source, function(x) which.min(abs(full_range - x)))
old_val <- old_range[unlist(old_val)]
old_val <- approx(full_range, old_range, range_info$major_source)$y
old_val_trans <- scale$trans$transform(old_val)

# rescale values from 0 to 1
Expand All @@ -243,8 +252,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
}

if (!is.null(range_info$minor_source)) {
old_val_minor <- lapply(range_info$minor_source, function(x) which.min(abs(full_range - x)))
old_val_minor <- old_range[unlist(old_val_minor)]
old_val_minor <- approx(full_range, old_range, range_info$minor_source)$y
old_val_minor_trans <- scale$trans$transform(old_val_minor)

range_info$minor[] <- round(
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 22 additions & 22 deletions tests/testthat/_snaps/coord-transform/sec-axis-with-coord-trans.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions tests/testthat/_snaps/guides/guide-axis-customization.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading