I've been working with Vega-Lite to build a complex, faceted visualization. However, I've encountered an issue that, as far as I'm aware, is known within the community. I am trying to make a faceted chart with columns based on a category in my data. Each column should have bars corresponding to different measures, which are represented by rows. Furthermore, each bar should have an independent scale on the X axis. However, the number of measures varies per category. The issue is that Vega-Lite displays the same number of rows in each column, even if there is no data available for some measures in certain categories. In other words, the facet grid is calculated prior to any data filtering being applied and it remains constant, leading to some facets having an excessive number of empty rows.
What I am hoping to achieve is that each facet is independent not only in terms of the X axis scale, but also in terms of the number of rows. I have created a demonstration of this issue on the Vega online editor. You can view it here: Vega Editor
Any suggestions on how to rectify this or if there is an alternative workaround available would be greatly appreciated.
Thank you for your time and assistancevega.github.io
I came up with a potential workaround for this solution using a "window"
transform (link) to compute a new field to use for the "row"
facet. At the start of the specification, you can use a "window"
transform grouped by the category to determine the row index for each subplot:
"transform": [
{
"window": [{"op": "row_number"}],
"groupby": ["VEDA Measure Category"]
}
],
The modified data would look like the following, with the last column corresponding to the newly computed "row_number"
. Notice that when we switch from "Category 1" to "Category 2", the count from 1 starts over:
Then, you can simply change your "facet"
definition to use "row_number"
instead of the measure name. Here is a link to the modified example in the Vega editor. Notice that I also set the "header"
to null
on line 15 to remove the overlapping labels.