Skip to content

Discrete secondary axes #5620

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 12 commits into from
May 20, 2024
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
(@teunbrand, #5856).
* New helper function `ggpar()` to translate ggplot2's interpretation of
graphical parameters to {grid}'s interpretation (@teunbrand, #5866).
* `scale_{x/y}_discrete()` can now accept a `sec.axis`. It is recommended to
only use `dup_axis()` to set custom breaks or labels, as discrete variables
cannot be transformed (@teunbrand, #3171).

# ggplot2 3.5.1

Expand Down
31 changes: 24 additions & 7 deletions R/axis-secondary.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ sec_axis <- function(transform = NULL,
#' @rdname sec_axis
#'
#' @export
dup_axis <- function(transform = ~., name = derive(), breaks = derive(),
dup_axis <- function(transform = identity, name = derive(), breaks = derive(),
labels = derive(), guide = derive(), trans = deprecated()) {
sec_axis(transform, trans = trans, name, breaks, labels, guide)
}
Expand All @@ -130,6 +130,11 @@ is.sec_axis <- function(x) {

set_sec_axis <- function(sec.axis, scale) {
if (!is.waive(sec.axis)) {
if (scale$is_discrete()) {
if (!identical(.subset2(sec.axis, "trans"), identity)) {
cli::cli_abort("Discrete secondary axes must have the {.fn identity} transformation.")
}
}
if (is.formula(sec.axis)) sec.axis <- sec_axis(sec.axis)
if (!is.sec_axis(sec.axis)) {
cli::cli_abort("Secondary axes must be specified using {.fn sec_axis}.")
Expand Down Expand Up @@ -179,7 +184,13 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
}
if (is.derived(self$name) && !is.waive(scale$name)) self$name <- scale$name
if (is.derived(self$breaks)) self$breaks <- scale$breaks
if (is.waive(self$breaks)) self$breaks <- scale$get_transformation()$breaks
if (is.waive(self$breaks)) {
if (scale$is_discrete()) {
self$breaks <- scale$get_breaks()
} else {
self$breaks <- scale$get_transformation()$breaks
}
}
if (is.derived(self$labels)) self$labels <- scale$labels
if (is.derived(self$guide)) self$guide <- scale$guide
},
Expand Down Expand Up @@ -214,10 +225,15 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
if (self$empty()) return()

# Test for monotonicity on unexpanded range
self$mono_test(scale)
if (!scale$is_discrete()) {
self$mono_test(scale)
breaks <- self$breaks
} else {
breaks <- scale$map(self$breaks)
}

# Get scale's original range before transformation
transformation <- scale$get_transformation()
transformation <- scale$get_transformation() %||% transform_identity()
along_range <- seq(range[1], range[2], length.out = self$detail)
old_range <- transformation$inverse(along_range)

Expand Down Expand Up @@ -245,7 +261,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
old_val_trans <- rescale(range_info$major, from = c(0, 1), to = range)
old_val_minor_trans <- rescale(range_info$minor, from = c(0, 1), to = range)
} else {
temp_scale <- self$create_scale(new_range)
temp_scale <- self$create_scale(new_range, breaks = breaks)
range_info <- temp_scale$break_info()

# Map the break values back to their correct position on the primary scale
Expand Down Expand Up @@ -294,10 +310,11 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
},

# Temporary scale for the purpose of calling break_info()
create_scale = function(self, range, transformation = transform_identity()) {
create_scale = function(self, range, transformation = transform_identity(),
breaks = self$breaks) {
scale <- ggproto(NULL, ScaleContinuousPosition,
name = self$name,
breaks = self$breaks,
breaks = breaks,
labels = self$labels,
limits = range,
expand = c(0, 0),
Expand Down
19 changes: 15 additions & 4 deletions R/scale-discrete-.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#'
#' @inheritDotParams discrete_scale -scale_name
#' @inheritParams discrete_scale
#' @param sec.axis [dup_axis()] is used to specify a secondary axis.
#' @rdname scale_discrete
#' @family position scales
#' @seealso
Expand Down Expand Up @@ -64,7 +65,8 @@
#' scale_x_discrete(labels = abbreviate)
#' }
scale_x_discrete <- function(name = waiver(), ..., expand = waiver(),
guide = waiver(), position = "bottom") {
guide = waiver(), position = "bottom",
sec.axis = waiver()) {
sc <- discrete_scale(
aesthetics = c("x", "xmin", "xmax", "xend"), name = name,
palette = identity, ...,
Expand All @@ -73,12 +75,13 @@ scale_x_discrete <- function(name = waiver(), ..., expand = waiver(),
)

sc$range_c <- ContinuousRange$new()
sc
set_sec_axis(sec.axis, sc)
}
#' @rdname scale_discrete
#' @export
scale_y_discrete <- function(name = waiver(), ..., expand = waiver(),
guide = waiver(), position = "left") {
guide = waiver(), position = "left",
sec.axis = waiver()) {
sc <- discrete_scale(
aesthetics = c("y", "ymin", "ymax", "yend"), name = name,
palette = identity, ...,
Expand All @@ -87,7 +90,7 @@ scale_y_discrete <- function(name = waiver(), ..., expand = waiver(),
)

sc$range_c <- ContinuousRange$new()
sc
set_sec_axis(sec.axis, sc)
}

# The discrete position scale maintains two separate ranges - one for
Expand Down Expand Up @@ -147,6 +150,14 @@ ScaleDiscretePosition <- ggproto("ScaleDiscretePosition", ScaleDiscrete,
expand_limits_scale(self, expand, limits)
},

sec_name = function(self) {
if (is.waive(self$secondary.axis)) {
waiver()
} else {
self$secondary.axis$name
}
},

clone = function(self) {
new <- ggproto(NULL, self)
new$range <- DiscreteRange$new()
Expand Down
8 changes: 6 additions & 2 deletions man/scale_discrete.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/sec_axis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions tests/testthat/test-sec-axis.R
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,23 @@ test_that("sec_axis() works for power transformations (monotonicity test doesn't
breaks <- scale$break_info()
expect_equal(breaks$major, breaks$sec.major, tolerance = .001)
})

test_that("discrete scales can have secondary axes", {

data <- data.frame(x = c("A", "B", "C"), y = c("D", "E", "F"))
p <- ggplot(data, aes(x, y)) +
geom_point() +
scale_x_discrete(sec.axis = dup_axis(labels = c("foo", "bar", "baz"))) +
scale_y_discrete(sec.axis = dup_axis(
breaks = c(1.5, 2.5), labels = c("grault", "garply")
))
b <- ggplot_build(p)

x <- get_guide_data(b, "x.sec")
expect_equal(x$.value, 1:3, ignore_attr = TRUE)
expect_equal(x$.label, c("foo", "bar", "baz"))

y <- get_guide_data(b, "y.sec")
expect_equal(y$.value, c(1.5, 2.5), ignore_attr = TRUE)
expect_equal(y$.label, c("grault", "garply"))
})
Loading