From 910b94c5fd95392ddb483678c2b949ef373fd8c1 Mon Sep 17 00:00:00 2001 From: Diego Javier Zea Date: Thu, 9 Jun 2016 02:38:19 -0300 Subject: [PATCH] Add trim kw arg to violin and density --- src/recipes.jl | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/recipes.jl b/src/recipes.jl index cc8c305d..7ddbaea5 100644 --- a/src/recipes.jl +++ b/src/recipes.jl @@ -560,13 +560,18 @@ end # otherwise, just use a histogram if is_installed("KernelDensity") @eval import KernelDensity - @eval function violin_coords(y, bins = 30) - kd = KernelDensity.kde(y, npoints = isa(bins, Integer) ? bins : 30) + @eval function violin_coords(y; trim::Bool=false) + 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 end else - @eval function violin_coords(y, bins = 30) - edges, widths = hist(y, isa(bins, Integer) ? bins : 30) + @eval function violin_coords(y; trim::Bool=false) + edges, widths = hist(y, 30) centers = 0.5 * (edges[1:end-1] + edges[2:end]) ymin, ymax = extrema(y) vcat(0.0, widths, 0.0), vcat(ymin, centers, ymax) @@ -575,7 +580,7 @@ end # 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) # TODO: add scatter series with outliers @@ -587,7 +592,7 @@ end # get the edges and widths y = d[:y][groupby.groupIds[i]] - widths, centers = violin_coords(y) + widths, centers = violin_coords(y, trim=trim) # normalize widths = _box_halfwidth * widths / maximum(widths) @@ -604,6 +609,9 @@ end # n = length(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) () @@ -613,14 +621,18 @@ end # --------------------------------------------------------------------------- # density -@recipe function f(::Type{Val{:density}}, x, y, z) - newx, newy = violin_coords(y, d[:bins]) +@recipe function f(::Type{Val{:density}}, x, y, z; trim=false) + newx, newy = violin_coords(y, trim=trim) if isvertical(d) newx, newy = newy, newx end x := newx y := newy seriestype := :path + + # clean up d + pop!(d, :trim) + () end