v5.8.0 (latest)

Overview

Installation and Configuration

Loading and Exporting Data

Immerse

Data Science

APIs and Interfaces

Tutorials

Troubleshooting and Special Topics

Powered By GitBook

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:

1

SELECT

2

reg_hex_horiz_pixel_bin_x(conv_4326_900913_x(lon),conv_4326_900913_x(-157),conv_4326_900913_x(157),conv_4326_900913_y(lat),conv_4326_900913_y(-63),conv_4326_900913_y(81),9.9667,11.5085,0,0,897,647) as x,

3

reg_hex_horiz_pixel_bin_y(conv_4326_900913_x(lon),conv_4326_900913_x(-157),conv_4326_900913_x(157),conv_4326_900913_y(lat),conv_4326_900913_y(-63),conv_4326_900913_y(81),9.9667,11.5085,0,0,897,647) as y,

4

count(*) as cnt

5

FROM tweets_nov_feb

6

WHERE ((lon >= -157 AND lon <= 157) AND (lat >= -63 AND lat <= 81))

7

GROUP BY x, y;

Copied!

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:

1

SELECT

2

min(cnt), max(cnt)

3

FROM (SELECT

4

reg_hex_horiz_pixel_bin_x(conv_4326_900913_x(lon),conv_4326_900913_x(-157),conv_4326_900913_x(157),conv_4326_900913_y(lat),conv_4326_900913_y(-63),conv_4326_900913_y(81),9.9667,11.5085,0,0,897,647) as x,

5

reg_hex_horiz_pixel_bin_y(conv_4326_900913_x(lon),conv_4326_900913_x(-157),conv_4326_900913_x(157),conv_4326_900913_y(lat),conv_4326_900913_y(-63),conv_4326_900913_y(81),9.9667,11.5085,0,0,897,647) as y,

6

count(*) as cnt

7

FROM tweets_nov_feb

8

WHERE ((lon >= -157 AND lon <= 157) AND (lat >= -63 AND lat <= 81))

9

GROUP BY x, y

10

);

Copied!

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.

1

{

2

"width": 897,

3

"height": 647,

4

"data": [

5

{

6

"name": "heatmap_query",

7

"sql": "SELECT reg_hex_horiz_pixel_bin_x(conv_4326_900913_x(lon),conv_4326_900913_x(-157),conv_4326_900913_x(157),conv_4326_900913_y(lat),conv_4326_900913_y(-63),conv_4326_900913_y(81),9.9667,11.5085,0,0,897,647) as x, reg_hex_horiz_pixel_bin_y(conv_4326_900913_x(lon),conv_4326_900913_x(-157),conv_4326_900913_x(157),conv_4326_900913_y(lat),conv_4326_900913_y(-63),conv_4326_900913_y(81),9.9667,11.5085,0,0,897,647) as y, count(*) as cnt FROM tweets_nov_feb WHERE ((lon >= -157 AND lon <= 157) AND (lat >= -63 AND lat <= 81)) GROUP BY x, y"

8

},

9

{

10

"name": "heatmap_stats",

11

"source": "heatmap_query",

12

"transform": [

13

{

14

"type": "aggregate",

15

"fields": ["cnt", "cnt"],

16

"ops": ["min", "max"],

17

"as": ["mincnt", "maxcnt"]

18

}

19

]

20

}

21

],

22

"scales": [

23

{

24

"name": "heat_color",

25

"type": "quantize",

26

"domain": {"data": "heatmap_stats", "fields": ["mincnt", "maxcnt"]},

27

"range": ["#115f9a", "#1984c5", "#22a7f0", "#48b5c4", "#76c68f",

28

"#a6d75b", "#c9e52f", "#d0ee11", "#d0f400"

29

]

30

}

31

],

32

"marks": [

33

{

34

"type": "symbol",

35

"from": {

36

"data": "heatmap_query"

37

},

38

"properties": {

39

"shape": "hexagon-horiz",

40

"xc": {

41

"field": "x"

42

},

43

"yc": {

44

"field": "y"

45

},

46

"width": 9.9667,

47

"height": 11.5085,

48

"fillColor": {

49

"scale": "heat_color",

50

"field": "cnt"

51

}

52

}

53

}

54

]

55

}

