coord-flip

Cartesian coordinate system with the x and y axes swapped at render time.

Use this to turn a vertical bar chart into a horizontal one without rewriting the data or the mapping. The axis labels, ticks, and the directional geoms (geom-col, geom-hline, geom-vline, geom-abline) follow the swap automatically. Direction-agnostic geoms (geom-point, geom-line, geom-path, geom-step, geom-segment) work via the same swap with no per-geom changes.

Usage

coord-flip()

Returns

Coordinate dictionary consumed by plot.

Examples

Flip a vertical bar chart into a horizontal one without rewriting the mapping.

#let d = (
  (q: "Q1", revenue: 10),
  (q: "Q2", revenue: 18),
  (q: "Q3", revenue: 25),
  (q: "Q4", revenue: 22),
)
#plot(
  data: d,
  mapping: aes(x: "q", y: "revenue"),
  layers: (geom-col(),),
  coord: coord-flip(),
  width: 10cm,
  height: 6cm,
)

Horizontal bar chart of quarterly revenue with Q1 to Q4 on the y-axis and revenue from 10 to 25 on the x-axis; Q3 is longest at 25, Q1 shortest at 10.

Horizontal bar chart of quarterly revenue with Q1 to Q4 on the y-axis and revenue from 10 to 25 on the x-axis; Q3 is longest at 25, Q1 shortest at 10.

Reference lines follow the flip: a yintercept becomes a vertical reference once the axes swap.

#let d = range(0, 10).map(i => (x: i, y: i * 0.5))
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (
    geom-point(size: 2pt),
    geom-hline(yintercept: 2.5, colour: rgb("#cc0000")),
  ),
  coord: coord-flip(),
  width: 10cm,
  height: 6cm,
)

Flipped scatter chart of a linear y = x/2 series with a red reference line drawn vertically because coord-flip swaps the original yintercept = 2.5 into the x direction.

Flipped scatter chart of a linear y = x/2 series with a red reference line drawn vertically because coord-flip swaps the original yintercept = 2.5 into the x direction.

See also

plot, coord-cartesian, geom-col.

Back to top