Add trim kw arg to violin and density
This commit is contained in:
parent
40cbddab29
commit
910b94c5fd
@ -560,13 +560,18 @@ end
|
|||||||
# otherwise, just use a histogram
|
# otherwise, just use a histogram
|
||||||
if is_installed("KernelDensity")
|
if is_installed("KernelDensity")
|
||||||
@eval import KernelDensity
|
@eval import KernelDensity
|
||||||
@eval function violin_coords(y, bins = 30)
|
@eval function violin_coords(y; trim::Bool=false)
|
||||||
kd = KernelDensity.kde(y, npoints = isa(bins, Integer) ? bins : 30)
|
kd = KernelDensity.kde(y, npoints = 200)
|
||||||
|
if trim
|
||||||
|
xmin, xmax = extrema(y)
|
||||||
|
inside = Bool[ xmin <= x <= xmax for x in kd.x]
|
||||||
|
return(kd.density[inside], kd.x[inside])
|
||||||
|
end
|
||||||
kd.density, kd.x
|
kd.density, kd.x
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
@eval function violin_coords(y, bins = 30)
|
@eval function violin_coords(y; trim::Bool=false)
|
||||||
edges, widths = hist(y, isa(bins, Integer) ? bins : 30)
|
edges, widths = hist(y, 30)
|
||||||
centers = 0.5 * (edges[1:end-1] + edges[2:end])
|
centers = 0.5 * (edges[1:end-1] + edges[2:end])
|
||||||
ymin, ymax = extrema(y)
|
ymin, ymax = extrema(y)
|
||||||
vcat(0.0, widths, 0.0), vcat(ymin, centers, ymax)
|
vcat(0.0, widths, 0.0), vcat(ymin, centers, ymax)
|
||||||
@ -575,7 +580,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
# function apply_series_recipe(d::KW, ::Type{Val{:violin}})
|
# function apply_series_recipe(d::KW, ::Type{Val{:violin}})
|
||||||
@recipe function f(::Type{Val{:violin}}, x, y, z)
|
@recipe function f(::Type{Val{:violin}}, x, y, z; trim=true)
|
||||||
# dumpdict(d, "box before", true)
|
# dumpdict(d, "box before", true)
|
||||||
# TODO: add scatter series with outliers
|
# TODO: add scatter series with outliers
|
||||||
|
|
||||||
@ -587,7 +592,7 @@ end
|
|||||||
|
|
||||||
# get the edges and widths
|
# get the edges and widths
|
||||||
y = d[:y][groupby.groupIds[i]]
|
y = d[:y][groupby.groupIds[i]]
|
||||||
widths, centers = violin_coords(y)
|
widths, centers = violin_coords(y, trim=trim)
|
||||||
|
|
||||||
# normalize
|
# normalize
|
||||||
widths = _box_halfwidth * widths / maximum(widths)
|
widths = _box_halfwidth * widths / maximum(widths)
|
||||||
@ -604,6 +609,9 @@ end
|
|||||||
# n = length(groupby.groupLabels)
|
# n = length(groupby.groupLabels)
|
||||||
# xticks --> (linspace(0.5,n-0.5,n), groupby.groupLabels)
|
# xticks --> (linspace(0.5,n-0.5,n), groupby.groupLabels)
|
||||||
|
|
||||||
|
# clean up d
|
||||||
|
pop!(d, :trim)
|
||||||
|
|
||||||
d[:x], d[:y] = shape_coords(shapes)
|
d[:x], d[:y] = shape_coords(shapes)
|
||||||
()
|
()
|
||||||
|
|
||||||
@ -613,14 +621,18 @@ end
|
|||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# density
|
# density
|
||||||
|
|
||||||
@recipe function f(::Type{Val{:density}}, x, y, z)
|
@recipe function f(::Type{Val{:density}}, x, y, z; trim=false)
|
||||||
newx, newy = violin_coords(y, d[:bins])
|
newx, newy = violin_coords(y, trim=trim)
|
||||||
if isvertical(d)
|
if isvertical(d)
|
||||||
newx, newy = newy, newx
|
newx, newy = newy, newx
|
||||||
end
|
end
|
||||||
x := newx
|
x := newx
|
||||||
y := newy
|
y := newy
|
||||||
seriestype := :path
|
seriestype := :path
|
||||||
|
|
||||||
|
# clean up d
|
||||||
|
pop!(d, :trim)
|
||||||
|
|
||||||
()
|
()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user