2
2
# '
3
3
# ' Breaks a set of pretty breaks for changes.
4
4
# ' @param n the number of breaks on either side of the reference
5
+ # ' @param extra An optional vector of additional breaks.
6
+ # ' The function always appends these breaks.
7
+ # ' Use this option when you want to force this values to be a part of the
8
+ # ' breaks.
5
9
# ' @export
6
10
# ' @importFrom assertthat assert_that is.count
7
11
# ' @importFrom utils head tail
8
12
# ' @family utils
9
- change_breaks <- function (n = 2 ) {
13
+ change_breaks <- function (n = 2 , extra = NULL ) {
10
14
assert_that(is.count(n ))
11
15
n_default <- n
12
- function (x , n = n_default ) {
16
+ extra_default <- extra
17
+ function (x , n = n_default , extra = extra_default ) {
13
18
if (length(x ) == 0 ) {
14
19
return (numeric (0 ))
15
20
}
21
+ stopifnot(is.numeric(x ))
16
22
abs(x ) | >
17
23
max() | >
18
24
exp() - > extreme
@@ -32,11 +38,22 @@ change_breaks <- function(n = 2) {
32
38
rel_position <- log(candidate ) / max(log(candidate ))
33
39
seq(0 , 1 , length = n + 1 ) | >
34
40
outer(rel_position , " -" ) - > delta
35
- selected <- candidate [apply(delta ^ 2 , 1 , which.min )]
41
+ selected <- candidate [unique( apply(delta ^ 2 , 1 , which.min ) )]
36
42
rev(1 / selected ) | >
37
43
head(- 1 ) | >
38
44
c(selected ) | >
39
- log()
45
+ log() - > breaks
46
+ if (is.null(extra )) {
47
+ return (breaks )
48
+ }
49
+ stopifnot(is.numeric(extra ))
50
+ outer(breaks , extra , " -" ) | >
51
+ abs() - > delta
52
+ to_replace <- which(delta < min(diff(breaks )) / 10 , arr.ind = TRUE )
53
+ breaks [to_replace [, " row" ]] <- extra [to_replace [, " col" ]]
54
+ c(breaks , extra ) | >
55
+ sort() | >
56
+ unique()
40
57
}
41
58
}
42
59
0 commit comments