Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geom.violin with color #1081

Merged
merged 1 commit into from
Jan 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/src/lib/geoms/geom_violin.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Aesthetics used directly:
* `x`: Group categorically on the X-axis
* `y`: Y-axis position.
* `width`: Density at a given `y` value.
* `color` (optional): Violin color. A suitable discrete variable is needed here. See example below.

With the default statistic [Stat.violin](@ref), only the following need be defined:

Expand All @@ -29,5 +30,7 @@ Gadfly.set_default_plot_size(14cm, 8cm)
```

```@example 1
plot(dataset("lattice", "singer"), x="VoicePart", y="Height", Geom.violin)
Dsing = dataset("lattice","singer")
Dsing[:Voice] = [x[1:5] for x in Dsing[:VoicePart]]
plot(Dsing, x=:VoicePart, y=:Height, color=:Voice, Geom.violin)
```
35 changes: 17 additions & 18 deletions src/geom/violin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,24 @@ function render(geom::ViolinGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthetic
Gadfly.assert_aesthetics_equal_length("Geom.violin", aes, :y, :width)

default_aes = Gadfly.Aesthetics()
default_aes.color = PooledDataArray(RGB{Float32}[theme.default_color])
default_aes.x = Float64[0.5]
aes = inherit(aes, default_aes)

n = length(aes.y)

# Group y and width by x
grouped_y = DefaultDict{eltype(aes.x), typeof(aes.y)}(() -> similar(aes.y, 0))
grouped_width = DefaultDict{eltype(aes.x), typeof(aes.width)}(() -> similar(aes.width, 0))
for (x, y, w) in zip(cycle(aes.x), aes.y, aes.width)
push!(grouped_y[x], y)
push!(grouped_width[x], w)
end
default_aes.color = fill(theme.default_color, length(aes.y))
aes = Gadfly.inherit(aes, default_aes)

# Group y, width and color by x
ux = unique(aes.x)
grouped_color = Dict(x => first(aes.color[aes.x.==x]) for x in ux)
grouped_y = Dict(x => aes.y[aes.x.==x] for x in ux)
grouped_width = Dict(x => aes.width[aes.x.==x] for x in ux)

kgy = keys(grouped_y)
violins = [vcat([(x - w/2, y) for (y, w) in zip(grouped_y[x], grouped_width[x])],
reverse!([(x + w/2, y) for (y, w) in zip(grouped_y[x], grouped_width[x])]))
for x in kgy]
colors = [grouped_color[x] for x in kgy]

ctx = context(order=geom.order)
compose!(ctx, Compose.polygon(
[vcat([(x - w/2, y) for (y, w) in zip(grouped_y[x], grouped_width[x])],
reverse!([(x + w/2, y) for (y, w) in zip(grouped_y[x], grouped_width[x])]))
for x in keys(grouped_y)], geom.tag))
compose!(ctx, Compose.polygon(violins, geom.tag), fill(colors))

compose!(ctx, svgclass("geometry"))

compose!(ctx, fill(theme.default_color), svgclass("geometry"))
end
50 changes: 25 additions & 25 deletions src/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1522,32 +1522,32 @@ function apply_statistic(stat::ViolinStatistic,

isa(aes.y[1], Real) || error("Kernel density estimation only works on Real types.")

if aes.x === nothing
y_f64 = collect(Float64, aes.y)
window = stat.n > 1 ? KernelDensity.default_bandwidth(y_f64) : 0.1
f = KernelDensity.kde(y_f64, bandwidth=window, npoints=stat.n)
aes.y = collect(Float64, f.x)
aes.width = f.density
else
grouped_y = DefaultDict{eltype(aes.x), Vector{Float64}}(() -> Float64[])
for (x, y) in zip(cycle(aes.x), aes.y)
push!(grouped_y[x], y)
end

aes.x = Array{Float64}(0)
aes.y = Array{Float64}(0)
aes.width = Array{Float64}(0)

for (x, ys) in grouped_y
window = stat.n > 1 ? KernelDensity.default_bandwidth(ys) : 0.1
f = KernelDensity.kde(ys, bandwidth=window, npoints=stat.n)
append!(aes.y, f.x)
append!(aes.width, f.density)
for _ in 1:length(f.x)
push!(aes.x, x)
end
end
grouped_y = Dict(1=>aes.y)
grouped_color = Dict{Int, Gadfly.ColorOrNothing}(1=>nothing)
ux = unique(aes.x)
uxflag = length(ux) < length(aes.x)
colorflag = aes.color != nothing

uxflag && (grouped_y = Dict(x=>aes.y[aes.x.==x] for x in ux))

grouped_color = (colorflag ? Dict(x=>first(aes.color[aes.x.==x]) for x in ux) :
uxflag && Dict(x=>nothing for x in ux) )

aes.x = Array{Float64}(0)
aes.y = Array{Float64}(0)
aes.width = Array{Float64}(0)
colors = eltype(aes.color)[]

for (x, ys) in grouped_y
window = stat.n > 1 ? KernelDensity.default_bandwidth(ys) : 0.1
f = KernelDensity.kde(ys, bandwidth=window, npoints=stat.n)
append!(aes.x, fill(x, length(f.x)))
append!(aes.y, f.x)
append!(aes.width, f.density)
append!(colors, fill(grouped_color[x], length(f.x)))
end

colorflag && (aes.color = colors)

pad = 0.1
maxwidth = maximum(aes.width)
Expand Down
2 changes: 1 addition & 1 deletion test/testscripts/violin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ using Gadfly, DataArrays, RDatasets

set_default_plot_size(6inch, 3inch)

plot(dataset("lattice", "singer"), x=:VoicePart, y=:Height, Geom.violin)
plot(dataset("lattice", "singer"), x=:VoicePart, y=:Height, color=:VoicePart, Geom.violin)