Skip to content

as_labeller

as_labeller(x: Mapping[str | int | float | bool, str] | Callable[[str], str] | Labeller | Literal, default: Labeller | Literal = <ggrust.plot.labeller.Labeller object at 0x7ef57944d400>, *, multi_line: bool = True) -> Labeller

Build a labeller from a lookup table or a function (ggplot2 as_labeller).

Parameters:

  • x ((Mapping, callable, Labeller or str)) –

    A table from level to label (levels not in it keep their text; a numeric key matches a numeric level by value), a function called with each level's text (a number as its plain value, "1000") that returns its label, or a labeller (or its name), returned with multi_line set.

  • default (Labeller or str, default: label_value ) –

    The labeller applied after the table or function, for example label_both for variable: label.

  • multi_line (bool, default: True ) –

    Whether each variable gets its own strip.

Returns:

  • Labeller –

    The labeller, for facet_wrap(labeller=...) or facet_grid(labeller=...).

Raises:

  • SpecError –

    When x or default is an unknown labeller name.

  • TypeError –

    When x is not a table, a callable or a labeller, or a table value is not a string.

Examples:

>>> from ggrust import *
>>> data = {"age": [1, 3, 5, 7, 2, 4], "weight": [4, 6, 6, 8, 12, 14],
...         "pet": ["cat"] * 3 + ["dog"] * 3}
>>> names = as_labeller({"cat": "Cats", "dog": "Dogs"})
>>> plot = ggplot(data, aes("age", "weight")) + geom_point()
>>> plot = plot + facet_wrap("pet", labeller=names)

A function of the level text, then label_both to put the variable in front:

>>> shout = as_labeller(str.upper, default=label_both)
>>> plot = ggplot(data, aes("age", "weight")) + geom_point()
>>> plot = plot + facet_wrap("pet", labeller=shout)

A function of the level text, then label_both to put the variable in front