# How to set x-axis positions in StatPlots' violin plot?

**URL:** https://discourse.julialang.org/t/how-to-set-x-axis-positions-in-statplots-violin-plot/15421
**Category:** Visualization
**Tags:** dataframes
**Created:** [September 24, 2018, 10:11am UTC](https://discourse.julialang.org/t/how-to-set-x-axis-positions-in-statplots-violin-plot/15421 "2018-09-24T10:11:15Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![rickhg12hs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rickhg12hs/32/5926_2.png) [@rickhg12hs](https://discourse.julialang.org/u/rickhg12hs)
#### Post date: [September 24, 2018, 10:11am UTC](https://discourse.julialang.org/t/how-to-set-x-axis-positions-in-statplots-violin-plot/15421/1 "2018-09-24T10:11:15Z")

</div>

I’d like to create a violin plot of a `DataFrame` and set each “violin” at its linear respective position. For example, this plot:

```julia
# some data to play with
n = 10000
f(k) = randn(k)
df2 = DataFrame(hcat(f(n).+1, f(n).-1, 2*f(n), 0.5*f(n)-3, 2*f(n).^2, abs.(f(n))),
    map(num->Symbol(num),[4,10,15,20,22,30]));
# Now plot a violin of each "Set" on the same graph
@df df2 violin(cols(), xticks=(1:ncol(df2), names(df2)))

```

… has all the “violins” evenly spaced. I’d like to place them according to their numeric column name. I.e., the x-axis would be linear and the first “violin” would be centered above `4`, the next would be centered above `10`, …, and the last “violin” would be centered above `30`.

How can this `violin` plot be created?

---

<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: [September 24, 2018, 12:25pm UTC](https://discourse.julialang.org/t/how-to-set-x-axis-positions-in-statplots-violin-plot/15421/2 "2018-09-24T12:25:06Z")

</div>

As usual, this is an issue with DataFrame formatting. The names of a DataFrame are Symbols and are not intended to be parsed as numbers.

Instead, your DataFrame should have a long format:

```julia
df2 = DataFrame(a = repeat([4,10,15,20,22,30], inner = n), b = vcat(f(n).+1, f(n).-1, 2*f(n), 0.5*f(n).-3, 2*f(n).^2, abs.(f(n))))
@df df2 violin(:a, :b)

```

---

<div class="post-metadata">

### Author: ![rickhg12hs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rickhg12hs/32/5926_2.png) [@rickhg12hs](https://discourse.julialang.org/u/rickhg12hs)
#### Post date: [September 24, 2018, 12:35pm UTC](https://discourse.julialang.org/t/how-to-set-x-axis-positions-in-statplots-violin-plot/15421/3 "2018-09-24T12:35:43Z")

</div>

Nice!

Any way for multicolor and (multitrace?) legend as before?

---

<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: [September 24, 2018, 12:48pm UTC](https://discourse.julialang.org/t/how-to-set-x-axis-positions-in-statplots-violin-plot/15421/4 "2018-09-24T12:48:24Z")

</div>

You can specify the colors as a vector of length 6. Or if you want it automatic like in your previous example, use the `group` keyword: `@df df2 violin(:a, :b, group = :a)`

---

<div class="post-metadata">

### Author: ![rickhg12hs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rickhg12hs/32/5926_2.png) [@rickhg12hs](https://discourse.julialang.org/u/rickhg12hs)
#### Post date: [September 24, 2018, 1:00pm UTC](https://discourse.julialang.org/t/how-to-set-x-axis-positions-in-statplots-violin-plot/15421/5 "2018-09-24T13:00:53Z")

</div>

Super! Thanks!