Copied!

The data section named heatmap_stats has a source data table defined by the "source": "heatmap_query" line:

1

"name": "heatmap_stats",

2

"source": "heatmap_query",

Copied!

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.

1

{

2

"name": "heatmap_stats",

3

"source": "heatmap_query",

4

"transform": [

5

{

6

"type": "aggregate",

7

"fields": ["cnt", "cnt", "cnt", "cnt"],

8

"ops": ["min", "max", "avg", "stddev"],

9

"as": ["mincnt", "maxcnt", "avgcnt", "stdcnt"]

10

},

11

{

12

"type": "formula",

13

"expr": "max(mincnt, avgcnt-2*stdcnt)",

14

"as": "mincnttouse"

15

},

16

{

17

"type": "formula",

18

"expr": "min(maxcnt, avgcnt+2*stdcnt)",

19

"as": "maxcnttouse"

20

}

21

]

22

}

Copied!

Then, reference these values in the scale domain:

1

{

2

"name": "heat_color",

3

"type": "quantize",

4

"domain": {"data": "heatmap_stats", "fields": ["mincnttouse", "maxcnttouse"]},

5

"range": ["#115f9a", "#1984c5", "#22a7f0", "#48b5c4", "#76c68f",

6

"#a6d75b", "#c9e52f", "#d0ee11", "#d0f400"

7

]

8

}

Copied!

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.

1

{

2

"width": $width,

3

"height": $height,

4

"data": [

5

{

6

"name": "table",

7

"sql": "SELECT conv_4326_900913_x(lon) as x,conv_4326_900913_y(lat) as y,followers,lang,rowid FROM tweets_2017_may WHERE (lon between $minLon and $maxLon AND lat between $minLat and $maxLat) LIMIT 200000"

8

},

9

{

10

"name": "xformtable",

11

"source": "table",

12

"transform": [

13

{

14

"type": "aggregate",

15

"fields": ["followers", "followers", "followers", "followers", "lang"],

16

"ops": ["min", "max", "avg", "stddev", "distinct"],

17

"as": ["minfol", "maxfol", "avgfol", "stdfol", "distinctlang"]

18

},

19

{

20

"type": "formula",

21

"expr": "max(minfol, avgfol-2*stdfol)",

22

"as": "minfoltouse"

23

},

24

{

25

"type": "formula",

26

"expr": "min(maxfol, avgfol+2*stdfol)",

27

"as": "maxfoltouse"

28

}

29

]

30

}

31

],

32

"scales": [

33

{

34

"name": "x",

35

"type": "linear",

36

"domain": [

37

$minXBounds,

38

$maxXBounds

39

],

40

"range": "width"

41

},

42

{

43

"name": "y",

44

"type": "linear",

45

"domain": [

46

$minYBounds,

47

$maxYBounds

48

],

49

"range": "height"

50

},

51

{

52

"name": "size",

53

"type": "linear",

54

"domain": {"data": "xformtable", "fields": ["minfoltouse", "maxfoltouse"]},

55

"range": [

56

1, 20

57

],

58

"clamp": true

59

},

60

{

61

"name": "color",

62

"type": "ordinal",

63

"domain": {"data": "xformtable", "field": "distinctlang"},

64

"range": [

65

"blue", "red", "green", "yellow", "magenta", "purple", "teal"

66

]

67

}

68

],

69

"marks": [

70

{

71

"type": "points",

72

"from": {

73

"data": "table"

74

},

75

"properties": {

76

"x": {

77

"scale": "x",

78

"field": "x"

79

},

80

"y": {

81

"scale": "y",

82

"field": "y"

83

},

84

"fillColor": {

85

"scale": "color",

86

"field": "lang"

87

},

88

"size": {

89

"scale": "size",

90

"field": "followers"

91

}

92

}

93

}

94

]

95

}

Copied!

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.

1

