# How to order axis with categorical variable in AlgebraOfGraphics?

**URL:** <https://discourse.julialang.org/t/how-to-order-axis-with-categorical-variable-in-algebraofgraphics/98027>\
**Category:** General Usage\
**Tags:** question, forwarddiff, autodiff\
**Created:** [April 28, 2023, 8:30am UTC](https://discourse.julialang.org/t/how-to-order-axis-with-categorical-variable-in-algebraofgraphics/98027 "2023-04-28T08:30:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SebastianCallh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sebastiancallh/32/16102_2.png) [@SebastianCallh](https://discourse.julialang.org/u/SebastianCallh)\
**Post date:** [April 28, 2023, 8:30am UTC](https://discourse.julialang.org/t/how-to-order-axis-with-categorical-variable-in-algebraofgraphics/98027/1 "2023-04-28T08:30:54Z")

</div>

Hello! I’m experimenting with `AlgebraOfGraphics` and stumbled upon something I haven’t been able to do. I looks like the default behaviour when plotting categorical variable on the y axis and continuous on the x axis is to order the y axis in ascending lexical order. Instead, I would like to order the y axis in the ascending order of values on the x axis. Is there a way to do this?

```julia
using CairoMakie, AlgebraOfGraphics, ColorSchemes
let
    df = DataFrame(x = [0.15, 0.13, 0.17], c = ["A", "B", "C"])
    plt = data(df) * mapping(:x, :c, color=:c) * visual(markersize=40)
    draw(
        plt;
        axis=(;limits=(0.1, 0.2, nothing, nothing),
        xticks=collect(0.1:0.02:0.2))
    )
end

```

The goal is for the y axis to be ordered (ascending) B, A, C.

 ![plot_47](https://global.discourse-cdn.com/julialang/original/3X/4/3/435be34a2ea7fecd3665dd1ecd9a575e8912a6a0.png)

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [April 28, 2023, 11:32am UTC](https://discourse.julialang.org/t/how-to-order-axis-with-categorical-variable-in-algebraofgraphics/98027/2 "2023-04-28T11:32:16Z")

</div>

By default a categorical variable is sorted according to its own values, but you can specify an alternative order with `mapping(:c => sorter(...))` :

```julia
using CairoMakie, AlgebraOfGraphics, DataFrames

df = DataFrame(x = [0.15, 0.13, 0.17], c = ["A", "B", "C"])

c_order = sort(df, :x).c

plt = data(df) *
      mapping(:x, :c => sorter(c_order), color=:c) *
      visual(markersize=40)

draw(plt; axis=(; limits=((0.1, 0.2), nothing), xticks=0.1:0.02:0.2))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/5/756f3d5523c87294241f910a136c6f5c0bf58431.png)
