Bar

Layers are declared with the DRAW clause. Read the documentation for this clause for a thorough description of how to use it.

The bar layer is used to create bar plots. You can either specify the height of the bars directly or let the layer calculate it either as the count of records within the same group or as a weighted sum of the records.

Aesthetics

The following aesthetics are recognised by the bar layer.

Required

The bar layer has no required aesthetics

Optional

  • x: Position on the x-axis. If missing all records will be shown in the same bar
  • y: The height of the plot. If missing, it will be calculated by the layer
  • colour: The default colour of each bar
  • stroke: The colour of the stroke around each bar. Overrides colour
  • fill: The fill colour of each bar. Overrides colour
  • opacity: The opacity of the bar fill
  • linewidth: The width of the stroke
  • linetype: The type of stroke, i.e. the dashing pattern

Settings

  • width: The width of the bars as a proportion of the available width

Data transformation

If y has not been mapped the layer will calculate it for you.

Properties

  • weight: If mapped, the sum of the weights within each group is calculated instead of the count in each group

Calculated statistics

  • count: The count or, if weight have been mapped, sum of weights in each group.
  • proportion: The groupwise proportion, i.e. the count divided by the sum of count within each group

Default remappings

  • count AS y: By default the barplot will show count as the height of the bars

Examples

Show the number of each species in the data

VISUALISE FROM ggsql:penguins
DRAW bar
    MAPPING species AS x

Use weight to instead show the collective mass of each species

VISUALISE FROM ggsql:penguins
DRAW bar
    MAPPING species AS x, body_mass AS weight

Map fill to a discrete value to create a stacked bar chart

VISUALISE FROM ggsql:penguins
DRAW bar
    MAPPING species AS x, island AS fill

Map to y if the dataset already contains the value you want to show

SELECT species, MAX(body_mass) AS max_mass FROM ggsql:penguins
GROUP BY species
VISUALISE
DRAW bar
    MAPPING species AS x, max_mass AS y