Color in vega map visualization

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "description": "A choropleth map depicting U.S. unemployment rates by county in 2009.",
  "width": 960,
  "height": 500,
  "autosize": "none",

  "data": [
    {
      "name": "unemp",
      "url": "data/population_engineers_hurricanes.csv",
            "format": {"type": "csv", "parse": "auto"}

    },
    {
      "name": "states",
      "url": "data/us-10m.json",
      "format": {"type": "topojson", "feature": "states"},
      "transform": [
        { "type": "lookup", "from": "unemp", "key": "id", "fields": ["id"], "values": ["population"] },
        { "type": "filter", "expr": "datum.population != null" },
         {
  "type": "joinaggregate",
  "fields": ["population"],
  "groupby": ["id"],
  "ops": ["sum"],
  "as": ["a_count"]
}
      ]
    }
  ],

  "projections": [
    {
      "name": "projection",
      "type": "albersUsa"
    }
  ],

  "scales": [
    {
      "name": "color",
      "type": "quantize",
      
      "range": {"scheme": "blues"}
    }
  ],

  "legends": [
    {
      "fill": "color",
      "orient": "bottom-right",
      "title": "Population",
      "format": "0.0f"
    }
  ],

  "marks": [
    {
      "type": "shape",
      "from": {"data": "states"},
      "encode": {
        "enter": { "tooltip": {"signal": "format(datum.a_count, '0.0f')"}},
        "update": { "fill": {"scale": "color", "field": "a_count"} },
        "hover": { "fill": {"value": "red"} }
      },
      "transform": [
        { "type": "geoshape", "projection": "projection" }
      ]
    }
  ]
}

Output :

I wanted it to apply color as per sum of population for each state, but it is applying same color for all the states. May I know how I have color differentiation based on a_count field ?

Easy way is just to do a test on the value and make it the color you want based on the test.

"fill": [
  {
    "test": "datum.a_count > 2000",
    "value": "red"
  },
  {
    "test": "datum.a_count > 10000",
    "value": "blue"
  },
  {"value": "grey"}
]

Hi @aaron-nimocks

I took this example as reference, in which color coding according through number was working fine. Not sure why its not working for the above one.

  "scales": [
    {
      "name": "color",
      "type": "quantize",
      "domain": [0, 0.15],
      "range": {"scheme": "blues", "count": 7}
    }

The above example uses scales to pick colors using a range and color scheme.

You can use the same technique if you wanted but you won't get to pick colors. It uses a scheme so will do the shades of blue for you.

yes, have done the same thing in above, just didn't add domain in scales. I have added that and checked, it's not working.

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "description": "A choropleth map depicting U.S. unemployment rates by county in 2009.",
  "width": 960,
  "height": 500,
  "autosize": "none",

  "data": [
    {
      "name": "unemp",
      "url": "data/population_engineers_hurricanes.csv",
            "format": {"type": "csv", "parse": "auto"}

    },
    {
      "name": "states",
      "url": "data/us-10m.json",
      "format": {"type": "topojson", "feature": "states"},
      "transform": [
        { "type": "lookup", "from": "unemp", "key": "id", "fields": ["id"], "values": ["population"] },
        { "type": "filter", "expr": "datum.population != null" },
         {
  "type": "joinaggregate",
  "fields": ["population"],
  "groupby": ["id"],
  "ops": ["sum"],
  "as": ["a_count"]
}
      ]
    }
  ],

  "projections": [
    {
      "name": "projection",
      "type": "albersUsa"
    }
  ],

  "scales": [
    {
      "name": "color",
      "type": "quantize",
      "domain": [0, 0.15],
      "range": {"scheme": "blues", "count": 7}
    }
  ],
  
  "legends": [
    {
      "fill": "color",
      "orient": "bottom-right",
      "title": "Population",
      "format": "0.0f"
    }
  ],

  "marks": [
    {
      "type": "shape",
      "from": {"data": "states"},
      "encode": {
        "enter": { "tooltip": {"signal": "format(datum.a_count, '0.0f')"}},
        "update": { "fill": {"scale": "color", "field": "a_count"} },
        "hover": { "fill": {"value": "red"} }
      },
      "transform": [
        { "type": "geoshape", "projection": "projection" }
      ]
    }
  ]
}

Play with the below to get what you want.

      "domain": [0, 10000000],
      "range": {"scheme": "blues", "count": 7}

So domain is 0 to enter the max number you want. Then that will divide into 7 colors in the blues scheme.

Change the 10000000 to the max number you think will be generated. Then change the 7 to increase/decrease the categories.

Got it, thanks @aaron-nimocks

1 Like