Skip to content

Commit

Permalink
Geom.bar update 1 (#1428)
Browse files Browse the repository at this point in the history
* Geom.bar(position=:identity) plus alpha
  • Loading branch information
Mattriks authored Apr 30, 2020
1 parent a23fcdc commit d2b98e1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 47 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Each release typically has a number of minor bug fixes beyond what is listed her

# Version 1.x

* Add `Geom.bar(position=:identity)` + alpha enabled (#1428)
* Enable stacked guides (#1423)


Expand Down
35 changes: 25 additions & 10 deletions docs/src/gallery/geometries.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,19 @@ plot(De, x=:Date, y=:Unemploy, Geom.line,
## [`Geom.bar`](@ref)

```@example
using Gadfly, RDatasets
set_default_plot_size(14cm, 8cm)
plot(dataset("HistData", "ChestSizes"), x="Chest", y="Count", Geom.bar)
using ColorSchemes, DataFrames, Distributions, Gadfly
set_default_plot_size(21cm, 8cm)
x = range(-4, 4, length=30)
fn1(μ,x=x) = pdf.(Normal(μ, 1), x)
D = [DataFrame(x=x, y=fn1(μ), μ="$(μ)") for μ in [-1, 1]]
cpalette(p) = get(ColorSchemes.viridis, p)
p1 = plot(D[1], y=:y, x=:x, color=0:29, Geom.bar,
Scale.color_continuous(colormap=cpalette),
Theme(bar_spacing=-0.2mm, key_position=:none))
p2 = plot(D[1], x=:x, y=:y, Geom.bar, alpha=range(0.2,0.9, length=30))
p3 = plot(vcat(D...), x=:x, y=:y, color=:μ, alpha=[0.5],
Geom.bar(position=:identity))
hstack(p1, p2, p3)
```

```@example
Expand Down Expand Up @@ -273,13 +283,18 @@ set_default_plot_size(21cm, 16cm)
D = dataset("ggplot2","diamonds")
gamma = Gamma(2, 2)
Dgamma = DataFrame(x=rand(gamma, 10^4))
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(Dgamma, Coord.cartesian(xmin=0, xmax=20),
layer(x->pdf(gamma, x), 0, 20, Geom.line, Theme(default_color="black")),
layer(x=:x, Geom.histogram(bincount=20, density=true, limits=(min=0,))),
Theme(default_color="bisque") )
p1 = plot(D, x="Price", color="Cut", Geom.histogram)
p2 = plot(D, x="Price", color="Cut", Geom.histogram(bincount=30))
p3 = plot(Dgamma, Coord.cartesian(xmin=0, xmax=20),
layer(x->pdf(gamma, x), 0, 20, color=[colorant"black"]),
layer(x=:x, Geom.histogram(bincount=20, density=true, limits=(min=0,)),
color=[colorant"bisque"]))
a = repeat([0.75, 0.85], outer=40) # opacity
D2 = [DataFrame(x=rand(Normal(μ,1), 500), μ="$(μ)") for μ in [-1, 1]]
p4 = plot(vcat(D2...), x=:x, color=:μ, alpha=[a;a],
Geom.histogram(position=:identity, bincount=40, limits=(min=-4, max=4)),
Scale.color_discrete_manual("skyblue","moccasin")
)
gridstack([p1 p2; p3 p4])
```

Expand Down
53 changes: 27 additions & 26 deletions src/geom/bar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ BarGeometry(; position=:stack, orientation=:vertical, tag=empty_tag) =
orientation = orientation), tag)

element_aesthetics(geom::BarGeometry) = geom.orientation == :vertical ?
[:xmin, :xmax, :y, :color] : [:ymin, :ymax, :x, :color]
[:xmin, :xmax, :y, :color, :alpha] : [:ymin, :ymax, :x, :color, :alpha]

default_statistic(geom::BarGeometry) = geom.default_statistic

Expand All @@ -22,6 +22,7 @@ If orientation is `:horizontal` switch x for y. Optionally categorically
groups bars using the `color` aesthetic. If `position` is `:stack` they will
be placed on top of each other; if it is `:dodge` they will be placed side by
side. For labelling dodged or stacked bar plots see [`Stat.dodge`](@ref).
If `position` is `:identity` then the bars can overlap (use the `alpha` aesthetic).
"""
const bar = BarGeometry

Expand Down Expand Up @@ -50,39 +51,34 @@ histogram(; position=:stack, bincount=nothing,
# Render a single color bar chart
function render_bar(geom::BarGeometry,
theme::Gadfly.Theme,
aes::Gadfly.Aesthetics,
orientation::Symbol)
if orientation == :horizontal
aes::Gadfly.Aesthetics)
rect = if geom.orientation == :horizontal
XT = eltype(aes.x)
xz = convert(XT, zero(XT))
ctx = compose!(context(),
rectangle([min(xz, x) for x in aes.x],
[ymin*cy - theme.bar_spacing/2 for ymin in aes.ymin],
abs.(aes.x),
[(ymax - ymin)*cy - theme.bar_spacing
for (ymin, ymax) in zip(aes.ymin, aes.ymax)], geom.tag),
svgclass("geometry"))
rectangle(min.(xz, aes.x),
aes.ymin.*cy .- theme.bar_spacing/2,
abs.(aes.x),
(aes.ymax .- aes.ymin).*cy .- theme.bar_spacing, geom.tag)
else
YT = eltype(aes.y)
yz = convert(YT, zero(YT))
ctx = compose!(context(),
rectangle([xmin*cx + theme.bar_spacing/2 for xmin in aes.xmin],
[min(yz, y) for y in aes.y],
[(xmax - xmin)*cx - theme.bar_spacing
for (xmin, xmax) in zip(aes.xmin, aes.xmax)],
abs.(aes.y), geom.tag),
svgclass("geometry"))
rectangle(aes.xmin.*cx .+ theme.bar_spacing/2,
min.(yz, aes.y),
(aes.xmax .- aes.xmin).*cx .- theme.bar_spacing,
abs.(aes.y), geom.tag)
end

cs = aes.color === nothing ? theme.default_color : aes.color
compose!(ctx, fill(cs), svgclass("geometry"))
if isa(theme.bar_highlight, Function)
compose!(ctx, stroke(theme.bar_highlight(theme.default_color)))
aes_alpha = eltype(aes.alpha) <: Int ? theme.alphas[aes.alpha] : aes.alpha
sc = if isa(theme.bar_highlight, Function)
stroke(theme.bar_highlight(theme.default_color))
elseif isa(theme.bar_highlight, Color)
compose!(ctx, stroke(theme.bar_highlight))
stroke(theme.bar_highlight)
else
compose!(ctx, stroke(nothing))
stroke(nothing)
end

ctx = context()
compose!(ctx, rect, fill(aes.color), sc, fillopacity(aes_alpha), svgclass("geometry"))
return ctx
end

Expand Down Expand Up @@ -272,8 +268,13 @@ function render(geom::BarGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthetics)
error("Orientation must be :horizontal or :vertical")
end

if aes.color === nothing
ctx = render_bar(geom, theme, aes, geom.orientation)
default_aes = Gadfly.Aesthetics()
default_aes.color = RGBA{Float32}[theme.default_color]
default_aes.alpha = Float64[theme.alphas[1]]
aes = inherit(aes, default_aes)

if geom.position==:identity || length(aes.color)==1
ctx = render_bar(geom, theme, aes)
elseif geom.position == :stack
if geom.orientation == :horizontal
for y in unique(aes.ymin)
Expand Down
20 changes: 10 additions & 10 deletions test/testscripts/color_bar.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Gadfly

set_default_plot_size(8inch, 3inch)
set_default_plot_size(6inch, 8inch)

dodge1 = plot(x=["a","a"], y=[3,2], color=["red","blue"],
Geom.bar(position=:dodge));
dodge2 = plot(x=["a","a","b","b"], y=[3,2,1,2], color=["red","blue","red","blue"],
Geom.bar(position=:dodge));
stack1 = plot(x=["a","a"], y=[3,2], color=["red","blue"],
Geom.bar(position=:stack));
stack2 = plot(x=["a","a","b","b"], y=[3,2,1,2], color=["red","blue","red","blue"],
Geom.bar(position=:stack));
c = ["red", "blue"]
dodge1 = plot(x=["a","a"], y=[3,2], color=c, Geom.bar(position=:dodge))
dodge2 = plot(x=["a","a","b","b"], y=[3,2,1,2], color=[c;c], Geom.bar(position=:dodge))
stack1 = plot(x=["a","a"], y=[3,2], color=c, Geom.bar(position=:stack))
stack2 = plot(x=["a","a","b","b"], y=[3,2,1,2], color=[c;c], Geom.bar(position=:stack))
identity1 = plot(x=["a","a"], y=[3,2], color=c, alpha=[0.5], Geom.bar(position=:identity))
identity2 = plot(x=["a","a","b","b"], y=[3,2,1,2], color=[c;c], alpha=[0.5],
Geom.bar(position=:identity))

gridstack([dodge1 dodge2; stack1 stack2])
gridstack([dodge1 dodge2; stack1 stack2; identity1 identity2])
2 changes: 1 addition & 1 deletion test/testscripts/histogram_limits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set_default_plot_size(6inch, 3inch)

beta = Beta(2,2)
Dbeta = DataFrame(x=rand(beta, 10^4))
layer1 = layer(x->pdf(beta, x), 0, 1, Geom.line, Theme(default_color="black"))
layer1 = layer(x->pdf(beta, x), 0, 1, Theme(default_color="black"))
p1 = plot(Dbeta, layer1,
layer(x=:x, Geom.histogram(bincount=20, density=true, limits=(min=0,))),
)
Expand Down

0 comments on commit d2b98e1

Please sign in to comment.