Bar

Edit this page
// Single View Specification
{
  "data": ... ,
  "mark": "bar",
  "encoding": ... ,
  ...
}

Bar marks are useful in many visualizations, including bar charts, stacked bar charts, and timelines.

Documentation Overview

Bar Mark Properties

// Single View Specification
{
  ...
  "mark": {
    "type": "bar",
    ...
  },
  "encoding": ... ,
  ...
}

A bar mark definition can contain any standard mark properties and the following special properties:

Property Type Description
width Number | ExprRef | RelativeBandSize

Width of the marks. One of:

  • A number representing a fixed pixel width.

  • A relative band size definition. For example, {band: 0.5} represents half of the band.

height Number | ExprRef | RelativeBandSize

Height of the marks. One of:

  • A number representing a fixed pixel height.

  • A relative band size definition. For example, {band: 0.5} represents half of the band

orient String

The orientation of a non-stacked bar, tick, area, and line charts. The value is either horizontal (default) or vertical.

  • For bar, rule and tick, this determines whether the size of the bar and tick should be applied to x or y dimension.
  • For area, this property determines the orient property of the Vega output.
  • For line and trail marks, this property determines the sort order of the points in the line if config.sortLineBy is not specified. For stacked charts, this is always determined by the orientation of the stack; therefore explicitly specified value will be ignored.
align String | ExprRef

The horizontal alignment of the text or ranged marks (area, bar, image, rect, rule). One of "left", "right", "center".

Note: Expression reference is not supported for range marks.

baseline String | ExprRef

For text marks, the vertical text baseline. One of "alphabetic" (default), "top", "middle", "bottom", "line-top", "line-bottom", or an expression reference that provides one of the valid values. The "line-top" and "line-bottom" values operate similarly to "top" and "bottom", but are calculated relative to the lineHeight rather than fontSize alone.

For range marks, the vertical alignment of the marks. One of "top", "middle", "bottom".

Note: Expression reference is not supported for range marks.

binSpacing Number

Offset between bars for binned field. The ideal value for this is either 0 (preferred by statisticians) or 1 (Vega-Lite default, D3 example style).

Default value: 1

cornerRadius Number | ExprRef

The radius in pixels of rounded rectangles or arcs’ corners.

Default value: 0

cornerRadiusEnd Number | ExprRef
  • For vertical bars, top-left and top-right corner radius.

  • For horizontal bars, top-right and bottom-right corner radius.

cornerRadiusTopLeft Number | ExprRef

The radius in pixels of rounded rectangles’ top right corner.

Default value: 0

cornerRadiusTopRight Number | ExprRef

The radius in pixels of rounded rectangles’ top left corner.

Default value: 0

cornerRadiusBottomRight Number | ExprRef

The radius in pixels of rounded rectangles’ bottom right corner.

Default value: 0

cornerRadiusBottomLeft Number | ExprRef

The radius in pixels of rounded rectangles’ bottom left corner.

Default value: 0

Examples

Single Bar Chart

Mapping a quantitative field to either x or y of the bar mark produces a single bar chart.

Bar Chart

If we map a different discrete field to the y channel, we can produce a horizontal bar chart. Specifying "height": {"step": 17} will adjust the bar’s height per discrete step.

Bar Chart with a Temporal Axis

While the bar mark typically uses the x and y channels to encode a pair of discrete and continuous fields, it can also be used with continuous fields on both channels. For example, given a bar chart with a temporal field on x, we can see that the x-scale is a continuous scale. By default, the size of bars on continuous scales will be set based on the continuousBandSize config.

{.#bar-width}

Relative Bar Width

To adjust the bar to be smaller than the time unit step, you can adjust the bar’s width to be a proportion of band. For example, the following chart sets the width to be 70% of the x band width.

Bar Chart with a Discrete Temporal Axis

If you want to use a discrete scale instead, you can cast the field to have an "ordinal" type. This casting strategy can be useful for time units with low cardinality such as "month".

Bar Chart with Rounded Corners

We can also adjust corner radius of the bar with various corner radius properties. For example, we can use cornerRadiusEnd to create a bar chart with rounded corners at the end of the bars.

Bar Chart with Negative Values and Zero Baseline

When there are negative values, you may want to hide domain the axis domain line, and instead use a conditional grid color to draw a zero baseline.

Histogram

If the data is not pre-aggregated (i.e. each record in the data field represents one item), mapping a binned quantitative field to x and aggregate count to y produces a histogram.

If you prefer to have histogram without gaps between bars, you can set the binSpacing mark property to 0.

Stacked Bar Chart

Adding color to the bar chart (by using the color attribute) creates a stacked bar chart by default. Here we also customize the color’s scale range to make the color a little nicer. (See stack for more details about customizing stack.)

Layered Bar Chart

To disable stacking, you can alternatively set stack to null. This produces a layered bar chart. To make it clear that bars are layered, we can make marks semi-transparent by setting the opacity to a value between 0 and 1 (e.g., 0.7).

Normalized Stacked Bar Chart

You can also create a normalized stacked bar chart by setting stack to "normalize". Here we can easily see the percentage of male and female population at different ages.

Grouped Bar Chart (with Offset)

Grouped Bar Chart (with Facet)

Alternatively, you can also use faceting to produce a grouped bar chart.

This allows you to use independent x-scale for each facet:

Grouped Bar Chart (Multiple Measure with Repeat)

Ranged Bars

Specifying x2 or y2 for the quantitative axis of bar marks creates ranged bars. For example, we can use ranged bars to create a gantt chart.

Bar Config

// Top-level View Specification
{
  ...
  "config": {
    "bar": ...,
    ...
  }
}

The bar property of the top-level config object sets the default properties for all bar marks. If mark property encoding channels are specified for marks, these config values will be overridden.

Besides standard bar mark properties, bar config can contain the following additional properties:

Property Type Description
continuousBandSize Number

The default size of the bars on continuous scales.

Default value: 5

discreteBandSize Number | RelativeBandSize

The default size of the bars with discrete dimensions. If unspecified, the default size is step-2, which provides 2 pixel offset between bars.

minBandSize Number | ExprRef

The minimum band size for bar and rectangle marks. Default value: 0.25