{

2

"width": $width,

3

"height": $height,

4

"data": [

5

{

6

"name": "heatmap_query",

7

"sql": "SELECT reg_hex_horiz_pixel_bin_x(conv_4326_900913_x(lon),$minXBounds,$maxXBounds,conv_4326_900913_y(lat),$minYBounds,

8

$maxYBounds,9.931506849315069,11.467916305821335,0,0,$width,$height) as x,reg_hex_horiz_pixel_bin_y(conv_4326_900913_x(lon),$minXBounds,$maxXBounds,

9

conv_4326_900913_y(lat),$minYBounds,$maxYBounds,9.931506849315069,11.467916305821335,0,0,$width,$height) as y, count(*) as color FROM tweets_2017_may

10

WHERE ((lon >= $minLon AND lon <= $maxLon) AND (lat >= $minLat AND lat <= $maxLat)) GROUP BY x, y"

11

},

12

{

13

"name": "heatmap_statistics",

14

"source": "heatmap_query",

15

"transform": [

16

{

17

"type": "aggregate",

18

"fields": ["color"],

19

"ops": ["median"],

20

"as": ["mediancolor"]

21

}

22

]

23

}

24

],

25

"scales": [

26

{

27

"name": "heat_color",

28

"type": "threshold",

29

"domain": {"data": "heatmap_statistics", "field": "mediancolor"},

30

"range": [

31

"blue", "red"

32

]

33

}

34

],

35

"marks": [

36

{

37

"type": "symbol",

38

"from": {

39

"data": "heatmap_query"

40

},

41

"properties": {

42

"shape": "hexagon-horiz",

43

"xc": {

44

"field": "x"

45

},

46

"yc": {

47

"field": "y"

48

},

49

"width": 9.931506849315069,

50

"height": 11.467916305821335,

51

"fillColor": {

52

"scale": "heat_color",

53

"field": "color"

54

}

55

}

56

}

57

]

58

}

Copied!

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":

1

...

2

{

3

"name": "xformtable",

4

"source": "table",

5

"transform": [

6

{

7

"type": "aggregate",

8

"fields": ["followers", "followers"],

9

"ops": [{"type": "quantile", "numQuantiles": 8}, {"type": "quantile", "numQuantiles": 6}],

10

"as": ["octile_fol", "sextile_fol"]

11

}

12

]

13

}

14

...

Copied!

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.

1

{

2

"width": $width,

3

"height": $height,

4

"data": [

5

{

6

"name": "heatmap_query",

7

"sql": "SELECT reg_hex_horiz_pixel_bin_x(conv_4326_900913_x(lon),$minXBounds,$maxXBounds,conv_4326_900913_y(lat),

8

$minYBounds,$maxYBounds,9.931506849315069,11.467916305821335,0,0,$width,$height) as x,reg_hex_horiz_pixel_bin_y(conv_4326_900913_x(lon),

9

$minXBounds,$maxXBounds,conv_4326_900913_y(lat),$minYBounds,$maxYBounds,9.931506849315069, 11.467916305821335,0,0,$width,$height) as y,

10

count(*) as color FROM tweets_2017_may WHERE ((lon >= $minLon AND lon <= $maxLon) AND (lat >= $minLat AND lat <= $maxLat)) GROUP BY x, y"

11

},

12

{

13

"name": "heatmap_statistics",

14

"source": "heatmap_query",

15

"transform": [

16

{

17

"type": "aggregate",

18

"fields": ["x"],

19

"ops": [{"type": "quantile", "numQuantiles": 6}],

20

"as": ["sextilex"]

21

}

22

]

23

}

24

],

25

"scales": [

26

{

27

"name": "heat_color",

28

"type": "threshold",

29

"domain": {"data": "heatmap_statistics", "field": "sextilex"},

30

"range": [

31

"rgb(0, 0, 255)", "rgb(51, 0, 204)", "rgb(102, 0, 153)", "rgb(153, 0, 102)", "rgb(204, 0, 51)", "rgb(255, 0, 0)"

32

]

33

}

34

],

35

"marks": [

36

{

37

"type": "symbol",

38

"from": {

39

"data": "heatmap_query"

40

},

41

"properties": {

42

"shape": "hexagon-horiz",

43

"xc": {

44

"field": "x"

45

},

46

"yc": {

47

"field": "y"

48

},

49

"width": 9.931506849315069,

50

"height": 11.467916305821335,

51

"fillColor": {

52

"scale": "heat_color",

53

"field": "x"

54

}

55

}

56

}

57

]

58

}

Copied!

Last modified 1yr ago