# Plots color based on discrete value

**URL:** https://discourse.julialang.org/t/plots-color-based-on-discrete-value/72472
**Category:** Visualization
**Tags:** plotting, statsplots, pgfplotsx
**Created:** [December 2, 2021, 4:12pm UTC](https://discourse.julialang.org/t/plots-color-based-on-discrete-value/72472 "2021-12-02T16:12:04Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![JianghuiDu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghuidu/32/8028_2.png) [@JianghuiDu](https://discourse.julialang.org/u/JianghuiDu)
#### Post date: [December 2, 2021, 4:12pm UTC](https://discourse.julialang.org/t/plots-color-based-on-discrete-value/72472/1 "2021-12-02T16:12:04Z")

</div>

```nohighlight
data = DataFrame(x=1:5,y=1:5,color=["a","a","b","a","c"])
using StatsPlots
@df data scatter(:x,:y)

```

How can add color to the plot using the discrete value of column `:color` automatically?  
In `ggplot` I can do something like

```julia
# how to add color based on column :color
@df data scatter(:x,:y, color=:color, palettee= :Spectral)

```

Which doesn’t work in Julia.  
I don’t want to assign color values to the column `:color` manually. I just want to select a palette and let the package chose the discrete color values automatically.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [December 3, 2021, 11:33am UTC](https://discourse.julialang.org/t/plots-color-based-on-discrete-value/72472/2 "2021-12-03T11:33:12Z")

</div>

Assuming all your prerequisites are satisfied, would something like this suit you?

 ![Plots_pgfplotsx_pseudo_categorical_colorbar](https://global.discourse-cdn.com/julialang/original/3X/2/9/295479153abc9077f4ae0d608fad01739e66dc84.png)

_(added one additional letter “g”)_

---

<div class="post-metadata">

### Author: ![JianghuiDu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghuidu/32/8028_2.png) [@JianghuiDu](https://discourse.julialang.org/u/JianghuiDu)
#### Post date: [January 17, 2022, 9:02pm UTC](https://discourse.julialang.org/t/plots-color-based-on-discrete-value/72472/3 "2022-01-17T21:02:42Z")

</div>

Yes. That’s what I’m looking for.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [January 17, 2022, 9:20pm UTC](https://discourse.julialang.org/t/plots-color-based-on-discrete-value/72472/4 "2022-01-17T21:20:56Z")

</div>

OK, a long time passed and the code snippet was kept in the fridge and maybe it will have to be reheated. Note the manual adjustment of colorbar label spacing.

```julia
using DataFrames, StatsPlots, LaTeXStrings; pgfplotsx()

df = DataFrame(x=1:6, y=1:6, color=["a","a","b","a","c","g"])
dict = Dict(c => i for (i, c) in pairs(unique(df.color)))
n = length(dict)
@df df scatter(:x, :y, zcolor=get.(Ref(dict),df.color,NaN), color=palette(:roma,n+1), label=false,
    ms=7, clims=(1,n+1), colorbar_tickfontcolor=:white, colorbar_title=join(unique(df.color), L"\ "^15),
)

```

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [January 18, 2022, 10:40am UTC](https://discourse.julialang.org/t/plots-color-based-on-discrete-value/72472/5 "2022-01-18T10:40:38Z")

</div>

If you are familiar with plotting tools in another language and like them, there’s nothing wrong with using them from Julia. The interop story with Python is great, and I just continue plotting with `matplotlib` and `altair` (the latter for stats + interactivity) from Julia as I did before from Python. The syntax is almost always literally the same. If RCall.jl is also similarly seamless - I have no idea - then using `ggplot` directly can be just as feasible.
