Skip to content

Commit

Permalink
Discrete labels from break names (#6148)
Browse files Browse the repository at this point in the history
* return break names

* add test

* fix #6089

* add news bullet

* carry over names

* prevent name-dropping

* swap `mtfrm()` for `as.character()`
  • Loading branch information
teunbrand authored Nov 11, 2024
1 parent 6606390 commit 094b957
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* When discrete breaks have names, they'll be used as labels by default
(@teunbrand, #6147).
* The helper function `is.waiver()` is now exported to help extensions to work
with `waiver()` objects (@arcresu, #6173).
* Date(time) scales now throw appropriate errors when `date_breaks`,
Expand Down
4 changes: 2 additions & 2 deletions R/axis-secondary.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
if (is.derived(self$breaks)) self$breaks <- scale$breaks
if (is.waiver(self$breaks)) {
if (scale$is_discrete()) {
self$breaks <- scale$get_breaks()
self$breaks <- setNames(nm = scale$get_breaks())
} else {
breaks <- scale$get_transformation()$breaks
n_breaks <- scale$n.breaks
Expand Down Expand Up @@ -235,7 +235,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
self$mono_test(scale)
breaks <- self$breaks
} else {
breaks <- scale$map(self$breaks)
breaks <- setNames(scale$map(self$breaks), names(self$breaks))
}

# Get scale's original range before transformation
Expand Down
6 changes: 5 additions & 1 deletion R/scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,8 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
}

# Breaks only occur only on values in domain
in_domain <- intersect(breaks, limits)
breaks <- setNames(as.character(breaks), names(breaks))
in_domain <- vec_set_intersect(breaks, as.character(limits))
structure(in_domain, pos = match(in_domain, breaks))
},

Expand Down Expand Up @@ -1085,6 +1086,9 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
}

if (is.waiver(self$labels)) {
if (!is.null(names(breaks))) {
return(names(breaks))
}
if (is.numeric(breaks)) {
# Only format numbers, because on Windows, format messes up encoding
format(breaks, justify = "none")
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-scales-breaks-labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,17 @@ test_that("equal length breaks and labels can be passed to ViewScales with limit
expect_identical(test_view_scale_rev$get_labels(), c(c("0", "20", "40")))
})

test_that("break names are returned as labels", {

sc <- scale_x_continuous(breaks = c(A = 10, B = 20, C = 30))
sc$train(c(10, 30))
expect_equal(sc$get_labels(), c("A", "B", "C"))

sc <- scale_x_discrete(breaks = c(foo = "A", bar = "B", qux = "C"))
sc$train(c(LETTERS[1:3]))
expect_equal(sc$get_labels(), c("foo", "bar", "qux"))
})

# Visual tests ------------------------------------------------------------

test_that("minor breaks draw correctly", {
Expand Down

0 comments on commit 094b957

Please sign in to comment.