Plot: Facets - filter facet frames and values

I have created a plot similar to this example where I am using time (in this case “year” as the facet. I also have a select filter defined that filters by “site”. The filter is working but I get both “year” values in the faceted by “year” frames?

Here’s the code I have so far

Plot.plot({
  marginLeft: 110,
  height: 500,
  grid: true,
  x: {
    nice: true
  },
  y: {
    domain: d3.groupSort(barley, g => -d3.median(g, d => d.yield), d => d.variety),
    inset: 5
  },
  color: {
    type: "categorical"
  },
  facet: {
    data: barley,
    x: "year",
    marginRight: 90
  },
  marks: [
    Plot.frame(),
    Plot.barX(barley.filter(d => d.site === barley_stat), {x: "yield", y: "variety", fill: "year"})
  ]
})

Hi there. I just took a crack at your other question. Reading your code here, I see that you are familiar familiar w/ d3.group, so apologies if I was off the mark on that one.

For this question, I am not sure that I entirely follow what your asking / noting with “filter is working but I get both “year” values in the faceted by “year” frames?”. Your code looks like this is the desired outcome: you show two views of your data, one for each year in the dataset. When you filter by site, these views update: again, showing the specifics for each year. Could you please describe a bit more what you’d like to do differently?

Also - if it’s possible for you to share your notebook, this would help with troubleshooting/problem-solving.

Cheers!

It’s the filter that is causing the problem.

If there are inline, like this

  facet: {
    data: barley.filter(d => d.site === 'Grand Rapids'),
    x: "year",
  },
  marks: [
    Plot.frame(),
    Plot.barX(barley.filter(d => d.site === 'Grand Rapids'),

It doesn’t work.

but if the data is filtered in its own cell then it works.

  },
  facet: {
    data: barle,
    x: "year",
  },
  marks: [
    Plot.frame(),
    Plot.barX(barle,

Not sure why that is as they both represent the same array of data.

1 Like

Here is a link to the notebook - test_facet / mferro64 / Observable

1 Like

I provided the notebook link down below. Each year represented by the year facet should only have the counts for the respective year - not both

1 Like

Did you read this, or look at the link?

I did look at your remarks - it does work without the filter - but I have a select input to choose the city so the facet will show the respective year and varieties just for that city…

Look at the notebook and see how I put the filter in a different cell and referenced that cell for the plot

The test for “auto” faceting is checking if the data in the facet is strictly equal to the data in the mark. In this case, both data are derived in the same way, but they are distinct objects, and thus no “auto” faceting happens. You can force the mark to facet by specifying facet: true. Or, by using the same derived object (which is what happens when the data.filter(…) is computed once and given a name that you then use in both places).

2 Likes

That worked - thanks for your help