vega-lite icon indicating copy to clipboard operation
vega-lite copied to clipboard

Cannot sort faceted boxplots when extent is "min-max"

Open jwoLondon opened this issue 5 years ago • 2 comments

Boxplots can be nicely sorted by some aggregation function with faceting:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
  "config": {
    "view": {"stroke": ""},
    "header": {"labelAngle": 0},
    "facet": {"spacing": 0}
  },
  "data": {
    "values": [
      {"x": 3, "y": "b"},
      {"x": 4, "y": "b"},
      {"x": 5, "y": "b"},
      {"x": 1, "y": "a"},
      {"x": 2, "y": "a"},
      {"x": 3, "y": "a"},
      {"x": 2, "y": "c"},
      {"x": 3, "y": "c"},
      {"x": 4, "y": "c"}
    ]
  },
  "encoding": {
    "row": {
      "field": "y",
      "type": "nominal",
      "sort": {"field": "x", "op": "median"}
    },
    "x": {"field": "x", "type": "quantitative"},
    "color": {"field": "y", "type": "nominal"}
  },
  "mark": {"type": "boxplot", "extent": 1}
}

But if extent is set to "min-max", sorting no longer works and boxplots are displayed in original row order (and not even in the field's natural order). Perhaps this bug is related to the cause of #4140?

jwoLondon avatar Apr 26 '19 11:04 jwoLondon

{
  "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
  "data": {
    "values": [
      {"x": 3, "y": "b"},
      {"x": 4, "y": "b"},
      {"x": 1, "y": "a"},
      {"x": 2, "y": "a"},
      {"x": 2, "y": "c"},
      {"x": 3, "y": "c"}
    ]
  },
  "facet": {
    "row": {
      "field": "y",
      "type": "nominal",
      "sort": {"field": "x", "op": "median"}
    }
  },
  "spec": {
    "transform": [
      {
        "aggregate": [{"op": "mean", "field": "x", "as": "mean_x"}],
        "groupby": []
      }
    ],
    "mark": "point",
    "encoding": {"x": {"field": "mean_x", "type": "quantitative"}}
  }
}

also produces this output: visualization

Without the transform, the output is produced correctly.

chanwutk avatar May 15 '19 08:05 chanwutk

It appears that the issue isn't due to the min-max extent. I'm getting the same issue if I set outliers and rule to false, rather than setting the extent. (If I only set either one of the two, it sorts correctly)

{
  "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
  "config": {
    "view": {"stroke": ""},
    "header": {"labelAngle": 0},
    "facet": {"spacing": 0}
  },
  "data": {
    "values": [
      {"x": 3, "y": "b"},
      {"x": 4, "y": "b"},
      {"x": 5, "y": "b"},
      {"x": 1, "y": "a"},
      {"x": 2, "y": "a"},
      {"x": 3, "y": "a"},
      {"x": 2, "y": "c"},
      {"x": 3, "y": "c"},
      {"x": 4, "y": "c"}
    ]
  },
  "encoding": {
    "row": {
      "field": "y",
      "type": "nominal",
      "sort": {"field": "x", "op": "median"}
    },
    "x": {"field": "x", "type": "quantitative"},
    "color": {"field": "y", "type": "nominal"}
  },
  "mark": {"type": "boxplot", "outliers": false, "rule": false}
}

hannahburkhardt avatar May 11 '22 20:05 hannahburkhardt