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_sci = TRUE
)

Arguments

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

digits

An integer that defines the number of decimal places. If NULL, this is defined automatically to be the minimum number of decimal places required to distinguish numbers.

specific

A logical input that defines whether or not to convert only the specific numbers in x whose order of magnitude exceed magnitude into scientific notation. Otherwise, if the absolute order of magnitude of element in x exceeds magnitude, all elements are reformatted in scientific notation. (However, 0 is always retained as 0.)

make_sci

A logical input that defines whether or not to create scientific notation. This acts as an overall control: if make_sci is FALSE, the function simplify returns x unchanged.

Value

A vector of expression objects that can be added to a plot.

See also

The function is implemented internally in pretty_axis for numeric observations.

Author

Edward Lavender

Examples

#### Example (1): sci_notation() returns an expression object sci_notation(seq(1e-10, 1e10, by = 1e9))
#> expression("1" ~ "x" ~ 10^-10, "1" ~ "x" ~ 10^9, "2" ~ "x" ~ #> 10^9, "3" ~ "x" ~ 10^9, "4" ~ "x" ~ 10^9, "5" ~ "x" ~ 10^9, #> "6" ~ "x" ~ 10^9, "7" ~ "x" ~ 10^9, "8" ~ "x" ~ 10^9, "9" ~ #> "x" ~ 10^9, "1" ~ "x" ~ 10^10)
# Except for vectors in which all elements are below the specified magnitude, # ... which are left unchanged: sci_notation(1:10)
#> [1] 1 2 3 4 5 6 7 8 9 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))
#> expression("1.299" ~ "x" ~ 10^11, "1.290" ~ "x" ~ 10^11, "1.290" ~ #> "x" ~ 10^12)
sci_notation(c(1.29876e11, 1.298769e11, 1.29e12))
#> expression("1.298760" ~ "x" ~ 10^11, "1.298769" ~ "x" ~ 10^11, #> "1.290000" ~ "x" ~ 10^12)
sci_notation(c(1.29876e11, 1.298769e12, 1.29e13))
#> expression("1" ~ "x" ~ 10^11, "1" ~ "x" ~ 10^12, "1" ~ "x" ~ #> 10^13)
# Otherwise, this can be directly specified: sci_notation(c(1.29876e11, 1.29e11), digits = 8)
#> expression("1.29876000" ~ "x" ~ 10^11, "1.29000000" ~ "x" ~ 10^11)
#### Example (4) Magnitude and specific control implementation sci_notation(c(0, 1, 2, 1e9), magnitude = 0)
#> expression("0", "1" ~ "x" ~ 10^0, "2" ~ "x" ~ 10^0, "1" ~ "x" ~ #> 10^9)
sci_notation(c(0, 1, 2, 1e9), magnitude = 5, specific = FALSE)
#> expression("0", "1" ~ "x" ~ 10^0, "2" ~ "x" ~ 10^0, "1" ~ "x" ~ #> 10^9)
sci_notation(c(0, 1, 2, 1e9), magnitude = 5, specific = TRUE)
#> expression("0", "1", "2", "1" ~ "x" ~ 10^9)