Opacity in violinplot with AlgebraOfGraphics

Hi,

I want to combine box plots and violin plots in Algebra of Graphics.
But I cannot set an alpha value in the visual function, when using a violin plot.

MWE:

using CairoMakie
using AlgebraOfGraphics
using DataFrames

num = 1000
df = DataFrame(
    variable = [repeat(["group1"],num); repeat(["group2"], num)], 
    value = [1.8.*randn(num); 4 .+ randn(num)]
)
# WORKS
p = data(df) * mapping(:variable, :value, color=:variable)
# WORKS
p * visual(BoxPlot, alpha = 0.1) |> draw
# WORKS
p * visual(Violin) |> draw
# NOT WORKING
p * visual(Violin, alpha = 0.1) |> draw

Output:

ERROR: MethodError: no method matching color(::Vector{ColorTypes.RGBA{Float32}})
Closest candidates are:
  color(::ColorTypes.Color) at ~/.julia/packages/ColorTypes/1dGw6/src/traits.jl:108
  color(::ColorTypes.TransparentColor{C, T, 2} where T) where C at ~/.julia/packages/ColorTypes/1dGw6/src/traits.jl:109
  color(::ColorTypes.TransparentColor{C, T, 3} where T) where C at ~/.julia/packages/ColorTypes/1dGw6/src/traits.jl:110
  ...
