This function adds lagging .0(s) to a number. This is useful for tidy graph labels. This is implemented internally in pretty_axis.

add_lagging_point_zero(x, n = NULL, ignore = FALSE)

Arguments

x

A numeric or character vector which contains some elements to which lagging .0(s) should be added.

n

A number which defines the desired number of decimal places. Any element in x with fewer than n decimal places will have the appropriate number of decimal places added. If n is less than the number of decimal places for any element in x, the function will return an error. In this case, round all numbers to the same precision first, before implementing add_lagging_point_zero. If n = NULL, the function defines n internally to be the maximum number of decimal places in x.

ignore

A logical input which specifies whether or not, under the condition that no number has any decimal places, the n argument should be ignored (i.e. if TRUE, the input vector, x, is simply returned without any adjustments).

Value

A vector, as inputted, but in which any elements with fewer than n decimal places have had ".0"(s) added.

Details

This function is not designed to work with scientific notation. Digits for scientific notation can be controlled via sci_notation.

Author

Edward Lavender

Examples

#### Example (1): Bring all numbers up to the same number of decimal places # Use maximum number of decimal places of any one number (i.e., default n specification): add_lagging_point_zero(c(0.01, 0.002))
#> [1] 0.010 0.002
# Specify desired number of decimal places add_lagging_point_zero(c(0.01, 0.002), n = 4)
#> [1] "0.0100" "0.0020"
#### Example (2): More examples add_lagging_point_zero(seq(0, 1000, by = 100), 1)
#> [1] "0.0" "100.0" "200.0" "300.0" "400.0" "500.0" "600.0" "700.0" #> [9] "800.0" "900.0" "1000.0"
add_lagging_point_zero(c(50.123, 1000, 150, 2000), 3)
#> [1] "50.123" "1000.000" "150.000" "2000.000"
add_lagging_point_zero(c(50.0, 1000, 150, 2000), 1)
#> [1] "50.0" "1000.0" "150.0" "2000.0"
#### Example (3): the ignore argument returns an unchanged character vector if no numbers # ... in the vector have decimal places add_lagging_point_zero(seq(0.1, 1000, by = 100), 2, ignore = TRUE) # decimal places added
#> [1] "0.10" "100.10" "200.10" "300.10" "400.10" "500.10" "600.10" "700.10" #> [9] "800.10" "900.10"
add_lagging_point_zero(seq(0, 1000, by = 100), 2, ignore = TRUE) # decimal places not added
#> [1] 0 100 200 300 400 500 600 700 800 900 1000
#### Example (3): Using add_lagging_point_zero() for prettier axis labels # Numeric vector input # Define some data/axis positions at <- seq(0, 1, by = 0.2) * 10^5 # Plot graph graphics::plot(at, at, axes = FALSE)
# Labels before add_lagging_point_zero(): labels1 <- at/10^5; labels1
#> [1] 0.0 0.2 0.4 0.6 0.8 1.0
as.character(labels1) # the .0 disappears from 0 and 1 when coerced to a character (see graph)
#> [1] "0" "0.2" "0.4" "0.6" "0.8" "1"
# Labels after add_lagging_point_zero(): labels2 <- add_lagging_point_zero(labels1, n = 1); labels2
#> [1] "0.0" "0.2" "0.4" "0.6" "0.8" "1.0"
as.character(labels2)
#> [1] "0.0" "0.2" "0.4" "0.6" "0.8" "1.0"
# Labels before add_lagging_point_zero() are varying numbers of decimal places: axis(side = 1, at, labels = labels1)
# Labels after before add_lagging_point_zero() are tidier: axis(side = 2, at, labels = labels2)
#### Example (4): add_lagging_point_zero() is not designed to work with scientific notation # ... Use sci_notation() instead: add_lagging_point_zero(1e9, n = 2)
#> [1] "1000000000.00"
sci_notation(1e9, digits = 2)
#> expression("1.00" ~ "x" ~ 10^9)