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 )
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_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 that can be added to a plot.
The function is implemented internally in pretty_axis
for numeric observations.
Edward Lavender
#### 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)#### 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)#> expression("1.298760" ~ "x" ~ 10^11, "1.298769" ~ "x" ~ 10^11, #> "1.290000" ~ "x" ~ 10^12)#> expression("1" ~ "x" ~ 10^11, "1" ~ "x" ~ 10^12, "1" ~ "x" ~ #> 10^13)#> 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)#> expression("0", "1" ~ "x" ~ 10^0, "2" ~ "x" ~ 10^0, "1" ~ "x" ~ #> 10^9)#> expression("0", "1", "2", "1" ~ "x" ~ 10^9)