Altair: How to properly use `facet` and `resolve_scale` together?

Created on 22 Nov 2019  路  5Comments  路  Source: altair-viz/altair

Hi I am trying to use resolve_scale() and facet() in the same chart but either don't get the scale resolved or keep getting an error saying:

Javascript Error: Unrecognized scale name: "child_layer_0_y"

This usually means there's a typo in your chart specification. See the JavaScript console for the full traceback.

Here is the code:

from vega_datasets import data

cars = data.cars()

chart_base = alt.Chart(cars.sample(20, random_state=42))

chart_mpg = chart_base.encode(
    x='Name:N',
    y=alt.Y('Miles_per_Gallon:Q', scale=alt.Scale(type='linear'))
)

chart_weight = chart_base.transform_filter(
    alt.datum.Weight_in_lbs > 0  
).encode(
    x='Name:N',
    y=alt.Y('Weight_in_lbs:Q', scale=alt.Scale(base=10, type='log')),
    color=alt.value('red')
)

# this errors out
display((chart_mpg.mark_bar() + chart_weight.mark_line()).resolve_scale(y='independent').facet(column='Origin'))

# this works but the scale is not resolved
display((chart_mpg.mark_bar() + chart_weight.mark_line()).facet(column='Origin').resolve_scale(y='independent'))

And here is the working but wrong chart (the scale is not resolved, Miles_per_Gallon ends up on the same scale as Weight_in_lbs):
visualization

  • May I know how to resolve this issue? Thanks~
bug vega-lite-related

Most helpful comment

Agree that faceted multi-axis charts would be nice 馃憤

All 5 comments

Unfortunately, I don't think resolve_scale can be used at this level of granularity within Vega-Lite. It would be worth a bug report / feature request in http://github.com/vega/vega-lite

Here's a compact Vega-Lite spec based on your example that demonstrates the problem (vega editor):

{
  "data": {"url": "data/cars.json"},
  "transform": [{"sample": 20}],
  "facet": {"type": "nominal", "field": "Origin"},
  "spec": {
    "encoding": {"x": {"type": "nominal", "field": "Name"}},
    "layer": [
      {
        "mark": "bar",
        "encoding": {"y": {"type": "quantitative", "field": "Miles_per_Gallon"}}
      },
      {
        "mark": "line",
        "encoding": {"y": {"type": "quantitative", "field": "Weight_in_lbs"}}
      }
    ],
    "resolve": {"scale": {"y": "independent"}}
  }
}

@jakevdp Thank you very much for pointing out the right direction. I did a little research on the vega-lite git repo and found out that a very similar bug was reported by you about a year ago and it seems that it is still open. From what I read in the threads, the faceted dual axis charts have not been supported in vega-lite, not sure if it will be supported in the future though. The workaround proposed by you using hconcat and/or vconcat with filter works for me as well (vega editor), only that it seems a bit laborious compared with the declarative intent of facet.

Agree that faceted multi-axis charts would be nice 馃憤

This thread on facets might be helpful. I used it to solve this related issue w/ facets.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

breadbaron picture breadbaron  路  4Comments

tonylee3399 picture tonylee3399  路  3Comments

Juan-132 picture Juan-132  路  3Comments

maxgerma picture maxgerma  路  3Comments

firasm picture firasm  路  3Comments