How Do You Use Predicates in Vega-Lite?
Hey there, data enthusiast! Ever wondered how you can filter and test your data points in Vega-Lite to make your visualizations more insightful? Enter predicates! In this guide, we'll break down the different types of predicates and how you can use them.
What Are Predicates?
Predicates are conditions you can use to test your data points in a filter transform or a test
property in conditional encoding. They help you decide which data points to include in your visualizations based on specific conditions.
Types of Predicates
-
Vega Expression: A string where
datum
refers to the current data object.- Example:
datum.b2 > 60
checks if the value in theb2
field is greater than 60.
- Example:
-
Field Predicates: Conditions on specific fields.
-
Parameter Predicates: Conditions based on parameter selections.
- Example:
{"param": "brush"}
filters data points based on thebrush
selection.
- Example:
-
Logical Composition: Combine multiple predicates using
and
,or
,not
.
Field Predicates — What Are They and How to Use Them?
Field predicates test if a specific field in your data meets certain criteria.
Equal Predicate
To check if a field equals a specific value:
{"field": "car_color", "equal": "red"}
This checks if the car_color
field's value is "red"
.
Less Than Predicate
To check if a field is less than a specific value:
{"field": "height", "lt": 180}
This checks if the height
field's value is less than 180
.
Less Than or Equal Predicate
To check if a field is less than or equals to a specific value:
{"timeUnit": "year", "field": "Year", "lte": "2000"}
This checks if the Year
field's value is less than or equals to "2000"
.
Greater Than Predicate
To check if a field is greater than a specific value:
{"field": "state", "gt": "Arizona"}
This checks if the state
field's value is greater than "Arizona"
by string comparison.
Greater Than or Equal Predicate
To check if a field is greater than or equals to a specific value:
{"field": "height", "gte": 0}
This checks if the height
field's value is greater than or equals to 0
.
Range Predicate
To check if a field is within a range:
{"field": "x", "range": [0, 5]}
This checks if the x
field's value is between 0
and 5
.
One-Of Predicate
To check if a field's value is one of specified values:
{"field": "car_color", "oneOf": ["red", "yellow"]}
This checks if the car_color
field's value is "red"
or "yellow"
.
Valid Predicate
To check if a field's value is valid (not null
or NaN
):
{"field": "car_color", "valid": true}
This checks if the car_color
field's value is valid.
Parameter Predicates
Use parameter predicates to filter data based on selections:
{"param": "brush"}
This keeps data points within the brush
selection.
Logical Composition
Combine predicates for more complex conditions:
-
{"and": [{"field": "height", "gt": 0}, {"field": "height", "lt": 180}]}
- Checks if
height
is between0
and180
.
- Checks if
-
{"not": {"field": "x", "range": [0, 5]}}
- Checks if
x
is not between0
and5
.
- Checks if
FAQ Section
What is a predicate in Vega-Lite?
A predicate is a condition used to test data points to decide whether they should be included in your visualization.
How do you use a field predicate?
You specify a field and a condition, like whether the field's value is equal to a specific value or within a range.
Can predicates be combined?
Yes, you can use logical operators like and
, or
, and not
to combine multiple predicates for more complex conditions.
And there you have it! With predicates, you can make your data visualizations smarter and more specific. Happy visualizing!