Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
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).
* Fixed bug where the `ggplot2::`-prefix did not work with `stage()`
(@teunbrand, #6104).
* New `get_labs()` function for retrieving completed plot labels
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.waive(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 @@ -1029,7 +1029,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 @@ -1083,6 +1084,9 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
}

if (is.waive(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 @@ -273,6 +273,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
Loading