Skip to content

Commit

Permalink
n.breaks propagate to sec.axis (#5973)
Browse files Browse the repository at this point in the history
* capture `n.breaks` parameter upon initialisation

* add test

* add news bullet
  • Loading branch information
teunbrand committed Aug 26, 2024
1 parent 5482939 commit 332a8ea
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ggplot2 (development version)

* Secondary axes respect `n.breaks` setting in continuous scales (@teunbrand, #4483).
* Layers can have names (@teunbrand, #4066).
* (internal) improvements to `pal_qualitative()` (@teunbrand, #5013)
* `coord_radial(clip = "on")` clips to the panel area when the graphics device
Expand Down
8 changes: 7 additions & 1 deletion R/axis-secondary.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,13 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
if (scale$is_discrete()) {
self$breaks <- scale$get_breaks()
} else {
self$breaks <- scale$get_transformation()$breaks
breaks <- scale$get_transformation()$breaks
n_breaks <- scale$n.breaks
if (!is.null(n_breaks) && "n" %in% fn_fmls_names(breaks)) {
self$breaks <- function(x) breaks(x, n = n_breaks)
} else {
self$breaks <- breaks
}
}
}
if (is.derived(self$labels)) self$labels <- scale$labels
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test-sec-axis.R
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,22 @@ test_that("discrete scales can have secondary axes", {
expect_equal(y$.value, c(1.5, 2.5), ignore_attr = TRUE)
expect_equal(y$.label, c("grault", "garply"))
})

test_that("n.breaks is respected by secondary axes (#4483)", {

b <- ggplot_build(
ggplot(data.frame(x = c(0, 10)), aes(x, x)) +
scale_y_continuous(
n.breaks = 11,
sec.axis = sec_axis(~.x*100)
)
)

# We get scale breaks via guide data
prim <- get_guide_data(b, "y")
sec <- get_guide_data(b, "y.sec")

expect_equal(prim$.value, sec$.value) # .value is in primary scale
expect_equal(prim$.label, as.character(seq(0, 10, length.out = 11)))
expect_equal(sec$.label, as.character(seq(0, 1000, length.out = 11)))
})

0 comments on commit 332a8ea

Please sign in to comment.