Professional Documents
Culture Documents
Vega-Lite Tutorial UC Davis
Vega-Lite Tutorial UC Davis
Vega-Lite Tutorial UC Davis
● 9:15am - noon*
● 15 minutes break
Outline
Effective Visualization Design
Vega-Lite
Future of Vega-Lite
Why Visualization?
Visualizations are an effective tool to explore and communicate data.
● Data abstraction
● Task abstraction
How is it shown?
● Visual encoding
What? Data
Usually Tabular Data Data Types
Fields (columns)
Quantitative
Ordinal
Nominal
Temporal
There are other types such as
networks but these are not the focus.
Quantitative to Ordinal with binning
Why? Task
How? Visual Encoding
Facet
Partition data by an ordinal variable.
Color
<
Which one is better?
>
Which one is better?
?
Components of a visualization
Data + Transformation
Legend
Legend label
green
Vega-Lite
Future of Vega-Lite
What is Vega-Lite
● A Visualization Specification Language
● a concise JSON syntax for rapidly creating visualizations in analysis.
○ Can serve as file format for creating and sharing visualizations
● data source
● (optional) data transformation
● Graphical mark type
● Mappings between data and encoding channels (x, y, color, etc.) – encoding
○ Properties of Scale, Axis, Legend
● Faceting
JSON (Javascript Object Notation)
Text-based data interchange format based on Javascript’s Object.
{
“numberProperty”: 1234,
“stringProperty”: “abc”,
“dateProperty”: “1/1/2013”,
“arrayProperty”: [1,2,3],
“objectProperty: {
“childNumberProperty”: 444
}
}
Please open
http://vega.github.io/vega-editor/?mode=vega-lite
Tutorial A:
Getting Started with Vega-Lite
(adapted from https://vega.github.io/vega-lite/tutorials/getting_started.html)
Data
[
{"a": "C", "b": 2},
{"a": "C", "b": 7},
{"a": "C", "b": 4},
{"a": "D", "b": 1},
Given a table We can represent it in JSON. {"a": "D", "b": 2},
{"a": "D", "b": 6},
{"a": "E", "b": 8},
{"a": "E", "b": 4},
{"a": "E", "b": 7}
]
Describing data inline with data.values
{
"data": {
"values": [
{"a": "C", "b": 2}, {"a": "C", "b": 7}, {"a": "C", "b": 4},
{"a": "D", "b": 1}, {"a": "D", "b": 2}, {"a": "D", "b": 6},
{"a": "E", "b": 8}, {"a": "E", "b": 4}, {"a": "E", "b": 7}
]
}
}
Note: Vega-Lite also supports loading data from URL from CSV/JSON files
mark
{
"data": {
"values": [
{"a": "C", "b": 2}, {"a": "C", "b": 7}, {"a": "C", "b": 4},
{"a": "D", "b": 1}, {"a": "D", "b": 2}, {"a": "D", "b": 6},
{"a": "E", "b": 8}, {"a": "E", "b": 4}, {"a": "E", "b": 7}
]
},
"mark": "point"
}
encoding
{
"data": {
"values": [
{"a": "C", "b": 2}, {"a": "C", "b": 7}, {"a": "C", "b": 4},
{"a": "D", "b": 1}, {"a": "D", "b": 2}, {"a": "D", "b": 6},
{"a": "E", "b": 8}, {"a": "E", "b": 4}, {"a": "E", "b": 7}
]
},
"mark": "point",
"encoding": {
"x": {"field": "a", "type": "nominal"}
}
}
encoding.x.axis.title = “Average of b”
Publishing Vega-Lite Visualization on a web-page
See example page: http://vega.github.io/vega-lite-demo/
...
From https://www.ncdc.noaa.gov/cdo-web/
Describing data from url with data.url
{
"data": {
"url": "data/seattle-weather.csv",
"formatType": "csv"
}
}
{
"data": {"url": "data/seattle-weather.csv", "formatType": "csv"},
"mark": "bar",
"encoding": {
"x": {"bin": true, "field": "precipitation", "type": "quantitative"},
"y": {"aggregate": "count", "field": "*", "type": "quantitative"}
}
}
Exercise: create dot plots or histograms for
temp_max, temp_min, wind, weather, date
TimeUnit for Dates
{
"data": {"url": "data/seattle-weather.csv", "formatType": "csv"},
"mark": "tick",
"encoding": {
"x": {"timeUnit": "month", "field": "precipitation", "type": "quantitative"}
}
}
{
"data": {"url": "data/seattle-weather.csv", "formatType": "csv"},
"mark": "bar",
"encoding": {
"x": {
"field": "weather", "type": "ordinal",
"sort": {"aggregate": "count", "field": "*"}
},
"y": {"aggregate": "count", "field": "*", "type": "quantitative"}
}
}
Calculate New Fields
{
"data": {"url": "data/seattle-weather.csv", "formatType": "csv"},
"transform": {
"calculate": [{
"field": "temp_range",
"expr": "datum.temp_max - datum.temp_min"
}]
},
"mark": "bar",
"encoding": {
"x": {"bin": true, "field": "temp_range", "type": "quantitative"},
"y": {"aggregate": "count", "field": "*", "type": "quantitative"}
}
}
Relationship between variables
Try to create a visualization to answer the following questions:
{
"data": {"url": "data/seattle-weather.csv", "formatType": "csv"},
"mark": "bar",
"encoding": {
"x": {"bin": "true", "field": "temp_max", "type": "quantitative"},
"y": {"aggregate": "count", "field": "*", "type": "quantitative"},
"color": {"field": "weather", "type": "ordinal"}
}
}
Faceting
{
"data": {"url": "data/seattle-weather.csv", "formatType": "csv"},
"mark": "bar",
"encoding": {
"x": {"bin": "true", "field": "temp_max", "type": "quantitative"},
"y": {"aggregate": "count", "field": "*", "type": "quantitative"},
"row”: {"field": "weather", "type": "ordinal"}
}
}
Filtering
{
"data": {"url": "data/seattle-weather.csv", "formatType": "csv"},
"mark": "bar",
"transform": {
"filter": "datum.weather ==='fog'"
},
"encoding": {
"x": {"bin": "true", "field": "temp_max", "type": "quantitative"},
"y": {"aggregate": "count", "field": "*", "type": "quantitative"}
}
}
More Examples
https://vega.github.io/vega-lite/examples/
and docs
http://vega.github.io/vega-lite/docs/
Outline
Effective Visualization Design
Vega-Lite
Future of Vega-Lite
Set up
https://github.com/vega/ipyvega
Vega-Lite
Future of Vega-Lite
Please open
http://vega.github.io/polestar/
Outline
Effective Visualization Design
Vega-Lite
Future of Vega-Lite
Composition and Interaction
Q&A
Relationship with D3 and Vega
D3’s low-level building blocks to Vega
Vega-Lite
Vega
D3
Faceting
-