Skip to content

Commit

Permalink
Merge pull request #901 from tlnagy/tlnagy-pull-request/f29309b6
Browse files Browse the repository at this point in the history
Implement manual bandwidth specification for density plots
  • Loading branch information
tlnagy committed Sep 18, 2016
2 parents 63a0c28 + 0c268b2 commit 1bcd9fb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
18 changes: 17 additions & 1 deletion docs/src/lib/geoms/geom_density.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
```@meta
Author = "Daniel C. Jones"
Author = "Daniel C. Jones, Tamas Nagy"
```

# Geom.density
Expand All @@ -11,11 +11,17 @@ Draw a kernel density estimate from data. An alias for [Geom.line](@ref) with

* `x`: Sample to draw density estimate from.

## Arguments

* `bandwidth`: How closely the density estimate should mirror the data.
Larger values will smooth the density estimate out.

## Examples

```@setup 1
using RDatasets
using Gadfly
using Distributions
Gadfly.set_default_plot_size(14cm, 8cm)
```

Expand All @@ -26,3 +32,13 @@ plot(dataset("ggplot2", "diamonds"), x="Price", Geom.density)
```@example 1
plot(dataset("ggplot2", "diamonds"), x="Price", color="Cut", Geom.density)
```

```@example 1
# adjusting bandwidth manually
dist = MixtureModel(Normal, [(0.5, 0.2), (1, 0.1)])
xs = rand(dist, 10^5)
plot(layer(x=xs, Geom.density, Theme(default_color=colorant"orange")),
layer(x=xs, Geom.density(bandwidth=0.0003), Theme(default_color=colorant"green")),
layer(x=xs, Geom.density(bandwidth=0.25), Theme(default_color=colorant"purple")),
Guide.manual_color_key("bandwidth", ["auto", "bw=0.0003", "bw=0.25"], ["orange", "green", "purple"]))
```
4 changes: 2 additions & 2 deletions src/geom/line.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ function path()
return LineGeometry(preserve_order=true)
end

function density()
return LineGeometry(Gadfly.Stat.density())
function density(; bandwidth::Real=-Inf)
return LineGeometry(Gadfly.Stat.density(bandwidth=bandwidth))
end


Expand Down
13 changes: 7 additions & 6 deletions src/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,11 @@ end
immutable DensityStatistic <: Gadfly.StatisticElement
# Number of points sampled
n::Int
# Bandwidth used for the kernel density estimation
bw::Real

function DensityStatistic(n=300)
new(n)
function DensityStatistic(; n=300, bandwidth=-Inf)
new(n, bandwidth)
end
end

Expand Down Expand Up @@ -508,9 +510,8 @@ function apply_statistic(stat::DensityStatistic,
end

x_f64 = collect(Float64, aes.x)
# When will stat.n ever be <= 1? Seems pointless
# certainly its length will always be 1
window = stat.n > 1 ? KernelDensity.default_bandwidth(x_f64) : 0.1

window = stat.bw <= 0.0 ? KernelDensity.default_bandwidth(x_f64) : stat.bw
f = KernelDensity.kde(x_f64, bandwidth=window, npoints=stat.n)
aes.x = collect(Float64, f.x)
aes.y = f.density
Expand All @@ -528,7 +529,7 @@ function apply_statistic(stat::DensityStatistic,
aes.x = Array(Float64, 0)
aes.y = Array(Float64, 0)
for (c, xs) in groups
window = stat.n > 1 ? KernelDensity.default_bandwidth(xs) : 0.1
window = stat.bw <= 0.0 ? KernelDensity.default_bandwidth(xs) : stat.bw
f = KernelDensity.kde(xs, bandwidth=window, npoints=stat.n)
append!(aes.x, f.x)
append!(aes.y, f.density)
Expand Down
14 changes: 12 additions & 2 deletions test/density.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@

using Gadfly, DataArrays, RDatasets
using Gadfly, DataArrays, RDatasets, Distributions

plot(dataset("ggplot2", "diamonds"), x=:Price, Geom.density)
plot(dataset("ggplot2", "diamonds"), x=:Price, Geom.density)

# manual densities
dist = MixtureModel(Normal, [(0.5, 0.2), (1, 0.1)])
xs = rand(dist, 10^5)
plot(layer(x=xs, Geom.density, Theme(default_color=colorant"orange")),
layer(x=xs, Geom.density(bandwidth=0.003),
Theme(default_color=colorant"blue")),
layer(x=xs, Geom.density(bandwidth=0.25),
Theme(default_color=colorant"purple")),
Guide.manual_color_key("bandwidth", ["auto", "bw=0.003", "bw=0.25"],
["orange", "blue", "purple"]))

0 comments on commit 1bcd9fb

Please sign in to comment.