# Color in vega map visualization

**URL:** <https://discuss.elastic.co/t/color-in-vega-map-visualization/263565>\
**Category:** Kibana\
**Tags:** vega\
**Created:** [February 8, 2021, 9:25am UTC](https://discuss.elastic.co/t/color-in-vega-map-visualization/263565 "2021-02-08T09:25:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pranusha119](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@Pranusha119](https://discuss.elastic.co/u/Pranusha119)\
**Post date:** [February 8, 2021, 9:25am UTC](https://discuss.elastic.co/t/color-in-vega-map-visualization/263565/1 "2021-02-08T09:25:34Z")

</div>

```auto
{
  "$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 :

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/4/543d5a083e2ed1cc09d727297da8ba178b22b0e4.png)

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 ?

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [February 8, 2021, 2:42pm UTC](https://discuss.elastic.co/t/color-in-vega-map-visualization/263565/2 "2021-02-08T14:42:08Z")

</div>

Easy way is just to do a [test on the value](https://vega.github.io/vega/docs/marks/#production-rules) and make it the color you want based on the test.

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

```

---

<div class="post-metadata">

**Author:** ![Pranusha119](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@Pranusha119](https://discuss.elastic.co/u/Pranusha119)\
**Post date:** [February 8, 2021, 4:36pm UTC](https://discuss.elastic.co/t/color-in-vega-map-visualization/263565/3 "2021-02-08T16:36:33Z")

</div>

Hi @aaron-nimocks

> **[County Unemployment Example](https://vega.github.io/vega/examples/county-unemployment/)**
>
> Vega - A Visualization Grammar. Vega is a visualization grammar, a declarative format for creating, saving, and sharing interactive visualization designs. With Vega, you can describe the visual appearance and interactive behavior of a visualization...

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.

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [February 8, 2021, 4:49pm UTC](https://discuss.elastic.co/t/color-in-vega-map-visualization/263565/4 "2021-02-08T16:49:09Z")

</div>

```auto
  "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.

---

<div class="post-metadata">

**Author:** ![Pranusha119](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@Pranusha119](https://discuss.elastic.co/u/Pranusha119)\
**Post date:** [February 8, 2021, 4:54pm UTC](https://discuss.elastic.co/t/color-in-vega-map-visualization/263565/5 "2021-02-08T16:54:48Z")

</div>

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.

```auto
{
  "$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" }
      ]
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [February 8, 2021, 5:10pm UTC](https://discuss.elastic.co/t/color-in-vega-map-visualization/263565/6 "2021-02-08T17:10:57Z")

</div>

Play with the below to get what you want.

```auto
      "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.

---

<div class="post-metadata">

**Author:** ![Pranusha119](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@Pranusha119](https://discuss.elastic.co/u/Pranusha119)\
**Post date:** [February 9, 2021, 5:44pm UTC](https://discuss.elastic.co/t/color-in-vega-map-visualization/263565/7 "2021-02-09T17:44:28Z")

</div>

Got it, thanks @aaron-nimocks

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 9, 2021, 5:45pm UTC](https://discuss.elastic.co/t/color-in-vega-map-visualization/263565/8 "2021-03-09T17:45:02Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
