sci_notation | R Documentation |
This function is used to express numbers in scientific notation for plotting purposes. Specific elements in a vector x
, or all elements in that vector, are converted into scientific notation if their absolute order of magnitude is greater than a user-specified value. Scientific notation is expressed using the 'x10' format, suitable for publication-quality plots, rather than R's default 'e' notation. The number of decimal places can be user-defined or defined automatically such that only the minimum number of decimal places required to distinguish numbers in a sequence is used (i.e., as suitable for pretty axes on a plot).
sci_notation(
x,
magnitude = 5L,
digits = NULL,
specific = TRUE,
make_exp = TRUE,
make_sci = TRUE
)
x |
A numeric vector. |
magnitude |
An integer that defines the order of magnitude (below or above 0) after which numbers in all or specific elements in x are converted to scientific notation (see |
digits |
An integer that defines the number of decimal places. If |
specific |
A logical input that defines whether or not to convert only the specific numbers in |
make_exp |
A logical input that defines whether or not to create an expression object or a character object. |
make_sci |
A logical input that defines whether or not to create scientific notation. This acts as an overall control: if |
A vector of expression objects (or a vector of character objects) that can be added to a plot.
Edward Lavender
The function is implemented internally in pretty_axis
for numeric observations.
#### Example (1): sci_notation() returns an expression object by default
sci_notation(seq(1e-10, 1e10, by = 1e9))
# Except for vectors in which all elements are below the specified magnitude,
# ... which are left unchanged:
sci_notation(1:10)
#### Example (2): sci_notation() can be used to create pretty axis labels
x <- seq(1e-10, 1e10, by = 1e9)
y <- runif(length(x), 0, 100)
xtidy <- sci_notation(x)
plot(x, y, axes = FALSE)
axis(side = 1, at = x, labels = xtidy, pos = min(y), las = 2)
axis(side = 2, at = seq(0, 100, by = 10), pos = min(x))
# This is implemented automatically by pretty_axis() (e.g., via pretty_plot()):
pretty_plot(x, y)
#### Example (3): The digits argument controls the number of decimal places:
# The default is to select the minimum number of decimal places to distinguish
# ... numbers:
sci_notation(c(1.29876e11, 1.29e11, 1.29e12))
sci_notation(c(1.29876e11, 1.298769e11, 1.29e12))
sci_notation(c(1.29876e11, 1.298769e12, 1.29e13))
# Otherwise, this can be directly specified:
sci_notation(c(1.29876e11, 1.29e11), digits = 8)
#### Example (4) Magnitude and specific control implementation
sci_notation(c(0, 1, 2, 1e9), magnitude = 0)
sci_notation(c(0, 1, 2, 1e9), magnitude = 5, specific = FALSE)
sci_notation(c(0, 1, 2, 1e9), magnitude = 5, specific = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.