Skip to content
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