add_lagging_point_zero: Add lagging .0(s) to a number

View source: R/add_lagging_point_zero.R

add_lagging_point_zeroR Documentation

Add lagging .0(s) to a number

Description

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

Usage

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).

Details

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

Value

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

Author(s)

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))
# 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)


edwardlavender/prettyGraphics documentation built on Jan. 19, 2025, 2:47 p.m.