Drop empty facets

When using facets, how do I drop empty combinations?

Screenshot 2024-04-08 at 14.07.45

An example code is included here:

Plot.plot({
  marginLeft: 80,
  marginRight: 80,
  color: {legend: true},
  marks: [
    Plot.barX(penguins, 
      Plot.groupY({
        x: "count"
       }, {
         fy: "species",
         y: "island",
         fill: "sex"
       })),
    Plot.frame()
  ]
})

Here is the link to a working document: Facets and missing combinations / workbook | Observable

Update: This is the intended result I am looking for:

Screenshot 2024-04-08 at 14.07.45

Can you make a drawing of what you have in mind? (Do you want to remove the labels, or to move the bars?)

Updated to show the intended result.

Thanks! Yes that could be desirable, but it’s not possible with Plot (yet). You can do an approximation with quite some work:

html`${d3
  .groups(penguins, (d) => d.species)
  .map(([species, penguins], i) =>
    Plot.barX(
      penguins,
      Plot.groupY(
        { x: "count" },
        { fill: "sex", y: "island", order: ["FEMALE", "MALE", null] }
      )
    ).plot({
      marginLeft: 90,
      marginTop: 0,
      marginRight: 50,
      marginTop: 10 * (i > 0),
      marginBottom: 30 * (i === 2),
      //style: "border: 1px solid red",
      height:
        d3.groupSort(
          penguins,
          (v) => null,
          (d) => d.island
        ).length *
          14 +
        20 +
        30 * (i == 2),
      x: { domain: [0, 130], axis: i === 2 },
      y: { label: i == 0 ? undefined : null },
      color: { domain: ["FEMALE", "MALE", null] },
      marks: [
        Plot.text([species], { frameAnchor: "right", textAnchor: "start" })
        // , Plot.frame({stroke: "green")
      ]
    })
  )}`

Thanks for the effort. Even for this dummy dataset, it’s so complex. I cannot see myself figuring this out for a larger dataset with x and y facets. I will skip this for now and wait for a proper solution. Thanks again.

Yes I’m sorry I don’t have a simpler answer. I’m not even sure how we could make this easier!