Vega Lite
View
Add and Customize Title

How to Add and Customize Titles in Vega-Lite Charts?

Want to make your Vega-Lite charts more informative and appealing? Adding a title can help! In this guide, we'll walk you through how to add a title to your charts and customize it to fit your needs.

Why Add a Title?

A title gives your chart context and helps viewers quickly understand what the data is about. Imagine flipping through a document or a dashboard with multiple charts—having titles makes navigation and comprehension much easier.

Adding a Basic Title

You can use the title property in your view specification. It can be as simple as a string. Let's start with a basic example:

Example: A Simple Bar Chart with a Title

{
  "title": "A Simple Bar Chart",
  "data": {"url": "data/barley.json"},
  "mark": "bar",
  "encoding": {
    "x": {"field": "yield", "type": "quantitative"},
    "y": {"field": "variety", "type": "nominal"}
  }
}

Here, the title "A Simple Bar Chart" will be displayed at the top of the chart.

Customizing Your Chart Title

Now that you've got the basic title down, let's explore how you can customize it. You can do more than just add text; you can adjust its appearance and position to match your design needs.

Title Properties Object

The title property can also be an object that defines various properties. Here are some of the things you can customize:

PropertyTypeDescription
textText | ExprRefRequired. The title text.
alignStringHorizontal text alignment for title text. One of "left", "center", or "right".
anchorNull | StringThe anchor position for placing the title. One of "start", "middle", or "end". For example, with an orientation of top these anchor positions map to a left-, center-, or right-aligned title. Default value: "middle" for single (opens in a new tab) and layered (opens in a new tab) views. "start" for other composite views. Note: For now (opens in a new tab), anchor is only customizable only for single (opens in a new tab) and layered (opens in a new tab) views. For other composite views, anchor is always "start".
angleNumber | ExprRefAngle in degrees of title and subtitle text.
baselineStringVertical text baseline for title and subtitle text. One of "alphabetic" (default), "top", "middle", "bottom", "line-top", or "line-bottom". The "line-top" and "line-bottom" values operate similarly to "top" and "bottom", but are calculated relative to the lineHeight rather than fontSize alone.
colorNull | Color | ExprRefText color for title text.
dxNumber | ExprRefDelta offset for title and subtitle text x-coordinate.
dyNumber | ExprRefDelta offset for title and subtitle text y-coordinate.
fontString | ExprRefFont name for title text.
fontSizeNumber | ExprRefFont size in pixels for title text.
fontStyleString | ExprRefFont style for title text.
fontWeightString | Number | ExprRefFont weight for title text. This can be either a string (e.g "bold", "normal") or a number (100, 200, 300, …, 900 where "normal" = 400 and "bold" = 700).
frameString | ExprRefThe reference frame for the anchor position, one of "bounds" (to anchor relative to the full bounding box) or "group" (to anchor relative to the group width or height).
limitNumber | ExprRefThe maximum allowed length in pixels of title and subtitle text.
lineHeightNumber | ExprRefLine height in pixels for multi-line title text or title text with "line-top" or "line-bottom" baseline.
offsetNumber | ExprRefThe orthogonal offset in pixels by which to displace the title group from its position along the edge of the chart.
orientString | ExprRefDefault title orientation ("top", "bottom", "left", or "right")
styleString | String[]A mark style property (opens in a new tab) to apply to the title text mark. Default value: "group-title".
subtitleTextThe subtitle Text.
subtitleColorNull | Color | ExprRefText color for subtitle text.
subtitleFontString | ExprRefFont name for subtitle text.
subtitleFontSizeNumber | ExprRefFont size in pixels for subtitle text.
subtitleFontStyleString | ExprRefFont style for subtitle text.
subtitleFontWeightString | Number | ExprRefFont weight for subtitle text. This can be either a string (e.g "bold", "normal") or a number (100, 200, 300, …, 900 where "normal" = 400 and "bold" = 700).
subtitleLineHeightNumber | ExprRefLine height in pixels for multi-line subtitle text.
subtitlePaddingNumber | ExprRefThe padding in pixels between title and subtitle text.
zindexNumberThe integer z-index indicating the layering of the title group relative to other axis, mark and legend groups. Default value: 0.

