Format labels

Description

Function for formatting label appearance, e.g. axis ticks labels. This is what the top-level xaxl and yaxl arguments from tinyplot ultimately get passed to.

Usage

tinylabel(x, labeller = NULL)

Arguments

x a numeric or character vector
labeller a formatting function to be applied to x, e.g. format, toupper, abs, or other custom function (including from the popular scales package). Can also be one of the following convenience strings (symbols), for which common formatting transformations are provided: “percent” (“%”), “comma” (“,”), “log” (“l”), “dollar” (“$”), “euro” (“€”), or “sterling” (“£”).

Examples

library("tinyplot")

x = 1e4
tinylabel(x, "comma")
[1] "10,000"
tinylabel(x, ",") # same
[1] "10,000"
tinylabel(x, "$") # or "dollar"
[1] "$10,000"
# pass to xaxl/yaxl for adjusting axes tick labels in a tinyplot call
tinyplot(I(mpg/hp) ~ hp, data = mtcars, yaxl = "%")

# log example (combined with axis scaling)
tinyplot(x = 10^c(10:0), y = 0:10, type = "b", log = "x", xaxl = "log")

#
## custom function examples

## example I: date formatting

dat = data.frame(
  date = seq(as.Date("2000/1/1"), by = "month", length.out = 12),
  trend = 1:12 + rnorm(12, sd = 1)
)

tinyplot(trend ~ date, data = dat, xaxl = function(x) format(x, "%b, %Y"))

## example II: string wrapping 

# create a "vectorised" version of `base::strwrap` that breaks long
# strings into new lines every 18 characters
strwrap18 = function(x) sapply(
  strwrap(x, width = 18, simplify = FALSE),
  paste,
  collapse = "\n"
)

# now demonstrate on a dataset with long y-tick labels
dat2 = data.frame(
  x = rep(rnorm(100), 3),
  y = c(
    "tinyplot is a lightweight extension of the base R graphics system.",
    "R is a language for statistical computing.",
    "Data visualization is an essential skill."
  )
)

tinytheme("bw")
tinyplot(y ~ x, data = dat2, type = "j", yaxl = strwrap18)

tinytheme()