Deneb Example - Faceted Bar Chart

Deneb/Vega-Lite can be used to create a faceted bar chart to enhance the visualization and interpretation of insights from data subsets. Earlier this year I saw and infographic of fuel-saving technology use by various automotive manufacturers (https://www.visualcapitalist.com/chart-automakers-adoption-of-fuel-saving-technologies/), and wondered if Deneb/Vega-Lite could be used to produce something similar. The example presented herein uses a public dataset from the U.S. EPA and shows a bar chart of the percent of a manufacturer’s products using a particular technology faceted by technology. This actually was one of the easier Deneb visuals I’ve developed: mostly OOTB syntax was used, with a majority of the time spent fiddling with formatting options to increase the “similarity” to the original.

This example illustrates a number of Deneb/Vega-Lite features, including:
0 - General:

  • use of a “title” block with title and subtitle
  • use of a “transform” block to extend the dataset with in-visual calculation of the concatenated manufacturer sort order and name
  • use of a “facet” block with [Technology] by column with a custom “sort” block and “header” block with key:value pairs for title and label font sizes and paddings
  • use of a “spec” block with 3-layered marks (cell border, bar, and label)

1 - Cell Border Bar Chart:

  • use of a “bar” mark with fixed 100% size, grey border, and transparent fill

2 - Bar Chart:

  • use of a “bar” mark with:
    • 70% opacity
    • the concatenated manufacturer sort order and name on the Y axis (label showing only name portion)
    • the percent value for the X axis (using a fixed 0%-to-100% scale)
    • the Country colours as per the current Power BI theme as enabled by Deneb with a custom “sort” order
    • a standard horizontal legend at the bottom with same-line title and circle symbols

3 - Label:

  • use of a “text” mark with:
    • Vega-Lite percent formatting
    • variable alignment (if percent < 50% then left else right)
    • variable X offset (if percent < 50% then +4 pixels else -4 pixels)
    • variable colour (if percent < 1% then transparent else if percent < 50% then black else white)
Deneb/Vega-Lite JSON Code
{
  "title": {
    "anchor": "start",
    "align": "left",
    "orient": "top",
    "offset": 5,
    "text": "Power BI Faceted Bar Chart using Deneb",
    "font": "Segoe UI",
    "fontSize": 24,
    "fontWeight": "bold",
    "fontStyle": "normal",
    "subtitle": "Fuel-Saving Automotive Technologies 2001-2020, US EPA 2022 Automotive Trends Report",
    "subtitleFont": "Segoe UI",
    "subtitleFontSize": 12,
    "subtitleFontWeight": "normal",
    "subtitleFontStyle": "italic"
  },
  "data": {"name": "dataset"},
  "spacing": 5,
  "transform": [
    {
      "calculate": "datum['Manufacturer Sort'] + '-' + datum['Manufacturer']",
      "as": "_manufacturer"
    }
  ],
  "facet": {
    "column": {
      "field": "Technology",
      "sort": [
        "Turbo",
        "Direct Injection",
        "Cylinder Deactivation",
        "CVT",
        "7+ Gears",
        "Stop/Start",
        "Hybrid",
        "PHEV/EV/FC"
      ],
      "header": {
        "titleFontSize": 18,
        "labelFontSize": 12,
        "labelPadding": 2,
        "titlePadding": -6
      }
    }
  },
  "spec": {
    "width": 125,
    "height": 530,
    "layer": [
      {
        "name": "CELL_BORDER",
        "mark": {
          "type": "bar",
          "fill": "transparent",
          "stroke": "#969696",
          "strokeWidth": 2
        },
        "encoding": {
          "y": {
            "field": "_manufacturer",
            "type": "nominal"
          },
          "x": {"datum": 1}
        }
      },
      {
        "name": "BAR",
        "mark": {
          "type": "bar",
          "opacity": 0.7
        },
        "encoding": {
          "y": {
            "field": "_manufacturer",
            "type": "nominal",
            "axis": {
              "domain": false,
              "ticks": false,
              "title": "Manufacturer",
              "titleFontSize": 18,
              "labelExpr": "slice( datum.value, 4, 100 )",
              "labelFontSize": 14,
              "labelFontWeight": "normal",
              "labelBaseline": "middle"
            }
          },
          "x": {
            "field": "Percent",
            "type": "quantitative",
            "scale": {"domain": [0, 1]},
            "axis": {"title": null}
          },
          "color": {
            "field": "Country",
            "sort": [
              "Japan",
              "South Korea",
              "Germany",
              "United States",
              "Global"
            ],
            "scale": {
              "scheme": "pbiColorNominal"
            },
            "legend": {
              "orient": "bottom",
              "titleOrient": "left",
              "symbolType": "circle",
              "symbolSize": 300,
              "labelFontSize": 12
            }
          }
        }
      },
      {
        "name": "LABEL",
        "mark": {
          "type": "text",
          "fontWeight": "bold",
          "align": {
            "expr": "datum['Percent'] < 0.5 ? 'left' : 'right'"
          },
          "xOffset": {
            "expr": "datum['Percent'] < 0.5 ? 4 : -4"
          },
          "color": {
            "expr": "datum['Percent'] < 0.01 ? 'transparent' : datum['Percent'] < 0.5 ? 'black' : 'white'"
          }
        },
        "encoding": {
          "y": {
            "field": "_manufacturer",
            "type": "nominal"
          },
          "x": {
            "field": "Percent",
            "type": "quantitative",
            "axis": null
          },
          "text": {
            "field": "Percent",
            "format": ".0%"
          }
        }
      }
    ]
  }
}

The intent of this example is not to provide a finished visual, but rather to serve as a starting point for further custom visual development.

Also included is the development sample PBIX using public data from the United States Environmental Protection Agency 2022 EPA Automotive Trends Report, accessed December 10, 2023 (www.epa.gov/automotive-trends/explore-automotive-trends-data). (Only minor modifications to the dataset were used in Power BI, namely a simple unpivot on manufacturer name and model year and the replacement of missing values with a very small number [0.01%].)

This example is provided as-is for information purposes only, and its use is solely at the discretion of the end user; no responsibility is assumed by the author.

Greg
Deneb Examples - Faceted Bar Chart - V3.pbix (1.5 MB)

3 Likes

marking as solved

Seriously impressive