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)
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 |
ignore | A logical input which specifies whether or not, under the condition that no number has any decimal places, the |
A vector, as inputted, but in which any elements with fewer than n
decimal places have had ".0"(s) added.
This function is not designed to work with scientific notation. Digits for scientific notation can be controlled via sci_notation
.
Edward Lavender
#### 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#> [1] "0.0100" "0.0020"#> [1] "0.0" "100.0" "200.0" "300.0" "400.0" "500.0" "600.0" "700.0" #> [9] "800.0" "900.0" "1000.0"#> [1] "50.123" "1000.000" "150.000" "2000.000"#> [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"#> [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#> [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"#> [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)#### 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"#> expression("1.00" ~ "x" ~ 10^9)