Hi everyone, I am appreciating observable plot. It’s really helped me understand data visualization with web technologies.
I have a question about showing value labels on a histogram
I have used a group transform in observable plot to create a histogram. The code below generates it, great!
const histFmCalc = Plot.plot({
y: {
label: "Innovation Count"
},
x: {
label: "Years in development"
},
marks: [
Plot.barY(
innovations,
Plot.groupX(
{y: "count"},
{
x: "DEVELOPMENT_TIME",
fill: "goldenrod"
}
)
)
]
});
I want the count for each bar to show above it. I have managed to do this with Plot.text
, but I’ve had to basically recreate the grouping function manually like so:
const innoHist = innovations.reduce((acc, curr) => {
const newAcc = acc;
if(newAcc[curr.DEVELOPMENT_TIME]) {
newAcc[curr.DEVELOPMENT_TIME]++
} else {
newAcc[curr.DEVELOPMENT_TIME] = 1
}
return newAcc;
},
[]);
const innoHistIndex = innoHist.map((value, index, array) => index);
And then use these new arrays in my plot (see last line):
const histFmCalc = Plot.plot({
y: {
label: "Innovation Count"
},
x: {
label: "Years in development"
},
marks: [
Plot.barY(
innovations,
Plot.groupX(
{y: "count"},
{
x: "DEVELOPMENT_TIME",
fill: "goldenrod"
},
)
),
Plot.text(innoHist, {x: innoHistIndex, y: innoHist, dy: -5})
]
});
I feel like there should be an easier and more efficient way to do this, is there?
I am including a screenshot of my histogram because I’m very proud of it.