How to get mean value within data of a facet? #1104
-
I am building a faceted strip plot chart in which I would like to display a red rule for the mean value within the facet. I found In the example below I've used The code (published at https://observablehq.com/d/4a0159d26906f08d) looks like this: Plot.plot({
grid: true,
inset: 10,
height: 400,
width: 800,
facet: {
data: ib_tr,
x: "fraktion"
},
y: {
nice: true
},
fx: {
domain: ['G', 'S', 'GL', 'M-E', 'RL', 'V'],
},
marks: [
Plot.ruleY([0]),
Plot.ruleY(ib_tr, Plot.selectMinY({y: "rating", stroke: "red"})),
Plot.dot(ib_tr, Plot.dodgeX({
y: "rating",
r: 4,
title: d =>`${d.parlamentarier}\nMandate: ${d.anzahl}`,
href: d => `https://lobbywatch.ch/de/daten/parlamentarier/${d.parlamentarier_id}`,
target: '_blank',
fill: "fraktion",
anchor: "middle",
id: d => d.parlamentarier_id
}))
],
color: {
legend: false,
type: "categorical",
domain: ['G', 'S', 'GL', 'M-E', 'RL', 'V'],
range: ["#85B50C", "#E4022D", "#A0A000", "#FF9100", "#3A8BC1", "#0A7228"]
},
}) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
I think I found a solution or at least a workaround based on the Plot.ruleY(ib_tr, Plot.select({
y: (I, V) => {
const median = d3.mean(I, i => V[i]);
const i = d3.least(I, i => Math.abs(V[i] - median));
return [i];
}
}, {y: "rating", stroke: "red"})), But AFAICT this will give me the data point that is nearest the mean value, which wasn't exactly what I wanted, but I will use it as a workaround. |
Beta Was this translation helpful? Give feedback.
-
You can use the groupZ transform: Plot.ruleY(ib_tr, Plot.groupZ({y: "median"}, {y: "rating", stroke: "red"})) |
Beta Was this translation helpful? Give feedback.
You can use the groupZ transform: