Skip to main content

Bar Chart with Average Line

A bar chart with an average line overlays a horizontal mean line across a grouped bar chart, so viewers can instantly see which bars sit above or below the overall average.

  • Good for: spotting over- and under-performers against the series average, comparing categories grouped by a second dimension, benchmark-style comparisons.
  • Not great for: time-based moving averages (use Bar Chart with Running Average Line), part-to-whole composition, or a single bar with no comparison group.
Bar chart with a horizontal average line overlaid across the bars

Syntax

Use the following AML definition to add the Bar Chart with Average Line to your custom chart library.

Legacy syntax
CustomChart {
fields {
field x {
type: 'dimension'
}
field legend {
type: 'dimension'
}
field y {
type: 'measure'
}
}
options {
}
template: @vgl {
"data": {"values": @{values}},
"layer": [{
"mark": {
"type": "bar",
"tooltip": true
},
"encoding": {
"x": {
"field": @{fields.x.name},
"axis": {
"format": @{fields.x.format},
"formatType": "holisticsFormat",
"labelAngle": 45
}
},
"y": {
"field": @{fields.y.name},
"type": "quantitative",
"axis": {
"format": @{fields.y.format},
"formatType": "holisticsFormat"
}
},
"xOffset": {
"field": @{fields.legend.name}
},
"color": {
"field": @{fields.legend.name}
},
}
}, {
"mark": "rule",
"encoding": {
"y": {
"aggregate": "mean",
"field": @{fields.y.name},
"type": "quantitative"
},
"color": {"value": "firebrick"},
"size": {"value": 3}
}
}],
};;
}

Required fields

A Bar Chart with Average Line expects exactly three fields. Each row is one bar, grouped along the x-axis by dimension and split into side-by-side bars by series.

FieldLabelTypeRole
dimensionDimensiondimensionX-axis category; one group of bars per value. Sorted ascending (apply_order: 1).
seriesSeriesdimensionSplits each group into side-by-side bars and drives the color and legend. Sorted ascending (apply_order: 2).
valueValuemeasureBar height. The average line is the mean of this field across all rows. Sorted descending (apply_order: 3).

Data requirements: Pre-aggregate to one row per dimension and series pair; the bar layer plots values as-is and does not combine duplicates. The average line is the mean of every plotted value, so it reflects all bars in view.

Sample data:

dimensionseriesvalue
JanOnline4200
JanRetail3100
FebOnline3800
FebRetail3600
MarOnline5100
MarRetail2900

Known limitations

  • One flat average across the whole series. The line is a single mean over all plotted rows, not a per-group or per-series average. For a trend that moves period by period, use Bar Chart with Running Average Line instead.

  • Requires a series field. The definition has three fields, including series for the color split. With only one category per x value, leave series constant or pick a simple bar chart.

  • Average is sensitive to outliers. A few very large or very small bars pull the mean line away from the typical value, which can mislead at a glance.


Open Markdown
Let us know what you think about this document :)