VISUALISE FROM ggsql:penguins
DRAW histogram
MAPPING body_mass AS xHistogram
Layers are declared with the
DRAWclause. Read the documentation for this clause for a thorough description of how to use it.
Visualise the distribution of a single continuous variable by dividing the primary axis into bins and counting the number of observations in each bin. If providing a weight then a weighted histogram is calculated instead.
Aesthetics
The following aesthetics are recognised by the bar layer.
Required
- Primary axis (e.g.
x): The continuous variable to bin
Optional
colour: The default colour of each barstroke: The colour of the stroke around each bar. Overridescolourfill: The fill colour of each bar. Overridescolouropacity: The opacity of the bar filllinewidth: The width of the strokelinetype: The type of stroke, i.e. the dashing pattern
Settings
position: Position adjustment. One of'identity','stack'(default),'dodge', or'jitter'bins: The number of bins to calculate. Whole number >= 1. Defaults to30binwidth: The width of each bin (must be > 0). If provided it will override the binwidth calculated frombinsclosed: Either'left'or'right'(default). Determines whether the bin intervals are closed to the left or right side
Data transformation
The histogram layer will bin the records in each group and count them. By default it will map the count to the secondary axis.
Properties
weight: If mapped, the sum of the weights within each bin is calculated instead of the count in each bin
Calculated statistics
count: The count or, ifweighthave been mapped, sum of weights in each bin.density: The groupwise density, i.e. thecountdivided by the sum ofcountamong all bins within each group
Default remappings
count AS <secondary axis>: By default the histogram will show count as the height of the bars
Orientation
The histogram has its primary axis along the binned variable. The orientation is deduced directly from the mapping. To create a horizontal histogram, you map the variable to y instead of x (assuming a default Cartesian coordinate system).
Examples
Show the number of each species in the data
Split the histogram in two by mapping sex to fill
VISUALISE FROM ggsql:penguins
DRAW histogram
MAPPING body_mass AS x, sex AS fillThe default is to stack multiple histograms. To compare them from a baseline of 0 set position to identity
VISUALISE FROM ggsql:penguins
DRAW histogram
MAPPING body_mass AS x, sex AS fill
SETTING position => 'identity'Make the two histograms the same scale by remapping to density
VISUALISE FROM ggsql:penguins
DRAW histogram
MAPPING body_mass AS x, sex AS fill
REMAPPING density AS ySpecify an exact binwidth if needed
VISUALISE FROM ggsql:penguins
DRAW histogram
MAPPING body_mass AS x
SETTING binwidth => 100Create a histogram along the y axis by changing the mapping
VISUALISE FROM ggsql:penguins
DRAW histogram
MAPPING body_mass AS y