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

Stat.histogram update #1236

Merged
merged 1 commit into from
Jan 3, 2019
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
11 changes: 5 additions & 6 deletions docs/src/gallery/geometries.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
```

Expand Down
45 changes: 12 additions & 33 deletions src/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))

Expand All @@ -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
Expand All @@ -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

Expand Down
7 changes: 5 additions & 2 deletions test/testscripts/discrete_histogram.jl
Original file line number Diff line number Diff line change
@@ -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)