label_sci: Add formatted scientific notation labels to ggplot

View source: R/label_sci.R

label_sciR Documentation

Add formatted scientific notation labels to ggplot

Description

This function takes the scale breaks as input and uses the to_scientific() function to convert the scale breaks to formatted scientific notation.

Usage

label_sci(
  digits = 2,
  max_cut = 10^5,
  min_cut = 10^-3,
  common = FALSE,
  factor = NULL,
  trailing = TRUE,
  units = NULL
)

Arguments

digits

Number of significant figures to display in the converted output

max_cut

Above this number, values will be converted into scientific format (inclusive)

min_cut

Below this number, values will be converted into scientific format (exclusive)

common

Should the formatted numbers share a common exponential factor?

factor

Provides a value for the common exponential factor; if NULL, the most occurring exponent will be used

trailing

Should trailing zeros be included in the formatted number?

units

If provided, adds units to the converted output

Details

The labels argument of the ggplot2 scale functions can accept a character vector providing the names or a function that takes the breaks as input and returns the labels as output. The axis breaks cannot be accessed as a named variable, but they are the provided input if a function is used for the labels argument.

This setup is based on the label_scientific() function in the scales package.

Value

Labels to use inside a scale_*() function for a ggplot2 object

Examples

library(ggplot2)

df <- data.frame(x = seq(1000, 5000, by = 1000), y = 1:5)

if (requireNamespace("ggplot2", quietly = TRUE)) {
p <- ggplot(df, aes(x, y)) +
    geom_point() +
    scale_x_continuous(labels = label_sci(max_cut = 10))
}


christyray/sciscales documentation built on April 14, 2022, 2:05 p.m.