# specific coloring for bar plots

**URL:** <https://talk.observablehq.com/t/specific-coloring-for-bar-plots/5996>\
**Category:** Help\
**Created:** [December 23, 2021, 5:04am UTC](https://talk.observablehq.com/t/specific-coloring-for-bar-plots/5996 "2021-12-23T05:04:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoffreyCodes](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/joffreycodes/32/5930_2.png) [@JoffreyCodes](https://talk.observablehq.com/u/JoffreyCodes)\
**Post date:** [December 23, 2021, 5:04am UTC](https://talk.observablehq.com/t/specific-coloring-for-bar-plots/5996/1 "2021-12-23T05:04:43Z")

</div>

I’m having issues specifying a color for a labeled bar in a bar plot. Below in the attached photo , I’m able to alter the color of a specific name, but the color does not match what I want. How may I do this such that I can assign a specific label with a color of my choosing in a bar plot?

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/observablehq/original/2X/e/e593b2b682c6e564da1e062476ad5616a2a9135b.png)

---

<div class="post-metadata">

**Author:** ![tophtucker](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/tophtucker/32/4781_2.png) [@tophtucker](https://talk.observablehq.com/u/tophtucker)\
**Post date:** [December 23, 2021, 6:54am UTC](https://talk.observablehq.com/t/specific-coloring-for-bar-plots/5996/2 "2021-12-23T06:54:25Z")

</div>

This confused me too at first! Unlike D3, Plot treats the return value of the function `(d) => (d.stuffyName === "Piggy" ? "red" : "black")` as a part of the domain, not of the range. It treats the strings `"red"` and `"black"` as _categories_, which are then mapped to the default categorical color scale. To get around this, you can add `color: {type: "identity"}`. Examples:

> **[Surprising colors in Plot](https://observablehq.com/@tophtucker/surprising-colors-in-plot)**
>
> JoffreyCodes writes in the forum: specific coloring for bar plots  
> \>I’m having issues specifying a color for a labeled bar in a bar plot. Below in the attached photo , I’m able to alter the color of a specific name, but the color does not match...

---

<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:** [January 8, 2022, 1:18am UTC](https://talk.observablehq.com/t/specific-coloring-for-bar-plots/5996/3 "2022-01-08T01:18:46Z")

</div>

Well, I don’t get this at all, but I can at least follow the pattern - 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:** [January 10, 2022, 7:54am UTC](https://talk.observablehq.com/t/specific-coloring-for-bar-plots/5996/4 "2022-01-10T07:54:04Z")

</div>

Here is a suggestion to address this (recurring) issue:

> <https://github.com/observablehq/plot/pull/660#>
>
> if all the categories are valid CSS colors and no range or scheme has been set, …the scale type defaults to identity.
> 
> Ref: https://talk.observablehq.com/t/specific-coloring-for-bar-plots/5996
> 
> Note that with this PR, you can still be surprised by "wrong colors", if any of the channels has an undefined value, the colors will switch back to tableau10. The user needs to use explicit colors \*everywhere\* for it to work.
> 
> I filed a commits with unit tests and another one with the implementation, because I initially had an alternative implementation where a domain comprised only of valid CSS colors would be promoted to the default range of a categorical scale.

Let us know what you think!

---

<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:** [January 18, 2022, 4:46pm UTC](https://talk.observablehq.com/t/specific-coloring-for-bar-plots/5996/5 "2022-01-18T16:46:19Z")

</div>

This has now been addressed by [automatic color identity scale by mbostock · Pull Request #673 · observablehq/plot · GitHub](https://github.com/observablehq/plot/pull/673) which will be available in the next release.

---

<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:** [January 18, 2022, 6:30pm UTC](https://talk.observablehq.com/t/specific-coloring-for-bar-plots/5996/6 "2022-01-18T18:30:49Z")

</div>

Am I correct in thinking that you use it just as before, but that the `color: {type: "identity"}` option is not necessary since the code automatically detects if it should be used?

---

<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:** [January 18, 2022, 6:45pm UTC](https://talk.observablehq.com/t/specific-coloring-for-bar-plots/5996/7 "2022-01-18T18:45:56Z")

</div>

yes that’s the gist of it; it defaults to identity if the category names are all valid css colors
