# Plot.text on Plot.stackX

**URL:** https://talk.observablehq.com/t/plot-text-on-plot-stackx/8696
**Category:** Help
**Created:** [January 13, 2024, 6:59am UTC](https://talk.observablehq.com/t/plot-text-on-plot-stackx/8696 "2024-01-13T06:59:36Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![GeoffRussell](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/geoffrussell/32/8435_2.png) [@GeoffRussell](https://talk.observablehq.com/u/GeoffRussell)
#### Post date: [January 13, 2024, 6:59am UTC](https://talk.observablehq.com/t/plot-text-on-plot-stackx/8696/1 "2024-01-13T06:59:36Z")

</div>

Not quite sure how to ask this … perhaps just link to a notebook cell?

> **[Tip Calculator](https://observablehq.com/@geoffrussell/tip-calculator#mychart)**
>
> A \*\* %\*\* tip on \*\*$ \*\* is \*\*$ \*\*, for a total of \*\*$ \*\*

I have a bar chart I want to label, but its made with stackX to get bar widths proportional to the X value. I’ve tried all manner of ways of labelling the height of the bar at the top of the bar … but without success.

First day using Observable … !!

Many thanks,

---

<div class="post-metadata">

### Author: ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)
#### Post date: [January 13, 2024, 3:32pm UTC](https://talk.observablehq.com/t/plot-text-on-plot-stackx/8696/2 "2024-01-13T15:32:43Z")

</div>

If I understand correctly, the problem is that the stackX transform implicitly groups by y, treating each unique value of y as a separate stack. In your case you want it all to be treated as a single stack. So you can move the y option out of the stack transform and apply it after, like so:

```js
Plot.text(groupdata20221, {
  ...Plot.stackX({x: "Population"}),
  y: "PerCapPower",
  textAnchor: "middle",
  dy: -5,
  text:(d) => `${d.PerCapPower.toFixed(2)}`
})

```

---

<div class="post-metadata">

### Author: ![GeoffRussell](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/geoffrussell/32/8435_2.png) [@GeoffRussell](https://talk.observablehq.com/u/GeoffRussell)
#### Post date: [January 14, 2024, 11:54pm UTC](https://talk.observablehq.com/t/plot-text-on-plot-stackx/8696/3 "2024-01-14T23:54:01Z")

</div>

I perhaps didn’t get the “…” right, but I did eventually solve the problem with { x: “x”, … not quite sure I understand this but it works!  
see the difference between cells “mychart” and “mychart2”.

------------ Works …  
mychart = Plot.plot({  
y: {label: “Per Capita Power (watts)”},  
x: {label: “Population (million)”},  
marks: [  
Plot.rectY(groupdata20221, Plot.stackX(  
{x: “Population”, y2: “PerCapPower”, fill: “Region”,  
title: (d) =\> `${d.Region} ${d.str}`  
})),  
Plot.text(groupdata20221,{x: “x”,y: “PerCapPower”, textAnchor: “middle”, dy: -5,text:(d) =\> `${d.PerCapPower.toFixed(2)}`}),  
Plot.text([[4000,800]], {textAnchor: “start”,  
text: d=\>[`Total current global power 2022: ${global} GW\n IEA NetZero by 2050 target: ${global *2.6} GW`]})  
]

})

---------------- Syntax error on second stackX  
mychart2 = Plot.plot({  
y: {label: “Per Capita Power (watts)”},  
x: {label: “Population (million)”},  
marks: [  
Plot.rectY(groupdata20221, Plot.stackX(  
{x: “Population”, y2: “PerCapPower”, fill: “Region”,  
title: (d) =\> `${d.Region} ${d.str}`  
})),  
Plot.text(groupdata20221,{  
Plot.stackX({x: “Population”}),  
y: “PerCapPower”,  
textAnchor: “middle”,  
dy: -5,  
text:(d) =\> `${d.PerCapPower.toFixed(2)}`  
}),  
Plot.text([[4000,800]], {textAnchor: “start”,  
text: d=\>[`Total current global power 2022: ${global} GW\n IEA NetZero by 2050 target: ${global *2.6} GW`]})  
]
