View source: R/add_lagging_point_zero.R
add_lagging_point_zero | R Documentation |
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 |
This function is not designed to work with scientific notation. Digits for scientific notation can be controlled via sci_notation
.
A vector, as inputted, but in which any elements with fewer than n
decimal places have had ".0"(s) added.
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))
# Specify desired number of decimal places
add_lagging_point_zero(c(0.01, 0.002), n = 4)
#### Example (2): More examples
add_lagging_point_zero(seq(0, 1000, by = 100), 1)
add_lagging_point_zero(c(50.123, 1000, 150, 2000), 3)
add_lagging_point_zero(c(50.0, 1000, 150, 2000), 1)
#### 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
add_lagging_point_zero(seq(0, 1000, by = 100), 2, ignore = TRUE) # decimal places not added
#### 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
as.character(labels1) # the .0 disappears from 0 and 1 when coerced to a character (see graph)
# Labels after add_lagging_point_zero():
labels2 <- add_lagging_point_zero(labels1, n = 1); labels2
as.character(labels2)
# 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)
sci_notation(1e9, digits = 2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.