# Using Transform Aggregation

You can create Vega-based visualizations with render properties that are driven by aggregated statistics. You can use Vega transform aggregation and formula expressions to automate the process of gathering statistical information about a rendered query. By doing so, you do not have to run an SQL prequery to get the information, thereby reducing the time it takes to process and render a chart.

The following examples show how to use transforms in Vega to do the following:

Render a heatmap that is colored using dynamic statistics of the bins

Create a geo pointmap with different transform modes

**NOTE:** You can see Vega examples in the OmniSci Vega Editor. For more information about the OmniSci Vega engine, see Try Vega.

## Rendering a Heatmap Colored by Bin Statistics

The following heatmap example demonstrates the benefits of Vega transforms for performance and reducing redundancy:

First, the example shows using an SQL expression to render a heatmap, as well as an additional expression to color the hexagonal bins according to the

`min`

and`max`

of the`cnt`

value of the aggregated bins from the query.Then, you will see how to render the heatmap and color the bins directly in Vega by using

`source`

data definitions and performing aggregation transforms on that data, decreasing chart rendering time and redundancy.

### SQL Queries

The following is a typical SQL query used for rendering a hexagonal heatmap:

To color the hexagonal bins according to the `min`

and `max`

of the `cnt`

value of the bins from the query, you need to run a prequery to gather these statistics manually. Here, this is done using a subquery SQL statement:

The values returned from this query can then be embedded in the Vega code to color the heatmap bins. Notice that the second query does an aggregation over the query, effectively running the query twice.

To avoid the redundancy and expense of running the query twice, you can instead specify the aggregation in Vega.

### Render the Heatmap in Vega

The following Vega code renders the heatmap colored by aggregated statistics using transforms.

The data section named `heatmap_stats`

has a source data table defined by the `"source": "heatmap_query"`

line:

The `"heatmap_stats"`

data takes as input the `"heatmap_query"`

data, which is the data supplied by the SQL query. Use the `source`

data type to apply intermediary steps or expressions (transforms) to the input source data.

For information about syntax and requirements for the `source`

and `transform`

properties, see the Data property.

To color the data according to the range of values defined by two standard deviations from the mean, edit the `"heatmap_stats"`

section as follows to:

Aggregate the minimum, maximum, average, and sampled standard deviation of the count column.

Use formula expressions to calculate values that are two standard deviations from the average.

Then, reference these values in the `scale`

domain:

Performing these calculations in Vega improves performance because the SQL query is only run once and the aggregated statistics are done “on the fly.” Because the query is not repeated in a statistical prequery step, you can reduce the full render time by half by performing the statistics step in Vega at render time.

## Creating a Geo Pointmap Using Transforms to Drive Color and Size

This section shows how to use Vega tranforms to drive the color and size of points in a geo pointmap. Specifically, it show examples using the following aggregation transforms:

`distinct`

: An array of distinct values from an input data column.

`median`

: The median of an input data column.

`quantile`

: An array of quantile separators; operates on numeric columns and takes the following pameters:

`numQuantiles`

: The number of contiguous intervals to create; returns the separators for the intervals. The number of separators equals`numQuantiles - 1`

.`includeExtrema`

: Whether to include min and max values (extrema) in the resulting separator array. The size of the resulting separator array will be`numQuantiles`

+ 1.

As with the heatmap example described earlier, using Vega transforms eliminate the need for an SQL prequery and significantly improves performance for dynamic operations.

The examples that follow use a Twitter dataset to create a geo pointmap.

For more information about the aggregate functions used in these examples, see transforms in the Vega Data reference.

### Distinct

In the following example, the size of the points in a geo pointmap are defined by the numeric range two standard deviations from the average number of followers of the input data. The color of the points is driven by the distinct languages of the input data. To calculate the distinct languages, you could run a prequery using DISTINCT and then populate a Vega color scale with the results. However, the query would need to be run before every render update if the distinct data is meant to be dynamic, which would be very costly.

With the `distinct`

Vega transform, this can be performed when evaluating the Vega code in the backend, so you do not need to run the prequery. This can improve performance considerably.

This Vega code results in this image:

### Median

Outliers in a dataset can significantly skew statistics such as AVG and STDDEV. To mitigate this, you can use `median`

and `quantile`

to create a more meaningful probability distribution of the data. Median and quantiles are computed dynamically when Vega is evaluated and can be used to drive different render properties.

The following hexmap example uses `median`

to drive the color of the hex bins. Notice in the final render that roughly half of the bins are colored red, and the other half are blue.

### Quantile

The quantile function takes two additional parameters:

`numQuantiles`

is the number of contiguous intervals to create and returns the separators for the intervals. The number of returned separators is`numQuantiles`

- 1.`includeExtrema`

is a`true`

or`false`

value indicating whether to include the extrema (min and max) in the resulting separator array. If`true`

, the number of returned values is`numQuantiles`

+ 1.

To see how a quantile works, consider a query that results in this set of values for `"followers"`

:

`{3, 6, 7, 8, 8, 10, 13, 15, 16, 20}`

With a quantile operator defined as `{"type": "quantile", "numQuantiles": 4}`

, the result of the operator would be the following array:

`[7, 9, 15]`

25% of the data has less than 7 followers, 25% has between 7 and 9, 25% has between 9 and 15, and 25% has more than 15.

With a quantile operator defined as `{"type": "quantile", "numQuantiles": 4, "includeExtrema": true}`

, the result of the operator would be the following array:

`[3, 7, 9, 15, 20]`

.

With `"includeExtrema" == true`

, the min and max are included in the resulting array, so 25% of the data has between 3 and 7 followers, 25% has between 7 and 9, 25% has between 9 and 15, and 25% has between 15 and 20.

The following Vega code snippet gets the octiles (8-quantiles) and sextiles (6-quantiles) of a column called `"followers"`

:

For more information about quantiles, see: https://en.wikipedia.org/wiki/Quantile.

Here is a more complete example using sextiles. Notice in the resulting image approximately the same number of hexagons appears in each of the six quantile groups colored blue to red, from left to right.

Last updated