dagroj
dagroj

Reputation: 35

Vega Lite Independent Scale with Multiple Layers and Facet

Is it possible to have an independent scale for each facet and each layer? The resolve works great when you have either a facet or an extra layer, but I cannot get it to do both, wondering if it is even possible.

What I want is: The two scales on each side

mixed with the faceting here

Upvotes: 3

Views: 1970

Answers (1)

jakevdp
jakevdp

Reputation: 86310

The way this would be expressed in Vega-Lite is using a layer, with resolve set, within a facet. Something like this:

{
  "data": {
    "url": "https://vega.github.io/vega-datasets/data/seattle-weather.csv"
  },
  "facet": {
    "column": {
      "field": "weather",
      "type": "nominal"
    }
  },
  "spec": {
    "layer": [
      {
        "encoding": {
          "x": {
            "field": "date",
            "timeUnit": "month",
            "type": "temporal"
          },
          "y": {
            "aggregate": "mean",
            "field": "temp_max",
            "type": "quantitative"
          }
        },
        "mark": {
          "color": "salmon",
          "type": "line"
        }
      },
      {
        "encoding": {
          "x": {
            "field": "date",
            "timeUnit": "month",
            "type": "temporal"
          },
          "y": {
            "aggregate": "mean",
            "field": "precipitation",
            "type": "quantitative"
          }
        },
        "mark": {
          "color": "steelblue",
          "type": "line"
        }
      }
    ],
    "resolve": {
      "scale": {
        "y": "independent"
      }
    }
  }
}

While this spec is valid according to the Vega-Lite schema, there is unfortunately a bug in the vega-lite renderer that makes it unable to render this spec.

As a workaround, you can manually concatenate two layered charts with a filter transform that selects the desired subset of data for each. For example:

{
  "data": {
    "url": "https://vega.github.io/vega-datasets/data/seattle-weather.csv"
  },
  "hconcat": [
    {
      "layer": [
        {
          "mark": {"type": "line", "color": "salmon"},
          "encoding": {
            "x": {"type": "temporal", "field": "date", "timeUnit": "month"},
            "y": {
              "type": "quantitative",
              "aggregate": "mean",
              "field": "temp_max"
            }
          }
        },
        {
          "mark": {"type": "line", "color": "steelblue"},
          "encoding": {
            "x": {"type": "temporal", "field": "date", "timeUnit": "month"},
            "y": {
              "type": "quantitative",
              "aggregate": "mean",
              "field": "precipitation"
            }
          }
        }
      ],
      "resolve": {"scale": {"y": "independent", "x": "shared"}},
      "transform": [{"filter": "(datum.weather === 'sun')"}]
    },
    {
      "layer": [
        {
          "mark": {"type": "line", "color": "salmon"},
          "encoding": {
            "x": {"type": "temporal", "field": "date", "timeUnit": "month"},
            "y": {
              "type": "quantitative",
              "aggregate": "mean",
              "field": "temp_max"
            }
          }
        },
        {
          "mark": {"type": "line", "color": "steelblue"},
          "encoding": {
            "x": {"type": "temporal", "field": "date", "timeUnit": "month"},
            "y": {
              "type": "quantitative",
              "aggregate": "mean",
              "field": "precipitation"
            }
          }
        }
      ],
      "resolve": {"scale": {"y": "independent", "x": "shared"}},
      "transform": [{"filter": "(datum.weather === 'fog')"}]
    }
  ],
  "$schema": "https://vega.github.io/schema/vega-lite/v2.6.0.json"
}

enter image description here

Upvotes: 3

Related Questions