When using facets, how do I drop empty combinations?
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:
Fil
April 8, 2024, 1:05pm
2
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.
Fil
April 8, 2024, 3:41pm
4
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.
Fil
April 10, 2024, 1:31pm
6
Yes I’m sorry I don’t have a simpler answer. I’m not even sure how we could make this easier!