oeuth

Of all tools for data visualization, I mostly enjoy working with Vega-Lite. As I enhance my skills in making the visualizations using it, I become increasingly interesting not just in the “dataviz” part of its capabilities, but also on making it aesthetically compelling, as well as challenging myself to reach the limits of what is possible with Vega-Lite. I am by no means near the limits so far, however I got increasingly interested in replicating the charts I see throughout the internet.

One that caught me attention is Tableau’s Viz of the Day: Seven Bar Charts to Visualise Year-Over-Year Growth

Here is what my attempt to replicate the charts in vega-lite looks like (each chart can be viewed in vega-editor via burger menu on the right top):

Initial Dataset

CategoryCurrentPrevious
Appliances22962.17413183.887
Art5122.6164004.32
Binders47133.13829871.657
Envelopes1926.6322698.966
Fasteners604.648667.028
Labels2416.9941276.072
Paper15947.48811683.83
Storage39407.74635680.368
Supplies2727.5587482.186

Bar-in-bar

This one looks like it’s the most straightofrward one. Still, before moving forward, we need to perform several transformations that would allow us to calculate percentages and neatly show them as text:

  {"transform": [
    {
      "calculate": "toNumber(datum.Current)",
      "as": "cur_value"
    },
    {
      "calculate": "toNumber(datum.Previous)",
      "as": "prev_value"
    },
    {
      "calculate": "(datum.cur_value - datum.prev_value) / datum.prev_value",
      "as": "difference"
    },
    {
      "calculate": "datum.difference > 0",
      "as": "is_positive"
    },
    {
      "calculate": "max(datum.cur_value, datum.prev_value)",
      "as": "max_value"
    },
    {
      "calculate": "format(datum.difference, '+.0%') + ' | ' + format(datum.cur_value, '$,.0f')",
      "as": "text_label"
    }
  ]}

After this, we can make the following layers:

  • Wider bar mark with previous value “background”
  • Thinner bar mark with actual numbers
  • text mark with calculated text_label
  • two separate point mark layers, each with a different color but more importantly - triangle-up and triangle-down. We cannot encode the two marks dynamically (or, rather, we cannot make dynamic fill color which is my goal here) so we’ll need to do it separately for positive and negative changes.

Highlighted Absolute Change

This one largely repeats the bar-in-bar, with the exception that the highlighted changes are achieved by combining x and x2 channels combined with color

Bullet

Key difference in schema here - instead of highlighted bar mark, we use tick with exact value for the previous period.

Direction Arrows

Error Bars