Stacktrace:
  [1] to_color(c::Tuple{Vector{ColorTypes.RGBA{Float32}}, Float64})
    @ Makie ~/.julia/packages/Makie/fJ4OE/src/conversions.jl:772
  [2] (::Makie.var"#829#836"{Vector{Float64}, Tuple{Vector{ColorTypes.RGBA{Float32}}, Float64}, Int64, MakieCore.Automatic, MakieCore.Automatic, MakieCore.Automatic, Tuple{Float64, Float64}})(::Pair{NamedTuple{(:x, :side), Tuple{Float64, Int64}}, Vector{Int64}})
    @ Makie ~/.julia/packages/Makie/fJ4OE/src/stats/violin.jl:82
  [3] iterate
    @ ./generator.jl:47 [inlined]
  [4] grow_to!(dest::Vector{Union{}}, itr::Base.Generator{Base.Generator{StructArrays.GroupPerm{StructArrays.StructVector{NamedTuple{(:x, :side), Tuple{Float64, Int64}}, NamedTuple{(:x, :side), Tuple{Vector{Float64}, Vector{Int64}}}, Int64}, Vector{Int64}, Base.OneTo{Int64}}, StructArrays.var"#95#97"{StructArrays.var"#94#96"{StructArrays.StructVector{NamedTuple{(:x, :side), Tuple{Float64, Int64}}, NamedTuple{(:x, :side), Tuple{Vector{Float64}, Vector{Int64}}}, Int64}, Vector{Int64}}}}, Makie.var"#829#836"{Vector{Float64}, Tuple{Vector{ColorTypes.RGBA{Float32}}, Float64}, Int64, MakieCore.Automatic, MakieCore.Automatic, MakieCore.Automatic, Tuple{Float64, Float64}}})
    @ Base ./array.jl:797
  [5] collect
    @ ./array.jl:721 [inlined]
  [6] map
    @ ./abstractarray.jl:2896 [inlined]
  [7] (::Makie.var"#827#834")(x::Vector{Int64}, y::Vector{Float64}, width::Int64, vside::Symbol, color::Tuple{Vector{ColorTypes.RGBA{Float32}}, Float64}, show_median::Bool, n::Int64, bound::MakieCore.Automatic, bw::MakieCore.Automatic, w::MakieCore.Automatic, limits::Tuple{Float64, Float64}, max_density::MakieCore.Automatic, dodge::MakieCore.Automatic, n_dodge::MakieCore.Automatic, gap::Float64, dodge_gap::Float64, orientation::Symbol)
    @ Makie ~/.julia/packages/Makie/fJ4OE/src/stats/violin.jl:70
  [8] map(::Makie.var"#827#834", ::Observable{Vector{Int64}}, ::Observable{Vector{Float64}}, ::Vararg{Any}; ignore_equal_values::Bool)
    @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:444
  [9] map(::Makie.var"#827#834", ::Observable{Vector{Int64}}, ::Observable{Vector{Float64}}, ::Observable{Any}, ::Vararg{Observable{Any}})
    @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:444
 [10] plot!(plot::Combined{Makie.violin, Tuple{Vector{Int64}, Vector{Float64}}})
    @ Makie ~/.julia/packages/Makie/fJ4OE/src/stats/violin.jl:54
 [11] plot!(scene::Scene, P::Type{Combined{Makie.violin, Tuple{Vector{Int64}, Vector{Float64}}}}, attributes::Attributes, input::Tuple{Observable{Vector{Int64}}, Observable{Vector{Float64}}}, args::Observable{Tuple{Vector{Int64}, Vector{Float64}}})
    @ Makie ~/.julia/packages/Makie/fJ4OE/src/interfaces.jl:402
 [12] plot!(::Scene, ::Type{Combined{Makie.violin}}, ::Attributes, ::Vector{Int64}, ::Vararg{Any}; kw_attributes::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Makie ~/.julia/packages/Makie/fJ4OE/src/interfaces.jl:320
 [13] plot!(::Scene, ::Type{Combined{Makie.violin}}, ::Attributes, ::Vector{Int64}, ::Vector{Float64})
    @ Makie ~/.julia/packages/Makie/fJ4OE/src/interfaces.jl:288
 [14] plot!(::Axis, ::Type{Combined{Makie.violin}}, ::Attributes, ::Vector{Int64}, ::Vararg{Any}; kw_attributes::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Makie ~/.julia/packages/Makie/fJ4OE/src/makielayout/blocks/axis.jl:732
 [15] plot!
    @ ~/.julia/packages/Makie/fJ4OE/src/makielayout/blocks/axis.jl:727 [inlined]
 [16] #plot!#1280
    @ ~/.julia/packages/Makie/fJ4OE/src/makielayout/blocks/axis.jl:744 [inlined]
 [17] plot!(ae::AxisEntries)
    @ AlgebraOfGraphics ~/.julia/packages/AlgebraOfGraphics/AJE5M/src/entries.jl:75
 [18] foreach(f::typeof(plot!), itr::Matrix{AxisEntries})
    @ Base ./abstractarray.jl:2712
 [19] plot!(fig::Figure, s::Layer; axis::NamedTuple{(), Tuple{}}, palettes::NamedTuple{(), Tuple{}})
    @ AlgebraOfGraphics ~/.julia/packages/AlgebraOfGraphics/AJE5M/src/draw.jl:22
 [20] (::AlgebraOfGraphics.var"#242#243"{NamedTuple{(), Tuple{}}, NamedTuple{(), Tuple{}}, NamedTuple{(), Tuple{}}, NamedTuple{(), Tuple{}}, NamedTuple{(), Tuple{}}, Layer})(f::Figure)
    @ AlgebraOfGraphics ~/.julia/packages/AlgebraOfGraphics/AJE5M/src/draw.jl:46
 [21] update
    @ ~/.julia/packages/AlgebraOfGraphics/AJE5M/src/draw.jl:10 [inlined]
 [22] #draw#241
    @ ~/.julia/packages/AlgebraOfGraphics/AJE5M/src/draw.jl:45 [inlined]
 [23] draw
    @ ~/.julia/packages/AlgebraOfGraphics/AJE5M/src/draw.jl:45 [inlined]
 [24] |>(x::Layer, f::typeof(draw))
    @ Base ./operators.jl:966
 [25] top-level scope
    @ ~/pCloudDrive/KU/ProjStat/algebraOfGraphics:22

Hope somebody has a fix.

Version:

Julia Version 1.7.3
Commit 742b9abb4d (2022-05-06 12:58 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Core™ i5-8250U CPU @ 1.60GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-12.0.1 (ORCJIT, skylake)

1 Like

the thing that alpha doesn’t work doesn’t surprise me, since: support `alpha` keyword arg · Issue #84 · JuliaPlots/Makie.jl · GitHub

But it surprises me a little bit that visual(BoxPlot, colormap=(:viridis, 0.1)) doesn’t work.

1 Like

Yes, alpha is implemented in AlgebraOfGraphics and not in Makie, so it is a little bit brittle, especially if you don’t pass color to either visual or mapping. See here for a workaround.

1 Like