# Plot : formatting numerical values computed by a transform in text mark

**URL:** <https://talk.observablehq.com/t/plot-formatting-numerical-values-computed-by-a-transform-in-text-mark/5828>\
**Category:** Help\
**Created:** [November 16, 2021, 1:43pm UTC](https://talk.observablehq.com/t/plot-formatting-numerical-values-computed-by-a-transform-in-text-mark/5828 "2021-11-16T13:43:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![juba](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/juba/32/3250_2.png) [@juba](https://talk.observablehq.com/u/juba)\
**Post date:** [November 16, 2021, 1:43pm UTC](https://talk.observablehq.com/t/plot-formatting-numerical-values-computed-by-a-transform-in-text-mark/5828/1 "2021-11-16T13:43:37Z")

</div>

Starting from an example from the Group transform notebook, I’m trying to add numeric values to a faceted stacked bar chart. This works great, but if I want to show proportions intead of count, I don’t know how I could format them to be displayed, say, as percentages. As the values are computed by a transform, I don’t know how to apply formatting to them.

Here is the plot code.

```auto

Plot.plot({
  y: {
    grid: true,
    percent: true
  },
  marks: [
    Plot.barY(penguins, Plot.groupX({y: "proportion"}, {x: "species", fill: "sex"})),
    Plot.textY(penguins, Plot.stackY(Plot.groupX({y: "proportion", text: "proportion"}, {x: "species", z: "sex"}))),
    Plot.ruleY([0])
  ]
})

```

Also available in this notebook : [Untitled / juba / Observable](https://observablehq.com/d/e7802ddfe73f927e)

Thanks !

---

<div class="post-metadata">

**Author:** ![mcmcclur](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mcmcclur/32/2364_2.png) [@mcmcclur](https://talk.observablehq.com/u/mcmcclur)\
**Post date:** [November 16, 2021, 3:03pm UTC](https://talk.observablehq.com/t/plot-formatting-numerical-values-computed-by-a-transform-in-text-mark/5828/2 "2021-11-16T15:03:44Z")

</div>

Each channel (such as `x`, `y`, `fill` or, most importantly, `text`) can be specified by a function. Strings (such as `"proportion"`) simply point to some commonly used function for convenience. It’s usually not too hard to roll your own, though.

Try this for the `text` channel:

```
text: (d) => d3.format("0.2%")(d.length / penguins.length)

```

Here’s the result:

https://observablehq.com/embed/249623e44d6f6dc2?cells=chart
* * *

Having said that, it’s just a matter of time before @Fil points out an easier, built in approach. 🙂

---

<div class="post-metadata">

**Author:** ![juba](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/juba/32/3250_2.png) [@juba](https://talk.observablehq.com/u/juba)\
**Post date:** [November 16, 2021, 3:23pm UTC](https://talk.observablehq.com/t/plot-formatting-numerical-values-computed-by-a-transform-in-text-mark/5828/3 "2021-11-16T15:23:24Z")

</div>

So that means that when in a Group transform, the function is applied to the data from the current group. I didn’t know that, it can be really useful, thanks !

---

<div class="post-metadata">

**Author:** ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)\
**Post date:** [November 16, 2021, 3:23pm UTC](https://talk.observablehq.com/t/plot-formatting-numerical-values-computed-by-a-transform-in-text-mark/5828/4 "2021-11-16T15:23:33Z")

</div>

You could use Plot.map 🙂

```auto
format = d3.format(".1%")

```

```auto
Plot.textY(
      penguins,
      Plot.map({ text: (data) => data.map(format) },
        Plot.stackY(
          Plot.groupX(
            { y: "proportion", text: "proportion" },
            { x: "species", z: "sex" }
          )
        )
      )
    )

```

---

<div class="post-metadata">

**Author:** ![mcmcclur](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mcmcclur/32/2364_2.png) [@mcmcclur](https://talk.observablehq.com/u/mcmcclur)\
**Post date:** [November 16, 2021, 3:28pm UTC](https://talk.observablehq.com/t/plot-formatting-numerical-values-computed-by-a-transform-in-text-mark/5828/5 "2021-11-16T15:28:23Z")

</div>

> [@juba](#):
>
> So that means that when in a Group transform, the function is applied to the data from the current group.

Perhaps, but I’m not sure that’s always the case. I think it can be tricky to figure out exactly what that argument is when using some sort of transform. To figure it out, I used the following intermediate step:

```
text: (d) => {console.log(d); return "temp"}

```

At that point, the labels were all `temp` but I could check the console to see the value of `d`. Then, it was pretty easy to write the function.

---

<div class="post-metadata">

**Author:** ![juba](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/juba/32/3250_2.png) [@juba](https://talk.observablehq.com/u/juba)\
**Post date:** [November 16, 2021, 3:33pm UTC](https://talk.observablehq.com/t/plot-formatting-numerical-values-computed-by-a-transform-in-text-mark/5828/6 "2021-11-16T15:33:06Z")

</div>

Great, now I’ve got a solution _and_ I better understand what `map` can be useful for. Composing transforms is very powerful, but I’m not completely used to this yet (and it is an approach which seems quite different from, say, `ggplot2` in R).

Thanks !
