From c63ee219e690b1a3ce65c5def23d6ad332c046d0 Mon Sep 17 00:00:00 2001 From: Mattriks Date: Thu, 3 Jan 2019 13:33:35 +1100 Subject: [PATCH] Stat_histogram update --- docs/src/gallery/geometries.md | 11 +++---- src/statistics.jl | 45 +++++++------------------- test/testscripts/discrete_histogram.jl | 7 ++-- 3 files changed, 22 insertions(+), 41 deletions(-) diff --git a/docs/src/gallery/geometries.md b/docs/src/gallery/geometries.md index c40d12994..2ea21d0be 100644 --- a/docs/src/gallery/geometries.md +++ b/docs/src/gallery/geometries.md @@ -203,12 +203,11 @@ hstack(p1,p2) ```@example using Gadfly, RDatasets set_default_plot_size(21cm, 16cm) -p1 = plot(dataset("ggplot2", "diamonds"), x="Price", Geom.histogram) -p2 = plot(dataset("ggplot2", "diamonds"), x="Price", color="Cut", Geom.histogram) -p3 = plot(dataset("ggplot2", "diamonds"), x="Price", color="Cut", - Geom.histogram(bincount=30)) -p4 = plot(dataset("ggplot2", "diamonds"), x="Price", color="Cut", - Geom.histogram(bincount=30, density=true)) +D = dataset("ggplot2","diamonds") +p1 = plot(D, x="Price", Geom.histogram) +p2 = plot(D, x="Price", color="Cut", Geom.histogram) +p3 = plot(D, x="Price", color="Cut", Geom.histogram(bincount=30)) +p4 = plot(D, x="Price", color="Cut", Geom.histogram(bincount=30, density=true)) gridstack([p1 p2; p3 p4]) ``` diff --git a/src/statistics.jl b/src/statistics.jl index e656119da..c633af00b 100644 --- a/src/statistics.jl +++ b/src/statistics.jl @@ -332,16 +332,11 @@ function apply_statistic(stat::HistogramStatistic, vals, stat.minbincount, stat.maxbincount) end - if stat.density - x_min = Gadfly.concrete_minimum(vals) - span = x_max - x_min - binwidth = span / d - bincounts = bincounts ./ (sum(bincounts) * binwidth) - end - binwidth = (x_max - x_min) / d end + stat.density && (bincounts = bincounts ./ (sum(bincounts) * binwidth)) + if aes.color === nothing T = typeof(x_min + 1*binwidth) setfield!(aes, othervar, Array{Float64}(undef, d)) @@ -355,21 +350,15 @@ function apply_statistic(stat::HistogramStatistic, getfield(aes, othervar)[j] = bincounts[j] end else - groups = Dict() + CT, VT = eltype(aes.color), eltype(vals) + groups = Dict{CT,Vector{VT}}(c=>VT[] for c in unique(aes.color)) for (x, c) in zip(vals, cycle(aes.color)) - Gadfly.isconcrete(x) || continue - - if !haskey(groups, c) - groups[c] = Float64[x] - else - push!(groups[c], x) - end + Gadfly.isconcrete(x) && push!(groups[c], x) end T = typeof(x_min + 1*binwidth) setfield!(aes, minvar, Array{T}(undef, d * length(groups))) setfield!(aes, maxvar, Array{T}(undef, d * length(groups))) setfield!(aes, var, Array{T}(undef, d * length(groups))) - setfield!(aes, othervar, Array{Float64}(undef, d * length(groups))) colors = Array{RGB{Float32}}(undef, d * length(groups)) @@ -378,7 +367,6 @@ function apply_statistic(stat::HistogramStatistic, for (i, (c, xs)) in enumerate(groups) fill!(bincounts, 0) for x in xs - Gadfly.isconcrete(x) || continue if isdiscrete bincounts[round(Int,x)] += 1 else @@ -394,22 +382,13 @@ function apply_statistic(stat::HistogramStatistic, stack_height += bincounts[1:d] - if isdiscrete - for j in 1:d - idx = (i-1)*d + j - getfield(aes, var)[idx] = j - getfield(aes, othervar)[idx] = bincounts[j] - colors[idx] = c - end - else - for j in 1:d - idx = (i-1)*d + j - getfield(aes, minvar)[idx] = x_min + (j - 1) * binwidth - getfield(aes, maxvar)[idx] = x_min + j * binwidth - getfield(aes, var)[idx] = x_min + (j - 0.5) * binwidth - getfield(aes, othervar)[idx] = bincounts[j] - colors[idx] = c - end + for j in 1:d + idx = (i-1)*d + j + getfield(aes, var)[idx] = isdiscrete ? j : x_min + (j - 0.5) * binwidth + getfield(aes, minvar)[idx] = x_min + (j - 1) * binwidth + getfield(aes, maxvar)[idx] = x_min + j * binwidth + getfield(aes, othervar)[idx] = bincounts[j] + colors[idx] = c end end diff --git a/test/testscripts/discrete_histogram.jl b/test/testscripts/discrete_histogram.jl index 01ed9b2f7..d02144322 100644 --- a/test/testscripts/discrete_histogram.jl +++ b/test/testscripts/discrete_histogram.jl @@ -1,5 +1,8 @@ using Gadfly, Distributions -set_default_plot_size(6inch, 3inch) +set_default_plot_size(7inch, 3inch) -plot(x=rand(Poisson(20), 1000), Scale.x_discrete, Geom.histogram) +x = sort!(rand(Poisson(20), 1000)) +p1 = plot(x=x, Scale.x_discrete, Geom.histogram) +p2 = plot(x=x, Scale.x_discrete, Geom.histogram(density=true)) +hstack(p1, p2)