vega / vega-lite

A concise grammar of interactive graphics, built on Vega.
https://vega.github.io/vega-lite/
BSD 3-Clause "New" or "Revised" License
4.56k stars 594 forks source link

Can't create a scatter pie chart #6800

Open kanitw opened 3 years ago

kanitw commented 3 years ago
{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",

  "height": 250,
  "data": {"url": "data/cars.json"},
  "mark": "arc",
  "encoding": {
    "x": {"bin": true, "field": "Acceleration",  "type": "quantitative"},
    "color": {"field": "Origin"},
    "angle": {"aggregate": "count", "title": "Number of Cars"}
  }
}
metasoarous commented 3 years ago

Hi @kanitw. Thanks for raising this issue.

It just so happens that I'm trying to do something similar. Less of a scatter plot though, and more of a matrix/table. I did manage to get something working, but it wasn't particularly straightforward.

Here's what I came up with:

{
  "data": {
    "values": [
      {"x": 1, "y": 2, "vote": "agree", "percent": 20, "order": 1},
      {"x": 1, "y": 2, "vote": "disagree", "percent": 30, "order": 2},
      {"x": 1, "y": 2, "vote": "pass", "percent": 50, "order": 3},
      {"x": 3, "y": 4, "vote": "agree", "percent": 30, "order": 4},
      {"x": 3, "y": 4, "vote": "disagree", "percent": 70, "order": 5}
    ]
  },
  "width": 100,
  "height": 100,
  "mark": {"type": "arc"},
  "encoding": {
    "x": {"field": "x", "type": "nominal"},
    "y": {"field": "y", "type": "nominal", "sort": [4, 2]},
    "radius": {"value": 20},
    "radius2": {"value": 8},
    "theta": {
      "field": "percent",
      "type": "quantitative",
      "scale": {"domain": [0, 100]}
    },
    "color": {
      "field": "vote",
      "scale": {
        "domain": ["agree", "disagree", "pass"],
        "range": ["#1b6244", "red", "lightgrey"]
      }
    },
    "order": {"field": "order"}
  }
}

image

The keys to getting this to work were:

I realize that your example is a bit different, as it's doing a bin/aggregation, and clearly has some other problems in terms of the x encoding not functioning properly. However, it's also worth noting that your's didn't seem to have the second of the issues I described, which suggests it was at least able to interpret each pie's theta values separately from the others, even if it didn't put them in the right place.

image

Adding a tooltip to your viz, it seemed like x and y values aren't making it into the aggregated datum values, which is maybe part of the problem.

I tried using quantitative instead of nominal in my plot, and while that worked similarly to my other example, it didn't leave any margin for the marks, as I'd have expected. But I also didn't see the particular set of issues you witnessed.

image

In summary, I don't know how related these problems are or whether we should think of these as separate issues. At the very least though it suggests that a scatter plot is possible if you pre-aggregate the data prior to handing off to vega-lite, and jump through a couple of other hoops to get the arcs right.

I'm likely to need to do this sort of plot a lot in the future, and so am pleased to know that you're thinking about improving support for it. For my purposes, it would be wonderful if vega-lite inferred which arcs belong to which pie based on x & y encodings, avoiding the need for manually applying order and scale domain (could perhaps be a mode if not always desireable).

Thanks again!

metasoarous commented 3 years ago

PS Figured I should also mention that I initially tried using theta and theta2 (as well as thetaOffset) to get around the problems described, but wasn't able to get those properties to behave as expected. However, I went back and confirmed that by adding the scale range, I was able to fix this problem. In my case, I think I'll stick with the order solution since the pre-processing will be a bit easier, but thought I'd throw this out there for posterity.

Thanks again!