Skip to content

position_dodge2

position_dodge2(
    width: Number | None = None,
    *,
    preserve: Literal["total", "single"] = "total",
    padding: Number = 0.1,
    reverse: bool = False,
) -> FeatureSpec

Place overlapping boxes or bars side by side, centred on their span (ggplot2).

Elements whose x spans overlap form one dodge group and are laid side by side, each as wide as its span divided by the elements of the group. Unlike position_dodge(), it needs no grouping across the whole layer, so boxes of different widths (varwidth=True) and missing groups dodge cleanly.

Parameters:

  • width (float, default: None ) –

    Width of elements without xmin/xmax (points, text), as a share of the x resolution. Boxes and bars use their own span.

  • preserve ((total, single), default: "total" ) –

    "total" splits each span among its overlapping elements; "single" gives every element the share of the most elements at one x.

  • padding (float, default: 0.1 ) –

    Share of each element's width left empty, between 0 and 1.

  • reverse (bool, default: False ) –

    True places the groups in reverse order.

Returns:

  • FeatureSpec –

    The position adjustment.

Raises:

  • ValueError –

    If preserve is unknown or padding is not between 0 and 1.

  • TypeError –

    If reverse is not a bool.

Examples:

>>> from ggrust import *
>>> data = {"day": ["Mon"] * 6 + ["Tue"] * 3,
...         "shift": ["early", "late"] * 3 + ["early"] * 3,
...         "minutes": [31, 40, 28, 45, 35, 38, 22, 25, 30]}
>>> plot = ggplot(data, aes("day", "minutes", fill="shift")) + geom_boxplot(
...     position=position_dodge2(preserve="single")
... )

Bars with more padding between them:

>>> sales = {"month": ["Jan", "Jan", "Feb", "Feb", "Mar", "Mar"],
...          "shop": ["north", "south"] * 3,
...          "sales": [12, 9, 15, 11, 14, 16]}
>>> plot = ggplot(sales, aes("month", "sales", fill="shop")) + geom_bar(
...     stat="identity", position=position_dodge2(padding=0.4)
... )

Bars with more padding between them