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

n.breaks propagate to sec.axis #5973

Merged
merged 4 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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).
* `stat_bin()` now accepts functions for argument `breaks` (@aijordan, #4561)
* (internal) The plot's layout now has a coord parameter that is used to
prevent setting up identical panel parameters (#5427)
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)))
})
Loading