Vega-Lite Data Visualizations
A high-level grammar of interactive graphics, embedded securely in your Markdown files.
While Chart.js is perfect for standard business charts, Vega-Lite is designed for complex data analysis. It uses a “grammar of graphics” approach (similar to ggplot2 in R or D3.js) where you map data to visual properties (like the x-axis, y-axis, color, and size) using a strictly defined JSON syntax.
To embed a Vega-Lite graphic, wrap the valid JSON inside a vega or vega-lite fenced code block.
The Grammar of Graphics
In Vega-Lite, you define the dataset, the mark (how the data is drawn, e.g., point, line, bar), and the encoding (which data column maps to which visual channel).
A Basic Scatter Plot
|
|
|
What makes the above code block powerful is the encoding. By simply stating "color": {"field": "Origin"}, Vega-Lite automatically analyzes the dataset, extracts the unique origins (USA, Europe, Japan), assigns them distinct colors, and generates a legend.
Bar Charts and Aggregation
Vega-Lite can perform data aggregation natively before plotting. Instead of pre-calculating the mean of your data, you can ask Vega-Lite to do it.
|
|
|
Layering Marks
Because Vega-Lite uses a grammar structure, it’s trivial to layer a line on top of a bar chart.
|
|
|
By embedding JSON directly into your Markdown files, you eliminate the need for external Excel sheets or Python scripts when generating dynamic, programmatic reports.