# Support for CategoricalValue in StatsPlots

**URL:** https://discourse.julialang.org/t/support-for-categoricalvalue-in-statsplots/47523
**Category:** Visualization
**Tags:** statsplots
**Created:** [September 30, 2020, 5:05am UTC](https://discourse.julialang.org/t/support-for-categoricalvalue-in-statsplots/47523 "2020-09-30T05:05:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![skorpio11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skorpio11/32/16093_2.png) [@skorpio11](https://discourse.julialang.org/u/skorpio11)
#### Post date: [September 30, 2020, 5:05am UTC](https://discourse.julialang.org/t/support-for-categoricalvalue-in-statsplots/47523/1 "2020-09-30T05:05:57Z")

</div>

Hi I am following the walkthrough for StatsPlots at:

[https://github.com/JuliaPlots/StatsPlots.jl](https://github.com/JuliaPlots/StatsPlots.jl)

Using Julia Version 1.4.1

Got into problems when trying the violin plot:

```julia
singers = RDatasets.dataset("lattice", "singer");
@df singers violin(:VoicePart,:Height, side=:right, marker=(0.2, :blue, stroke(0)), label="Scala")

```

Got the following error:  
Cannot convert CategoricalValue{String,UInt8} to series data for plotting

Stacktrace:  
[1] error(::String) at ./error.jl:33  
[2] \_prepare\_series\_data(::CategoricalValue{String,UInt8}) at /home/leon/.julia/packages/RecipesPipeline/tkFmN/src/series.jl:8  
[3] \_series\_data\_vector(::CategoricalValue{String,UInt8}, ::Dict{Symbol,Any}) at /home/leon/.julia/packages/RecipesPipeline/tkFmN/src/series.jl:27  
[4] (::RecipesPipeline.var"#45#48"{Dict{Symbol,Any}})(::CategoricalValue{String,UInt8}) at ./none:0  
[5] iterate(::Base.Generator{CategoricalArray{String,1,UInt8,String,CategoricalValue{String,UInt8},Union{}},RecipesPipeline.var"#45#48"{Dict{Symbol,Any}}}) at ./generator.jl:47  
[6] \_series\_data\_vector(::CategoricalArray{String,1,UInt8,String,CategoricalValue{String,UInt8},Union{}}, ::Dict{Symbol,Any}) at /home/leon/.julia/packages/RecipesPipeline/tkFmN/src/series.jl:42  
[7] macro expansion at /home/leon/.julia/packages/RecipesPipeline/tkFmN/src/series.jl:138 [inlined]  
[8] apply\_recipe(::Dict{Symbol,Any}, ::Type{RecipesPipeline.SliceIt}, ::CategoricalArray{String,1,UInt8,String,CategoricalValue{String,UInt8},Union{}}, ::Array{Int32,1}, ::Nothing) at /home/leon/.julia/packages/RecipesBase/aQmWx/src/RecipesBase.jl:281  
[9] \_process\_userrecipes!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{CategoricalArray{String,1,UInt8,String,CategoricalValue{String,UInt8},Union{}},Array{Int32,1}}) at /home/leon/.julia/packages/RecipesPipeline/tkFmN/src/user\_recipe.jl:35  
[10] recipe\_pipeline!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{CategoricalArray{String,1,UInt8,String,CategoricalValue{String,UInt8},Union{}},Array{Int32,1}}) at /home/leon/.julia/packages/RecipesPipeline/tkFmN/src/RecipesPipeline.jl:69  
[11] \_plot!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{CategoricalArray{String,1,UInt8,String,CategoricalValue{String,UInt8},Union{}},Array{Int32,1}}) at /home/leon/.julia/packages/Plots/M1wcx/src/plot.jl:167  
[12] plot(::CategoricalArray{String,1,UInt8,String,CategoricalValue{String,UInt8},Union{}}, ::Vararg{Any,N} where N; kw::Base.Iterators.Pairs{Symbol,Any,NTuple{4,Symbol},NamedTuple{(:side, :marker, :label, :seriestype),Tuple{Symbol,Tuple{Float64,Symbol,Plots.Stroke},String,Symbol}}}) at /home/leon/.julia/packages/Plots/M1wcx/src/plot.jl:57  
[13] violin(::CategoricalArray{String,1,UInt8,String,CategoricalValue{String,UInt8},Union{}}, ::Vararg{Any,N} where N; kw::Base.Iterators.Pairs{Symbol,Any,Tuple{Symbol,Symbol,Symbol},NamedTuple{(:side, :marker, :label),Tuple{Symbol,Tuple{Float64,Symbol,Plots.Stroke},String}}}) at /home/leon/.julia/packages/RecipesBase/aQmWx/src/RecipesBase.jl:402  
[14] add\_label(::Array{String,1}, ::Function, ::CategoricalArray{String,1,UInt8,String,CategoricalValue{String,UInt8},Union{}}, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Symbol,Any,Tuple{Symbol,Symbol,Symbol},NamedTuple{(:side, :marker, :label),Tuple{Symbol,Tuple{Float64,Symbol,Plots.Stroke},String}}}) at /home/leon/.julia/packages/StatsPlots/6bINV/src/df.jl:153  
[15] (::var"#57#58")(::DataFrame) at /home/leon/.julia/packages/Plots/M1wcx/src/components.jl:0  
[16] top-level scope at In[58]: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: [November 22, 2020, 7:28pm UTC](https://discourse.julialang.org/t/support-for-categoricalvalue-in-statsplots/47523/2 "2020-11-22T19:28:34Z")

</div>

Hi - did you figure this out? I’m having the same problem. One can convert the categorical array to a string with `string.(column)` but then I lose my custom ordering. Thanks!

---

<div class="post-metadata">

### Author: ![skorpio11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skorpio11/32/16093_2.png) [@skorpio11](https://discourse.julialang.org/u/skorpio11)
#### Post date: [November 23, 2020, 2:14am UTC](https://discourse.julialang.org/t/support-for-categoricalvalue-in-statsplots/47523/3 "2020-11-23T02:14:23Z")

</div>

Sadly no. Waiting until Julia matures a bit more.

---

<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: [November 23, 2020, 4:25am UTC](https://discourse.julialang.org/t/support-for-categoricalvalue-in-statsplots/47523/4 "2020-11-23T04:25:00Z")

</div>

I had totally forgotten that I had asked a similar question over two years ago. See [Ordering the y-axis values in a dotplot](https://discourse.julialang.org/t/ordering-the-y-axis-values-in-a-dotplot/10642) . Here is a workaround…

Assuming the `:VoicePart` is the categorical array, then you can do…

```julia
cticks(ctg::CategoricalArray) = (1:length(levels(ctg)), levels(ctg))
df singers violin(levelcode.(:VoicePart),:Height, xticks=cticks(:VoicePart), side=:right, marker=(0.2, :blue, stroke(0)), label="Scala")

```

A bit ugly, but it works. Basically, the x value is the integer code for the the level of that entry and then the `xticks` is a translation from that code to the string level description.

Would be nice if `CategoricalArrays` just worked for plotting. There’s an open issue about this at [https://github.com/JuliaData/CategoricalArrays.jl/issues/256](https://github.com/JuliaData/CategoricalArrays.jl/issues/256) .
