Deston
Deston

Reputation: 97

How to assign custom colors to bar mark in Vega-Lite

I have a layered chart, combining 2 stack bar charts, a normal bar chart and a line chart. I am attempting to assign bespoke color codes to the stacked bar charts using scales and domain declarations; however, I am not getting the intended results

current results

Firstly, I am not able to assign the different colors to the stacked bars, despite attempting to use different nominal categories.

Secondly, the I would like to display Bikes Sales, Car Sales AND Bike Sales LY, Car Sales LY within the legend.

Here's where I am at the moment:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "values": [
      {
        "Month": 1,
        "Category": "Bikes",
        "Sales": 100,
        "Sales LY": 90,
        "Target": 95,
        "Volume": 300
      },
      {
        "Month": 1,
        "Category": "Cars",
        "Sales": 560,
        "Sales LY": 800,
        "Target": 880,
        "Volume": 1200
      },
      {
        "Month": 2,
        "Category": "Bikes",
        "Sales": 130,
        "Sales LY": 170,
        "Target": 190,
        "Volume": 450
      },
      {
        "Month": 2,
        "Category": "Cars",
        "Sales": 600,
        "Sales LY": 665,
        "Target": 680,
        "Volume": 1200
      },
      {
        "Month": 3,
        "Category": "Bikes",
        "Sales": 200,
        "Sales LY": 150,
        "Target": 165,
        "Volume": 400
      },
      {
        "Month": 3,
        "Category": "Cars",
        "Sales": 750,
        "Sales LY": 1000,
        "Target": 1100,
        "Volume": 1200
      },
      {
        "Month": 4,
        "Category": "Bikes",
        "Sales": 80,
        "Sales LY": 105,
        "Target": 120,
        "Volume": 500
      },
      {
        "Month": 4,
        "Category": "Cars",
        "Sales": 800,
        "Sales LY": 600,
        "Target": 660,
        "Volume": 1200
      }
    ]
  },
    "transform": [
      {"calculate": "'Volume'", "as": "Volume Legend"},        
      {"calculate": "'Target'", "as": "Target Legend"}  ,      
      {"calculate": "datum.Category", "as": "Category2"}             
      ] ,
  "height": 300,
  "width": 300,  
  "layer": [
    {
      "name": "SALES",
      "mark": {"type": "bar", "xOffset": 0, "size": 10},
      "encoding": {
        "x": {"field": "Month", "type": "ordinal", "axis": {"labelAngle": 0}},
        "y": {"field": "Sales", "type": "quantitative"},
          "color": {
        "field": "Category",             
          "type" : "nominal",          
            "scale": 
          {"domain": [ "Bikes","Cars"], "range": ["#4CD964", "#39A34B"]}
    }
      }
    },
    {
      "name": "SALES LY",
      "mark": {"type": "bar", "xOffset": -11, "size": 10},
      "encoding": {
        "x": {"field": "Month", "type": "ordinal", "axis": {"labelAngle": 0}},
        "y": {"field": "Sales LY", "type": "quantitative", "axis": null},
          "color": {
        "field": "Category2",             
          "type" : "nominal",          
            "scale": 
          {"domain": [ "Bikes","Cars"], "range": [ "#5AC8FA", "#4496BC" ]}
        }
      }
    },
    {
      "name": "TARGET",
      "mark": {"type": "bar", "xOffset": 11, "size": 10},
      "encoding": {
        "x": {"field": "Month", "type": "ordinal", "axis": {"labelAngle": 0}},
        "y": {"aggregate": "sum", "field": "Target", "type": "quantitative", "axis": null},
         "color": {        
          "field": "Target Legend",          
          "scale": { "range": ["#FFCC00" ]},
          "legend": {"title": ""}
      }}
    },
    {
      "name": "VOLUME",
      "mark": {"type": "line"},
      "encoding": {
        "x": {"field": "Month", "type": "ordinal"},
        "y": {"aggregate": "sum", "field": "Volume", "type": "quantitative"},
        "stroke": {
          "field": "Volume Legend",
          "scale": {"range": ["green"]},
          "legend": {"title": ""}
        }
      }
    }
  ],
  "resolve": {"scale": {"y": "independent"}}
}

Any guidance on how to achieve these 2 outcomes would be greatly appreciated!

Upvotes: 1

Views: 339

Answers (1)

kikon
kikon

Reputation: 8665

You're almost there. You should only append the string ' LY' to the values of Category2 when you define it to the datum:

{
    "calculate": "datum.Category + ' LY'",
    "as": "Category2"
}

And also define the bar color scale once (in the same place) for all four colors:

"color": {
    "field": "Category",
    "type" : "nominal",
    "scale": {
        "domain": ["Bikes", "Cars", "Bikes LY", "Cars LY"],
        "range": ["#4CD964", "#39A34B", "#5AC8FA", "#4496BC"]
   }
}

vega editor link

Upvotes: 1

Related Questions