Plot, facet by month and year

Hi,

I want to facet time series data by Year on the Y axis. I can get the plot showing by the domain of the X axis is the entire ~10 years, and not just the months.

I have tried to manipulate the date field, since the data for each facet is sliced by a Year column, the year for the plot doesn’t matter much:

Plot.line(sum, {
  x: (d) => new Date(2021, d.Month.getMonth(), d.Month.getDay()),
  y: "sum",
  stroke: "Year",
  curve: "step"
}).plot({
  marks: setRuleMarks(0, -100, 100),
  facet: {
    data: sum,
    y: "Year",
    marginRight: 50
  },
  y: {
    ticks: 3
  },
  width,
  height: 700,
  marginLeft: 90
})

It looks OK, apart from the little 2021 label at the bottom left.

But in general, is this the way to do it?

1 Like

Here are two examples

2 Likes

Alright, cool. Thanks.

Do you happen to know how to add spacing between the facet plots?

yes, this would be the fy: {padding: 0.1} option.

1 Like

Thanks, about the only thing I did not try :slight_smile:

Hi @Fil,

I saw you updated this excellent example. Why isn’t it visible in the Plot Gallery? I just realized there are a bunch of useful notebooks hidden in the plot examples collection.

Also, I seem to be doing something wrong when applying your approach to Plot.rectY:

Below the first cell, I’ve put my workaround (which works, but ignores the Feb 29 issue).

nice! I think you can simplify a bit:
x: (d) => new Date(d.date.getTime()).setUTCFullYear(2023),

1 Like

Thanks!

I’m not sure I understand why the tickFormat fails with rects, though.

I think it’s because that scale transform returns a number not a date, and the tickFormat string is for dates