geom-bar

Bar layer that counts rows per x level (stat-count).

Maps the x aesthetic to a discrete variable; the layer then counts rows per x level and draws one bar per count. Use geom-col instead when y is already computed.

Usage

geom-bar(
  mapping: none,
  data: none,
  width: 0.9,
  colour: auto,
  fill: auto,
  stroke: none,
  alpha: auto,
  position: "stack",
  inherit-aes: true,
)

Parameters

Parameter Default Description
mapping none Layer-specific aesthetic mapping built with aes. Falls back to the plot mapping when none.
data none Layer-specific dataset. Falls back to the plot data when none.
width 0.9 Bar width as a fraction of the category width (0 to 1).
colour auto Bar outline colour. auto resolves via the colour scale, falling back to the theme ink only when neither colour nor fill is set.
fill auto Bar fill colour. auto resolves via the fill scale or a neutral default.
stroke none Bar outline thickness (a Typst length) or stroke dictionary; none disables the outline.
alpha auto Bar opacity in [0, 1].
position "stack" Position adjustment: "stack" (default), "dodge", "fill", or "identity".
inherit-aes true Whether to merge the plot-level mapping into this layer’s mapping.

Returns

Layer dictionary consumed by plot.

Examples

Plain count of rows per category.

#let d = (
  (grp: "a"),
  (grp: "b"),
  (grp: "a"),
  (grp: "c"),
  (grp: "b"),
  (grp: "a"),
)
#plot(
  data: d,
  mapping: aes(x: "grp"),
  layers: (geom-bar(),),
  width: 10cm,
  height: 6cm,
)

Bar chart of row counts per category (a, b, c) on the x axis with count on the y axis.

Bar chart of row counts per category (a, b, c) on the x axis with count on the y axis.

Map fill to a second column and switch position to "dodge" to compare counts side by side.

#let d = (
  (grp: "a", k: "x"),
  (grp: "b", k: "x"),
  (grp: "a", k: "y"),
  (grp: "c", k: "x"),
  (grp: "b", k: "y"),
  (grp: "a", k: "y"),
)
#plot(
  data: d,
  mapping: aes(x: "grp", fill: "k"),
  layers: (geom-bar(position: "dodge"),),
  width: 10cm,
  height: 6cm,
)

Dodged bar chart of counts per group (a, b, c) split into two fill categories (x, y) shown side by side.

Dodged bar chart of counts per group (a, b, c) split into two fill categories (x, y) shown side by side.

Penguin counts per island, broken down by species with dodged bars.

#plot(
  data: penguins,
  mapping: aes(x: "island", fill: "species"),
  layers: (geom-bar(position: "dodge"),),
  labs: labs(x: "Island", y: "Count", fill: "Species"),
  width: 11cm,
  height: 6cm,
)

Dodged bar chart of penguin counts per island on the x axis with species coloured by fill aesthetic.

Dodged bar chart of penguin counts per island on the x axis with species coloured by fill aesthetic.

See also

geom-col, stat-count.

Back to top