@@ -82,6 +82,89 @@ breaks_log <- function(n = 5, base = 10) {
82
82
# ' @rdname breaks_log
83
83
log_breaks <- breaks_log
84
84
85
+ # ' Minor breaks for log-10 axes
86
+ # '
87
+ # ' This break function is designed to mark every power, multiples of 5 and/or 1
88
+ # ' of that power for base 10.
89
+ # '
90
+ # ' @param detail Any of `1`, `5` and `10` to mark multiples of
91
+ # ' powers, multiples of 5 of powers or just powers respectively.
92
+ # ' @param smallest Smallest absolute value to mark when the range includes
93
+ # ' negative numbers.
94
+ # '
95
+ # ' @return A function to generate minor ticks.
96
+ # ' @export
97
+ # '
98
+ # ' @examples
99
+ # ' # Standard usage with log10 scale
100
+ # ' demo_log10(c(1, 1e10), minor_breaks = minor_breaks_log())
101
+ # ' # Increasing detail over many powers
102
+ # ' demo_log10(c(1, 1e10), minor_breaks = minor_breaks_log(detail = 1))
103
+ # ' # Adjusting until where to draw minor breaks
104
+ # ' demo_continuous(
105
+ # ' c(-1000, 1000),
106
+ # ' transform = asinh_trans(),
107
+ # ' minor_breaks = minor_breaks_log(smallest = 1)
108
+ # ' )
109
+ minor_breaks_log <- function (detail = NULL , smallest = NULL ) {
110
+ if (! is.null(detail ) && (! length(detail ) == 1 || ! detail %in% c(1 , 5 , 10 ))) {
111
+ cli :: cli_abort(" The {.arg detail} argument must be one of 1, 5 or 10." )
112
+ }
113
+ if (! is.null(smallest ) &&
114
+ (! length(smallest ) == 1 || smallest < 1e-100 || ! is.finite(smallest ))) {
115
+ cli :: cli_abort(
116
+ " The {.arg smallest} argument must be a finite, positive, non-zero number."
117
+ )
118
+ }
119
+ force(smallest )
120
+ function (x , ... ) {
121
+
122
+ has_negatives <- any(x < = 0 )
123
+
124
+ if (has_negatives ) {
125
+ large <- max(abs(x ))
126
+ small <- smallest %|| % min(c(1 , large ) * 0.1 )
127
+ x <- sort(c(small * 10 , large ))
128
+ }
129
+
130
+ start <- floor(log10(min(x ))) - 1L
131
+ end <- ceiling(log10(max(x ))) + 1L
132
+
133
+ if (is.null(detail )) {
134
+ i <- findInterval(abs(end - start ), c(8 , 15 ), left.open = TRUE ) + 1L
135
+ detail <- c(1 , 5 , 10 )[i ]
136
+ }
137
+
138
+ ladder <- 10 ^ seq(start , end , by = 1L )
139
+ tens <- fives <- ones <- numeric ()
140
+ if (detail %in% c(10 , 5 , 1 )) {
141
+ tens <- ladder
142
+ }
143
+ if (detail %in% c(5 , 1 )) {
144
+ fives <- 5 * ladder
145
+ }
146
+ if (detail == 1 ) {
147
+ ones <- as.vector(outer(1 : 9 , ladder ))
148
+ ones <- setdiff(ones , c(tens , fives ))
149
+ }
150
+
151
+ if (has_negatives ) {
152
+ tens <- tens [tens > = small ]
153
+ tens <- c(tens , - tens , 0 )
154
+ fives <- fives [fives > = small ]
155
+ fives <- c(fives , - fives )
156
+ ones <- ones [ones > = small ]
157
+ ones <- c(ones , - ones )
158
+ }
159
+
160
+ ticks <- c(tens , fives , ones )
161
+ n <- c(length(tens ), length(fives ), length(ones ))
162
+
163
+ attr(ticks , " detail" ) <- rep(c(10 , 5 , 1 ), n )
164
+ ticks
165
+ }
166
+ }
167
+
85
168
# ' @author Thierry Onkelinx, \email{[email protected] }
86
169
# ' @noRd
87
170
log_sub_breaks <- function (rng , n = 5 , base = 10 ) {
0 commit comments