SELECT species, COUNT(*) as count FROM ggsql:penguins
GROUP BY species
VISUALISE species AS x, count AS y, species AS fill
DRAW bar
LABEL
title => 'Penguin Count by Species',
x => 'Species',
y => 'Count'Bar Chart
basic
bar
Categorical comparisons using bars
Bar charts display categorical data with rectangular bars. The height of each bar represents the value for that category.
Code
Explanation
- The SQL query aggregates penguin counts by species
species AS xplaces species names on the x-axiscount AS ysets the bar height based on the countspecies AS fillcolors each bar by speciesDRAW barcreates vertical bars
Variations
Horizontal Bars
Use PROJECT y, x TO cartesian to flip the axes for horizontal bars:
SELECT species, COUNT(*) as count FROM ggsql:penguins
GROUP BY species
VISUALISE species AS x, count AS y, species AS fill
DRAW bar
PROJECT y, x TO cartesian
LABEL
title => 'Penguin Count by Species',
x => 'Species',
y => 'Count'Auto-Count Bar Chart
When you don’t specify a y aesthetic, ggsql automatically counts occurrences:
SELECT species FROM ggsql:penguins
VISUALISE species AS x
DRAW bar
LABEL
title => 'Penguin Distribution',
x => 'Species',
y => 'Count'