# Handle numerical values as categories in plots

**URL:** <https://discourse.julialang.org/t/handle-numerical-values-as-categories-in-plots/41256>\
**Category:** Visualization\
**Tags:** plots, statsplots\
**Created:** [June 12, 2020, 9:43am UTC](https://discourse.julialang.org/t/handle-numerical-values-as-categories-in-plots/41256 "2020-06-12T09:43:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [June 12, 2020, 9:43am UTC](https://discourse.julialang.org/t/handle-numerical-values-as-categories-in-plots/41256/1 "2020-06-12T09:43:07Z")

</div>

I am trying to plot boxplots of values with respect to 4 categories represented as numbers. The four categories are powers of two: 16384, 32768, 65536, 131072.

Let’s use this MWE dataframe:

```julia
using DataFrames, StatsPlots

df= DataFrame(cat = [16384, 32768, 65536, 131072, 16384, 32768, 65536, 131072, 16384, 32768, 65536, 131072], val = rand(12))

```

There are two possibilities, first I can plot without any change  
`boxplot(df.cat, df.val)`  
But as you can see on this image the boxes are not evenly spaced but the categories are numbers

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/2/62c1aa45c994e13cd62312fd728ad27ebbdc3d35.png)

But if I transform into strings I get nice boxes, but then the values are sorted alphabetically and not numerically.  
`boxplot(string.(df.cat), df.val)`

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/f/bf683bdaba9ada0e0291ebfceb3abdfdc6ed87f0.png)

I can’t find a way to make order this correctly. Anyone knows ?

---

<div class="post-metadata">

**Author:** ![josephmarturano](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@josephmarturano](https://discourse.julialang.org/u/josephmarturano)\
**Post date:** [June 17, 2020, 3:37pm UTC](https://discourse.julialang.org/t/handle-numerical-values-as-categories-in-plots/41256/2 "2020-06-17T15:37:14Z")

</div>

I think you are looking for the `xdiscrete_values` keyword, e.g.:

```julia
using DataFrames, StatsPlots
df= DataFrame(cat = [16384, 32768, 65536, 131072, 16384, 32768, 65536, 131072, 16384, 32768, 65536, 131072], val = rand(12))
boxplot(string.(df.cat), df.val, xdiscrete_values=string.(df.cat))

```

---

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [June 17, 2020, 3:40pm UTC](https://discourse.julialang.org/t/handle-numerical-values-as-categories-in-plots/41256/3 "2020-06-17T15:40:07Z")

</div>

YES that’s exactly what I needed thank you ! There’s a lot of keywords and aliases in the package, it’s hard to find what you need in the docs sometimes.

---

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [June 17, 2020, 3:43pm UTC](https://discourse.julialang.org/t/handle-numerical-values-as-categories-in-plots/41256/4 "2020-06-17T15:43:33Z")

</div>

Where did you find it by the way ? I don’t see it in the attributes section of the documentation.

---

<div class="post-metadata">

**Author:** ![josephmarturano](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@josephmarturano](https://discourse.julialang.org/u/josephmarturano)\
**Post date:** [June 17, 2020, 4:57pm UTC](https://discourse.julialang.org/t/handle-numerical-values-as-categories-in-plots/41256/5 "2020-06-17T16:57:58Z")

</div>

Yes I can sympathize with that sentiment; some of the `Plots.plot()` arguments could be better documented.

The `xdiscrete_values` keyword argument is at the bottom of the [gr() backend documentation page](http://docs.juliaplots.org/latest/generated/gr/). While this page does not describe how each argument affects the plot, I use it as a starting point if I need to explore new functionality.
