labeller

Combine labellers into a single rule keyed by facet variable.

rules is a dict of var-name -> labeller. Variables not listed fall back to the labeller passed via default.

Usage

labeller(
  rules: (:),
  default: none,
)

Parameters

Parameter Default Description
rules (:) Dict mapping facet variable names to labellers.
default none Labeller used for variables absent from rules.

Returns

Labeller dictionary consumed by facet-wrap and facet-grid.

Examples

Mix labellers per facet variable: rows show var: level, columns show level only.

#let d = ()
#for sp in ("a", "b") {
  for sex in ("F", "M") {
    d.push((sp: sp, sex: sex, x: 1, y: 1))
    d.push((sp: sp, sex: sex, x: 2, y: 2))
  }
}
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (geom-point(size: 2pt),),
  facet: facet-grid(
    rows: "sex",
    columns: "sp",
    labeller: labeller(rules: (sex: label-both(), sp: label-value())),
  ),
  width: 10cm,
  height: 6cm,
)

2-by-2 grid of small scatter panels with sex on rows formatted as 'sex: F' / 'sex: M' and sp on columns as plain 'a' / 'b'.

2-by-2 grid of small scatter panels with sex on rows formatted as 'sex: F' / 'sex: M' and sp on columns as plain 'a' / 'b'.

Set default to a fallback labeller for variables not listed in rules.

#let d = ()
#for sp in ("a", "b") {
  for sex in ("F", "M") {
    d.push((sp: sp, sex: sex, x: 1, y: 1))
    d.push((sp: sp, sex: sex, x: 2, y: 2))
  }
}
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (geom-point(size: 2pt),),
  facet: facet-grid(
    rows: "sex",
    columns: "sp",
    labeller: labeller(
      rules: (sex: label-both()),
      default: label-context(),
    ),
  ),
  width: 10cm,
  height: 6cm,
)

2-by-2 grid of scatter panels with sex rows labelled 'sex: F' / 'sex: M' (rule) and sp columns labelled with row counts via the label-context default.

2-by-2 grid of scatter panels with sex rows labelled 'sex: F' / 'sex: M' (rule) and sp columns labelled with row counts via the label-context default.

See also

label-value, label-both, label-context.

Back to top