question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Number of classes for discrete color classes (for quantile or quantize scheme)

See original GitHub issue

Given a simple bar chart with embedded data, for which I like to color encode the values using a quantize type scale (VL-docs):

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "values": [
      {"a": "A", "b": 28},
      {"a": "B", "b": 55},
      {"a": "C", "b": 43},
      {"a": "D", "b": 91},
      {"a": "E", "b": 81},
      {"a": "F", "b": 53},
      {"a": "G", "b": 19},
      {"a": "H", "b": 87},
      {"a": "I", "b": 52}
    ]
  },
  "mark": "bar",
  "encoding": {
    "x": {"field": "a", "type": "nominal", "axis": {"labelAngle": 0}},
    "y": {"field": "b", "type": "quantitative"},
    "color": {
      "field": "b",
      "type": "quantitative",
      "scale": {"type": "quantize", "scheme": "purples"}
    }
  }
}
image

Open the Vega-Lite Chart in the Vega Editor

I observe the default uses 5 classes for scale type quantize, but how to change the number of classes? Let’s say I want to use the color scheme purples to use 3 classes.

At the color scale page, I observe there is a discrete color schemes for purples:

image

But how to use these? Defining purples-3 for scheme does not work.

In Vega it would be possible to define a count as part of the scheme within range:

  {
    "name": "color",
    "type": "quantize",
    "range": {"scheme": "purples", "count": 3},
    "domain": {"data": "values", "field": "b"}
  }
image

Open the Vega Chart in the Vega Editor

As you can see, this successfully use 3 classes for the quantize scale. But how to approach this in Vega-Lite?

Maybe it is a regression and the style of purples-3 worked before? Since I see in this obs-notebook in the selection list of Sequential Multi-Hue Schemes it was defined as such that the suffix indicates the desired number of colors.

In this obs-notebook I observe they use range: d3.schemeBlues[9], but "scheme": "purples[3]" won’t work either.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mattijncommented, May 7, 2022

Thanks! That is what I need indeed! I was close, but still too far…

1reaction
PBI-Davidcommented, May 7, 2022

You need a scheme parameter object . I think this is what you want.

Editor

image

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "values": [
      {"a": "A", "b": 28},
      {"a": "B", "b": 55},
      {"a": "C", "b": 43},
      {"a": "D", "b": 91},
      {"a": "E", "b": 81},
      {"a": "F", "b": 53},
      {"a": "G", "b": 19},
      {"a": "H", "b": 87},
      {"a": "I", "b": 52}
    ]
  },
  "mark": "bar",
  "encoding": {
    "x": {"field": "a", "type": "nominal", "axis": {"labelAngle": 0}},
    "y": {"field": "b", "type": "quantitative"},
    "color": {
      "field": "b",
      "type": "quantitative",
      "scale": {"type": "quantize",  "scheme": {"name": "purples", "count": 3}}
    }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Quantile, Quantize and Threshold scales / D3 | Observable
A discrete color scale groups data points into classes, which help summarize the observations, and build visualizations where colors can be distinguished ...
Read more >
Data classification methods—ArcGIS Pro | Documentation
A quantile classification is well suited to linearly distributed data. Quantile assigns the same number of data values to each class. There are...
Read more >
19. Calculating Quantile Classes | The Nature of Geographic ...
Since our ability to differentiate colors is limited, the more classes you make, the harder they may be to tell apart. In general,...
Read more >
altair.Scale — Altair 4.2.0 documentation
class altair.Scale(align=Undefined, base=Undefined ... Discrete color schemes may be used with discrete or discretizing scales. Continuous color schemes are ...
Read more >
Using scaleQuantile with "viridis" color scheme - Stack Overflow
You have two problems here: The domain in a quantile scale, unlike a quantize scale, is not a range between two values.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found