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, na.ignore = TRUE, na.rm = TRUE)

Arguments

x a numeric or character vector
labeller

how the labels should be relabelled or reformatted. One of:

  • a function to be applied to x, e.g. format, toupper, abs, or any other custom function (including from the popular scales package).

  • one of the following convenience strings (or their symbol equivalents), for which common formatting transformations are provided: “percent” (“%”), “comma” (“,”), “log” (“l”), “dollar” (“$”), “euro” (“€”), or “sterling” (“£”).

  • a dictionary, i.e. a named character vector or list of form c(old_lab = “new_lab”, …). Entries of x that match a name are replaced by the corresponding value and the rest are left alone, so a partial mapping is fine. The lookup is by value rather than by position, which means it is unaffected by any reordering of the underlying categories.

Note that an unnamed character vector of length greater than one is an error, rather than a positional replacement of the labels. Such a vector cannot be distinguished from a formatting keyword when x is of length one, and would not follow the categories if they were reordered. Pass a function if you want to compute the labels positionally, e.g. function(x) LETTERS[seq_along(x)].
na.ignore logical indicating whether the labelling function should ignore NA values in x. In other words, should the NA values be left as-is? Default is TRUE.
na.rm logical indicating whether NA values should be removed from x and thus the return object too. Default is TRUE, but only evaluated if na.ignore is FALSE.

Value

a character vector of the same length as x with the transformed labels.

See Also

tinyplot

Examples

library("tinyplot")

x = 1e4
tinylabel(x, "comma")
[1] "10,000"
tinylabel(x, ",") # same
[1] "10,000"
tinylabel(x, "$") # or "dollar"
[1] "$10,000"
# a named vector acts as a dictionary: matched entries are replaced and the
# rest are left alone
tinylabel(c("setosa", "versicolor"), c(setosa = "SET"))
[1] "SET"        "versicolor"
# invoke tinylabel from a parent tinyplot call...
#   => x/yaxl for adjusting axes tick labels
#   => legend = list(labeller = ...) for adjusting the legend labels
s77 = transform(data.frame(state.x77), Illiteracy = Illiteracy / 100)
tinyplot(Life.Exp ~ Income | Illiteracy, data = s77,
         xaxl = '$',
         legend = list(labeller = '%'))

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

# combine with `x/yaxb` to adjust the actual tick marks ("break points")
# at the same time
tinyplot(x = 10^c(10:0), y = 0:10, type = "b",
         log = "x", xaxl = "log", xaxb = 10^c(1,3,5,7,9))

#
## 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."
  )
)

tinyplot(y ~ x, data = dat2, type = "j",
         yaxl = strwrap18,
         theme = "bw") # use theme for horizontal labels + dynamic margin