Example: Customizing the Title Anchor

In this example, the title "A Custom Anchored Title" will be anchored at the start of the chart and will be bold with a font size of 20.

{
  "title": {
    "text": "A Custom Anchored Title",
    "anchor": "start",
    "fontSize": 20,
    "fontWeight": "bold"
  },
  "data": {"url": "data/barley.json"},
  "mark": "bar",
  "encoding": {
    "x": {"field": "yield", "type": "quantitative"},
    "y": {"field": "variety", "type": "nominal"}
  }
}

Example: Customizing the Title Position

In this example, we used "orient": "left" to set the title to the left.

{
  "title": {
    "text": "A Orient Customized Title",
    "orient": "left"
  },
  "data": {"url": "data/barley.json"},
  "mark": "bar",
  "encoding": {
    "x": {"field": "yield", "type": "quantitative"},
    "y": {"field": "variety", "type": "nominal"}
  }
}

Example: Add a Subtitle

In this example, we used subtitle property to set the subtitle and use subtitleFontSize and subtitleFontWeight to make it bold with a size of 20.

{
  "title": {
    "text": "A Title",
    "subtitle": "A Subtitle",
    "subtitleFontSize": 20,
    "subtitleFontWeight": "bold"
  },
  "data": {"url": "data/barley.json"},
  "mark": "bar",
  "encoding": {
    "x": {"field": "yield", "type": "quantitative"},
    "y": {"field": "variety", "type": "nominal"}
  }
}

Global Title Configuration

If you're looking to apply a consistent title style across multiple charts, you can use the title configuration (config: { title: {...} }). This method is handy for setting themes.

{
  "config": {
    "title": {
      "font": "Calibri",
      "fontSize": 18,
      "color": "blue"
    }
  },
  "title": "Consistent Title Style",
  "data": {"url": "data/barley.json"},
  "mark": "bar",
  "encoding": {
    "x": {"field": "yield", "type": "quantitative"},
    "y": {"field": "variety", "type": "nominal"}
  }
}

In this example, all titles will use the Calibri font, size 18, and blue color unless overridden by specific chart titles.

FAQs

1. How do I customize the font weight on title?

You can use the fontWeight property like this:

{
  "title": {
    "text": "A Normal Customized Title",
    "fontWeight": "normal"
  },
  "data": {"url": "data/barley.json"},
  "mark": "bar",
  "encoding": {
    "x": {"field": "yield", "type": "quantitative"},
    "y": {"field": "variety", "type": "nominal"}
  }
}
{
  "title": {
    "text": "A Bold Customized Title",
    "fontWeight": "bold"
  },
  "data": {"url": "data/barley.json"},
  "mark": "bar",
  "encoding": {
    "x": {"field": "yield", "type": "quantitative"},
    "y": {"field": "variety", "type": "nominal"}
  }
}

2. How do I use some font style like normal or italic on title?

Just use the fontStyle property like this:

{
  "title": {
    "text": "A Normal Customized Title",
    "fontStyle": "normal"
  },
  "data": {"url": "data/barley.json"},
  "mark": "bar",
  "encoding": {
    "x": {"field": "yield", "type": "quantitative"},
    "y": {"field": "variety", "type": "nominal"}
  }
}
{
  "title": {
    "text": "A Italic Customized Title",
    "fontStyle": "italic"
  },
  "data": {"url": "data/barley.json"},
  "mark": "bar",
  "encoding": {
    "x": {"field": "yield", "type": "quantitative"},
    "y": {"field": "variety", "type": "nominal"}
  }
}

3. How do I customize the style of title with CSS?

For now, there is no way to customize the style of title with CSS.

4. How do I use HTML for the title?

For now, there is no way to inline HTML within the title.