Skip to content

Commit 0de8338

Browse files
authored
Merge branch 'main' into binned_zerorange
2 parents c8e1534 + 44af941 commit 0de8338

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* `scale_*_binned()` handles zero-range limits more gracefully (@teunbrand,
44
#5066)
5+
* Binned scales are now compatible with `trans = "date"` and `trans = "time"`
6+
(@teunbrand, #4217).
57
* `ggsave()` warns when multiple `filename`s are given, and only writes to the
68
first file (@teunbrand, #5114).
79
* Fixed a regression in `geom_hex()` where aesthetics were replicated across

R/scale-.r

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,10 +1081,13 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
10811081
# Ensure terminal bins are same width if limits not set
10821082
if (is.null(self$limits)) {
10831083
# Remove calculated breaks if they coincide with limits
1084-
breaks <- setdiff(breaks, limits)
1084+
breaks <- breaks[!breaks %in% limits]
10851085
nbreaks <- length(breaks)
10861086
if (nbreaks >= 2) {
1087-
new_limits <- c(2 * breaks[1] - breaks[2], 2 * breaks[nbreaks] - breaks[nbreaks - 1])
1087+
new_limits <- c(
1088+
breaks[1] + (breaks[1] - breaks[2]),
1089+
breaks[nbreaks] + (breaks[nbreaks] - breaks[nbreaks - 1])
1090+
)
10881091
if (breaks[nbreaks] > limits[2]) {
10891092
new_limits[2] <- breaks[nbreaks]
10901093
breaks <- breaks[-nbreaks]

tests/testthat/test-scale-binned.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,39 @@ test_that("binned scales only support continuous data", {
44
p <- ggplot(mtcars) + geom_point(aes(disp, mpg, colour = as.character(gear))) + scale_color_binned()
55
expect_snapshot_error(ggplot_build(p))
66
})
7+
8+
test_that('binned scales can calculate breaks on dates', {
9+
10+
data <- seq(as.Date("2000-01-01"), as.Date("2020-01-01"), length.out = 100)
11+
12+
scale <- scale_x_binned(trans = "date")
13+
scale$train(scale$transform(data))
14+
breaks <- scale$trans$inverse(scale$get_breaks())
15+
16+
expect_s3_class(breaks, "Date")
17+
expect_equal(
18+
unname(breaks),
19+
as.Date(paste0(seq(2002, 2018, by = 2), "-01-01"))
20+
)
21+
})
22+
23+
test_that('binned scales can calculate breaks on date-times', {
24+
data <- seq(
25+
strptime("2000-01-01", "%Y-%m-%d"),
26+
strptime("2020-01-01", "%Y-%m-%d"),
27+
length.out = 100
28+
)
29+
30+
scale <- scale_x_binned(trans = "time")
31+
scale$train(scale$transform(data))
32+
breaks <- scale$trans$inverse(scale$get_breaks())
33+
34+
expect_s3_class(breaks, "POSIXct")
35+
expect_equal(
36+
unname(unclass(breaks)),
37+
unclass(as.POSIXct(strptime(
38+
paste0(seq(2002, 2018, by = 2), "-01-01"),
39+
"%Y-%m-%d"
40+
)))
41+
)
42+
})

0 commit comments

Comments
 (0)