Skip to content

Commit 332a8ea

Browse files
authored
n.breaks propagate to sec.axis (#5973)
* capture `n.breaks` parameter upon initialisation * add test * add news bullet
1 parent 5482939 commit 332a8ea

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# ggplot2 (development version)
22

3+
* Secondary axes respect `n.breaks` setting in continuous scales (@teunbrand, #4483).
34
* Layers can have names (@teunbrand, #4066).
45
* (internal) improvements to `pal_qualitative()` (@teunbrand, #5013)
56
* `coord_radial(clip = "on")` clips to the panel area when the graphics device

R/axis-secondary.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,13 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
188188
if (scale$is_discrete()) {
189189
self$breaks <- scale$get_breaks()
190190
} else {
191-
self$breaks <- scale$get_transformation()$breaks
191+
breaks <- scale$get_transformation()$breaks
192+
n_breaks <- scale$n.breaks
193+
if (!is.null(n_breaks) && "n" %in% fn_fmls_names(breaks)) {
194+
self$breaks <- function(x) breaks(x, n = n_breaks)
195+
} else {
196+
self$breaks <- breaks
197+
}
192198
}
193199
}
194200
if (is.derived(self$labels)) self$labels <- scale$labels

tests/testthat/test-sec-axis.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,22 @@ test_that("discrete scales can have secondary axes", {
400400
expect_equal(y$.value, c(1.5, 2.5), ignore_attr = TRUE)
401401
expect_equal(y$.label, c("grault", "garply"))
402402
})
403+
404+
test_that("n.breaks is respected by secondary axes (#4483)", {
405+
406+
b <- ggplot_build(
407+
ggplot(data.frame(x = c(0, 10)), aes(x, x)) +
408+
scale_y_continuous(
409+
n.breaks = 11,
410+
sec.axis = sec_axis(~.x*100)
411+
)
412+
)
413+
414+
# We get scale breaks via guide data
415+
prim <- get_guide_data(b, "y")
416+
sec <- get_guide_data(b, "y.sec")
417+
418+
expect_equal(prim$.value, sec$.value) # .value is in primary scale
419+
expect_equal(prim$.label, as.character(seq(0, 10, length.out = 11)))
420+
expect_equal(sec$.label, as.character(seq(0, 1000, length.out = 11)))
421+
})

0 commit comments

Comments
 (0)