The function precision_prefix_
provides a suggested precision for
use with SI prefix to use,
given a value
to format, and the difference between the values,
step
, that will be formatted.
precision_prefix
calculates the suggested SI Prefix given a
numeric vector of values to format, x
.
precision_prefix(x, prefix = NULL) precision_prefix_(step, prefix)
si_prefix
. If NULL
in precision_prefix
,
then the prefix is determined from the median value in x
.The d3-format
function https://github.com/d3/d3-format/blob/master/src/precisionPrefix.js.
Named list of two elements:
SI_PREFIXES
.The suggested precision, \(p\), when formatting a vector with an SI Prefix of \(10^k\) is, $$ p = \max \left( 0, k - \lfloor \log_{10}d \rfloor \right) ,$$ where \(d\) is the maximum absolute value between elements in the vector.
precision_fixed
for the suggested precision to
use with formats that use a fixed number of digits after the decimal point,
and precision_round
for the suggested precision to use with
formats that round to significant digits.
x <- c(1.1e6, 1.2e6, 1.3e6) p <- precision_prefix(x, 6L) p$precision#> [1] 1#> [1] "1.1M" "1.2M" "1.3M"