Take either a numeric vector or the maximum absolute step
size
between values and and return a suggested decimal precision for fixed
point formats such as "f"
.
precision_fixed(x) precision_fixed_(step)
The d3-format
function https://github.com/d3/d3-format/blob/master/src/precisionFixed.js.
An integer vector of suggest precisions. For precision_fixed
,
this is length one, for precision_fixed_
it is the same length
as step
.
precision_round
Returns a suggested decimal precision for format types
that round to significant digits, such as "g"
and "r"
.
precision_fixed
Returns a suggested SI prefix.
The suggested precision is
$$
p = \max \left(0, \lfloor \log_{10}|d| \rfloor \right)
$$
where \(d\) is the maximum absolute distance between values.
This assumes that the values to be formatted are multiples of the step
size d
.
precision_round
for the suggested precision to
use with formats that round to significant digits,
and precision_prefix
for the suggested SI prefix to use with
formatting.
# step size is 1 and the suggested precision is 0 x <- c(1, 1.5, 2) p <- precision_fixed(x) p#> [1] 1#> [1] "1.0" "1.5" "2.0"# alternatively calculate the step size step <- max(diff(sort(x))) precision_fixed_(step)#> [1] 1# For 1, 2, 3, the step size is 1 suggested precision is 0 x2 <- 1:3 p2 <- precision_fixed(x2) p2#> [1] 0#> [1] "1.0" "2.0" "3.0"