# Ordering the y-axis values in a dotplot

**URL:** <https://discourse.julialang.org/t/ordering-the-y-axis-values-in-a-dotplot/10642>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [May 1, 2018, 7:56pm UTC](https://discourse.julialang.org/t/ordering-the-y-axis-values-in-a-dotplot/10642 "2018-05-01T19:56:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyonsquark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyonsquark/32/4483_2.png) [@lyonsquark](https://discourse.julialang.org/u/lyonsquark)\
**Post date:** [May 1, 2018, 7:56pm UTC](https://discourse.julialang.org/t/ordering-the-y-axis-values-in-a-dotplot/10642/1 "2018-05-01T19:56:32Z")

</div>

Hi, I’m trying to make a “dotplot”, e.g. see [here](https://www.statmethods.net/graphs/images/dotplot2.png), with `Plots.jl`. It mostly works, but I would like to order the categorical values that are shown on the y-axis.

Here is what I have so far,

![newplot%20(9)](https://global.discourse-cdn.com/julialang/original/3X/1/7/17412fbec0f434cb8ee4ebcb0a2bf7de6880171d.png)

It is produced by

```julia
@df d2d scatter(:when, :volume, xrotation=30, markersize=4, markeralpha=0.01, legend=nothing, group=:mover)

```

`:volume` is a categorical column. I have sorted the levels, …

```julia
> levels(d2d[:volume])
14-element Array{String,1}:
 "VPD430(GM2)"
 "VPD434(GM2)"
 "VPD435(GM2)"
 "VPD436(GM2)"
 "VPD440(GM2)"
 "VPD441(GM2)"
 "VPD442(GM2)"
...

```

But the plot does not honor this sorting. I even sorted the whole `DataFrame` by `:volume` and that didn’t change the ordering of the values on the y-axis. Does anyone know how to order the categorical values on the y-axis? Thanks! – Adam

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [May 1, 2018, 8:51pm UTC](https://discourse.julialang.org/t/ordering-the-y-axis-values-in-a-dotplot/10642/2 "2018-05-01T20:51:03Z")

</div>

An easy way to do it would be to replace :volume with a number specifying the y location, then override `yticks` to show the names, e.g. `scatter(:when, :numvolume, ticks = (1:14, levels(:volume))`. But agreed it would make sense if StatPlots would respect the level-order of ordered categorical columns.

---

<div class="post-metadata">

**Author:** ![lyonsquark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyonsquark/32/4483_2.png) [@lyonsquark](https://discourse.julialang.org/u/lyonsquark)\
**Post date:** [May 1, 2018, 9:30pm UTC](https://discourse.julialang.org/t/ordering-the-y-axis-values-in-a-dotplot/10642/3 "2018-05-01T21:30:45Z")

</div>

Thanks! Along what you suggested, this works nicely (though is very verbose)…

```julia
@df d2d scatter(:when, d2d[:volume].refs, ticks=(1:length(levels(:volume)), levels(:volume)), xrotation=30, markersize=4, markeralpha=0.01, group=:mover)

```

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [May 1, 2018, 11:03pm UTC](https://discourse.julialang.org/t/ordering-the-y-axis-values-in-a-dotplot/10642/4 "2018-05-01T23:03:34Z")

</div>

I wonder if it is an issue with categorical arrays or with `@df` (see discussion [here](https://github.com/JuliaPlots/Plots.jl/issues/1364)).

Does

```julia
scatter(d2d[:when], d2d[:volume])

```

produce the correct order?

---

<div class="post-metadata">

**Author:** ![lyonsquark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyonsquark/32/4483_2.png) [@lyonsquark](https://discourse.julialang.org/u/lyonsquark)\
**Post date:** [May 2, 2018, 12:56am UTC](https://discourse.julialang.org/t/ordering-the-y-axis-values-in-a-dotplot/10642/5 "2018-05-02T00:56:13Z")

</div>

Doesn’t look good…

```julia
> scatter(d2d[:when], d2d[:volume])
No user recipe defined for CategoricalArrays.CategoricalString{UInt32}

```

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [May 2, 2018, 9:51am UTC](https://discourse.julialang.org/t/ordering-the-y-axis-values-in-a-dotplot/10642/6 "2018-05-02T09:51:50Z")

</div>

Looks like a bug, ideally we should support any `AbstractArray`, could you open an issue at Plots.jl repository?

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [May 2, 2018, 10:19am UTC](https://discourse.julialang.org/t/ordering-the-y-axis-values-in-a-dotplot/10642/7 "2018-05-02T10:19:58Z")

</div>

```julia
julia> CategoricalArrays.CategoricalString{UInt32} <: AbstractArray
false

```

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [May 2, 2018, 10:27am UTC](https://discourse.julialang.org/t/ordering-the-y-axis-values-in-a-dotplot/10642/8 "2018-05-02T10:27:50Z")

</div>

Ups, my bad, so the problem is not supporting the container type but supporting categorical values. In particular seems like we should allow categorical values in Plots using their own ordering. Not sure why the `@df` macro would work at all then, will investigate.

EDIT: actually at least in my hands it does not work:

```julia
julia> @df df scatter(:x, :x)
ERROR: No user recipe defined for CategoricalArrays.CategoricalValue{Int64,UInt32}

```

I wonder if this is something the `@df` macro should take care of or if it’s actually simpler to fix it in Plots (may be as simple as widening some method signatures to accept categorical values).

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [May 2, 2018, 10:35am UTC](https://discourse.julialang.org/t/ordering-the-y-axis-values-in-a-dotplot/10642/9 "2018-05-02T10:35:46Z")

</div>

Would be best to fix in Plots IMHO. Maybe just having a type recipe on them? I guess the ordering means they need some special treatment. Essentially they should be treated more like symbols/strings in the code I guess.

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [May 2, 2018, 11:05am UTC](https://discourse.julialang.org/t/ordering-the-y-axis-values-in-a-dotplot/10642/10 "2018-05-02T11:05:37Z")

</div>

Yes, I think they should be treated like string and comparing with `isless` or sorting with `sort` will automatically give the correct order.
