@@ -4,3 +4,42 @@ test_that("binned scales only support continuous data", {
4
4
p <- ggplot(mtcars ) + geom_point(aes(disp , mpg , colour = as.character(gear ))) + scale_color_binned()
5
5
expect_snapshot_error(ggplot_build(p ))
6
6
})
7
+
8
+ test_that(" binned scales limits can expand to fit breaks" , {
9
+ # See also #5095
10
+
11
+ scale <- scale_x_binned(right = FALSE , show.limits = TRUE )
12
+ scale $ train(c(14 , 29 ))
13
+
14
+ limits <- scale $ get_limits()
15
+ breaks <- scale $ get_breaks()
16
+ new_limits <- scale $ get_limits()
17
+
18
+ # Positive control
19
+ expect_equal(limits , c(14 , 29 ))
20
+ # Test case, should have been updated in break calculation
21
+ expect_equal(new_limits , c(14 , 30 ))
22
+
23
+ # Negative control
24
+ # Now, new limits should not be updated because limits were given instead
25
+ # of computed
26
+ scale <- scale_x_binned(right = FALSE , show.limits = TRUE ,
27
+ limits = c(14 , 29 ))
28
+ limits <- scale $ get_limits()
29
+ breaks <- scale $ get_breaks()
30
+ new_limits <- scale $ get_limits()
31
+
32
+ expect_equal(limits , new_limits )
33
+ })
34
+
35
+ test_that(" binned limits should not compute out-of-bounds breaks" , {
36
+ scale <- scale_x_binned(n.breaks = 10 )
37
+ scale $ train(c(1 , 9 ))
38
+
39
+ limits <- scale $ get_limits()
40
+ breaks <- scale $ get_breaks()
41
+ expect_length(breaks , 7 ) # Not the requested 10 due to oob discarding
42
+ expect_true(all(
43
+ breaks > limits [1 ] & breaks < limits [2 ]
44
+ ))
45
+ })
0 commit comments