# Can you vary scales across facets in Plot.plot?

**URL:** https://talk.observablehq.com/t/can-you-vary-scales-across-facets-in-plot-plot/6070
**Category:** Help
**Created:** [January 13, 2022, 8:44pm UTC](https://talk.observablehq.com/t/can-you-vary-scales-across-facets-in-plot-plot/6070 "2022-01-13T20:44:24Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![cbwheadon](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/cbwheadon/32/6004_2.png) [@cbwheadon](https://talk.observablehq.com/u/cbwheadon)
#### Post date: [January 13, 2022, 8:44pm UTC](https://talk.observablehq.com/t/can-you-vary-scales-across-facets-in-plot-plot/6070/1 "2022-01-13T20:44:24Z")

</div>

I’m using facets in Plot.plot and wondered if there is an equivalent to setting scales to ‘free’ in R ggplot2? I’d like the scales to vary across rows rather than be shared across all facets. I’ve not been able to find an example.

---

<div class="post-metadata">

### Author: ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)
#### Post date: [January 13, 2022, 9:07pm UTC](https://talk.observablehq.com/t/can-you-vary-scales-across-facets-in-plot-plot/6070/2 "2022-01-13T21:07:45Z")

</div>

Not really, even though it’s often possible to find ways to go around the limitation (by normalizing on max, for an example see [Plot scatter plot matrix / Fil / Observable](https://observablehq.com/@fil/plot-splom)).

Please feel free to open an issue, or share a notebook with a specific application.

---

<div class="post-metadata">

### Author: ![RL31](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@RL31](https://talk.observablehq.com/u/RL31)
#### Post date: [January 18, 2023, 9:18pm UTC](https://talk.observablehq.com/t/can-you-vary-scales-across-facets-in-plot-plot/6070/3 "2023-01-18T21:18:54Z")

</div>

Hi, i’m also trying to adapt your normalizeMaxY function to a “common” bar plot but without success so far. Do you have any advice / hint ?  
Here you can find a reproductible example : [Facet bar plot / Rémi L | Observable (observablehq.com)](https://observablehq.com/d/d38cdc07f7f078cd)  
Thanks

---

<div class="post-metadata">

### Author: ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)
#### Post date: [January 18, 2023, 10:15pm UTC](https://talk.observablehq.com/t/can-you-vary-scales-across-facets-in-plot-plot/6070/4 "2023-01-18T22:15:47Z")

</div>

try this:

```plaintext
// common bar plot, with n
Plot.plot({
  x: {
    label: "n"
  },
  facet: {
    data: data, 
    x: "facet"
  },
  marks:[
    Plot.barX(data, Plot.normalizeX({
      x: "x",
      y: "y"
    }))
  ]
})

```

---

<div class="post-metadata">

### Author: ![urswilke](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/urswilke/32/7780_2.png) [@urswilke](https://talk.observablehq.com/u/urswilke)
#### Post date: [May 29, 2023, 12:04pm UTC](https://talk.observablehq.com/t/can-you-vary-scales-across-facets-in-plot-plot/6070/5 "2023-05-29T12:04:26Z")

</div>

I would be interested, how to facet on one category, **without** showing the levels of the empty sub-categories on the axis in the facets. An example, where the empty sub-categories are shown is [here](https://observablehq.com/@observablehq/plot-annotated-facets).

EDIT: Or as an alternative do something like [this](https://observablehq.com/@observablehq/plot-multiscale-date-axis) with the categories and their sub-categories.

---

<div class="post-metadata">

### Author: ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)
#### Post date: [June 25, 2023, 9:38am UTC](https://talk.observablehq.com/t/can-you-vary-scales-across-facets-in-plot-plot/6070/6 "2023-06-25T09:38:50Z")

</div>

@urswilke If I understand your question correctly, the answer is currently to create a distinct chart for each category. Currently in Plot all scales are shared between facets (but please vote up [Facet with varying scales? · Issue #8 · observablehq/plot · GitHub](https://github.com/observablehq/plot/issues/8) — concrete examples are also welcome).

---

<div class="post-metadata">

### Author: ![nachocab](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/nachocab/32/2426_2.png) [@nachocab](https://talk.observablehq.com/u/nachocab)
#### Post date: [September 8, 2023, 11:13am UTC](https://talk.observablehq.com/t/can-you-vary-scales-across-facets-in-plot-plot/6070/7 "2023-09-08T11:13:39Z")

</div>

I thought I could use @Maya’s [approach to free scale](https://observablehq.com/@mayagans/from-ggplot2-to-plot) in `Plot.rectY`, but [I get an error](https://observablehq.com/d/6c9736232251bd5b): “transforms cannot be applied after initializers”.

---

<div class="post-metadata">

### Author: ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)
#### Post date: [September 9, 2023, 2:49pm UTC](https://talk.observablehq.com/t/can-you-vary-scales-across-facets-in-plot-plot/6070/8 "2023-09-09T14:49:13Z")

</div>

@nachocab I’ve sent you a suggestion to fix the error. The issue stems from Plot.barY implicitly applying the stack transform when it is called with the y channel. If you call stackY explicitly instead, and before the custom initializer, it will work. The barY mark then receives fully formed y1 and y2 channels, and does not try to add stacking. The initializer needs to be adapted to work with the y1 and y2 channels instead of the y channel (in that case, only y2 is useful).

---

<div class="post-metadata">

### Author: ![nachocab](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/nachocab/32/2426_2.png) [@nachocab](https://talk.observablehq.com/u/nachocab)
#### Post date: [September 9, 2023, 2:55pm UTC](https://talk.observablehq.com/t/can-you-vary-scales-across-facets-in-plot-plot/6070/9 "2023-09-09T14:55:54Z")

</div>

Thanks, Fil! Is there a similar fix to get scalefreeX? That’s actually what I was going for. 😅

---

<div class="post-metadata">

### Author: ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)
#### Post date: [September 9, 2023, 3:27pm UTC](https://talk.observablehq.com/t/can-you-vary-scales-across-facets-in-plot-plot/6070/10 "2023-09-09T15:27:09Z")

</div>

I see; it’s certainly possible:

```auto
// adapted from https://observablehq.com/@mayagans/from-ggplot2-to-plot
scalefreeX = (options) =>
  Plot.initializer(options, (data, facets, channels, scales, dimensions) => {
    const {
      x1: { value: X1 },
      x2: { value: X2 }
    } = channels;
    for (const index of facets) {
      const x = d3
        .scaleUtc(d3.extent(Array.from(index, (i) => [X1[i], X2[i]]).flat()), [
          dimensions.marginLeft,
          dimensions.width - dimensions.marginRight
        ]);
      for (const i of index) (X1[i] = x(X1[i]), X2[i] = x(X2[i]));
    }
    return { data, facets, channels: { x1: { value: X1 }, x2: { value: X2 } } };
  })

```

However the _x_ axis is now completely useless.

Instead, I’d recommend to do work with a relative date:

```auto
      x: d => d3.utcDay.offset(new Date("1970-01-01"), d3.utcDay.count(d3.utcYear(d.date), d.date)),

```

or, more simply:

```auto
      x: d => +d.date - +d3.utcYear(d.date),

```

And then fix the axis’ tick format:

```auto
  x: { tickFormat: "%b", ticks: 12 },

```

(Expect a slight mis-alignment of days to months if you have a bisextile year—you should then use d3.utcMonth to floor the value before removing the year)

---

<div class="post-metadata">

### Author: ![nachocab](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/nachocab/32/2426_2.png) [@nachocab](https://talk.observablehq.com/u/nachocab)
#### Post date: [September 9, 2023, 3:47pm UTC](https://talk.observablehq.com/t/can-you-vary-scales-across-facets-in-plot-plot/6070/11 "2023-09-09T15:47:01Z")

</div>

Thanks for the help! I’ll study all your suggestions to understand how initializers actually work. 😅

---

<div class="post-metadata">

### Author: ![urswilke](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/urswilke/32/7780_2.png) [@urswilke](https://talk.observablehq.com/u/urswilke)
#### Post date: [January 26, 2024, 10:20am UTC](https://talk.observablehq.com/t/can-you-vary-scales-across-facets-in-plot-plot/6070/12 "2024-01-26T10:20:27Z")

</div>

I found a hack to plot multi-scale axis for categorical data (see [here](https://observablehq.com/@urswilke/plot-multiscale-categorical-axis)).

But if there is a solution using facets I would very much prefer this, because in this hack I would also need to adapt `tickSize` for labels of different width for the `Plot.barX` plot.

---

<div class="post-metadata">

### Author: ![nachocab](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/nachocab/32/2426_2.png) [@nachocab](https://talk.observablehq.com/u/nachocab)
#### Post date: [August 17, 2024, 4:01pm UTC](https://talk.observablehq.com/t/can-you-vary-scales-across-facets-in-plot-plot/6070/13 "2024-08-17T16:01:44Z")

</div>

> [@Fil](#):
>
> Please feel free to open an issue, or share a notebook with a specific application.

For anyone who would like to see this vote on this [Github Issue](https://github.com/observablehq/plot/issues/8):

> <https://github.com/observablehq/plot/issues/8>
>
> Currently faceting repeats scales. What if you want a scale that varies by facet…, as in a SPLOM?
