Skip to content

Commit 44af941

Browse files
authored
Merge pull request #5103 from teunbrand/date_bins
Binned scales and date(time) transformations
2 parents 853266c + 96333c1 commit 44af941

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
@@ -1,5 +1,7 @@
11
# ggplot2 (development version)
22

3+
* Binned scales are now compatible with `trans = "date"` and `trans = "time"`
4+
(@teunbrand, #4217).
35
* `ggsave()` warns when multiple `filename`s are given, and only writes to the
46
first file (@teunbrand, #5114).
57
* 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
@@ -1075,10 +1075,13 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
10751075
# Ensure terminal bins are same width if limits not set
10761076
if (is.null(self$limits)) {
10771077
# Remove calculated breaks if they coincide with limits
1078-
breaks <- setdiff(breaks, limits)
1078+
breaks <- breaks[!breaks %in% limits]
10791079
nbreaks <- length(breaks)
10801080
if (nbreaks >= 2) {
1081-
new_limits <- c(2 * breaks[1] - breaks[2], 2 * breaks[nbreaks] - breaks[nbreaks - 1])
1081+
new_limits <- c(
1082+
breaks[1] + (breaks[1] - breaks[2]),
1083+
breaks[nbreaks] + (breaks[nbreaks] - breaks[nbreaks - 1])
1084+
)
10821085
if (breaks[nbreaks] > limits[2]) {
10831086
new_limits[2] <- breaks[nbreaks]
10841087
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)