Vega Lite
How Can I Customize Legends in Vega-Lite?

Understanding Vega-Lite Legends: A Friendly Guide

Hey there! If you've ever wondered how to make those awesome color, shape, and size legends appear in your Vega-Lite visualizations, you're in the right place. Legends help people understand the scales you're using, making your charts a lot more user-friendly.

Just like axes, which help interpret scales with positional ranges, legends help with ranges like colors, shapes, and sizes. By default, Vega-Lite does a pretty good job generating these legends automatically. But hey, sometimes you want to add your personal touch, right? Let’s explore how you can customize these legends to suit your needs.


Legend Types: Gradient and Symbol

When you use color channels with quantitative data fields (e.g., numbers) or temporal fields (e.g., dates), Vega-Lite usually creates gradient legends by default. Here's a quick example:

{
  "mark": "point",
  "encoding": {
    "color": {
      "field": "value",
      "type": "quantitative"
    }
  }
}

For other types of data, you'll get symbol legends. Check this out:

{
  "mark": "point",
  "encoding": {
    "color": {
      "field": "category",
      "type": "nominal"
    }
  }
}

Combined Legends: When One Legend is Enough

Sometimes, you might use more than one encoding channel for the same data field. For instance, using both color and shape to differentiate data categories is a common case. Vega-Lite is smart enough to combine these into one legend automatically.

{
  "mark": "point",
  "encoding": {
    "color": {
      "field": "Origin",
      "type": "nominal"
    },
    "shape": {
      "field": "Origin",
      "type": "nominal"
    }
  }
}

Example: Combined Legend


Customizing Legends: Make It Your Own

Alright, now let’s get into the fun part: customizing your legends. You can specify a legend object within your encoding channel’s field definition to tweak legends just the way you like them.

{
  "encoding": {
    "color": {
      "field": "value",
      "type": "quantitative",
      "legend": {
        "title": "Custom Title",
        "labelColor": "red",
        "type": "gradient"
      }
    }
  }
}

Properties You Can Customize

You’ve got a lot of options to play with! Here are some of the categories you can look into:

  1. General: Adjust the position, orientation, and padding.
  2. Gradient: Customize the length, opacity, and thickness of gradient legends.
  3. Labels: Modify text alignment, font, size, and color.
  4. Symbols: Change the size, shape, color, and opacity of symbols.
  5. Symbol Layout: Configure the layout like columns and padding.
  6. Title: Style the title's font, color, size, and more.

For detailed customization options, check the full list of legend properties here.


Configuring Legends Globally

If you want a consistent look across all your legends, you can specify your preferences in the configuration object (config).

{
  "config": {
    "legend": {
      "labelFont": "Arial",
      "labelFontSize": 12,
      "titleFont": "Helvetica",
      "titleColor": "blue"
    }
  }
}

This way, all your legends will follow these global styling rules unless you override them locally in specific field definitions.


Frequently Asked Questions

1. How do I remove a legend?

Simply set the legend property to null in the field definition.

{
  "encoding": {
    "color": {
      "field": "value",
      "type": "quantitative",
      "legend": null
    }
  }
}

2. Can I combine legends for different fields?

No, Vega-Lite combines legends only when multiple channels encode the same field.

3. How can I position the legend?

Use the orient property in the legend object. Possible values are left, right, top, bottom.

{
  "encoding": {
    "color": {
      "field": "value",
      "type": "quantitative",
      "legend": {
        "orient": "right"
      }
    }
  }
}