diff --git a/docs/src/gallery/geometries.md b/docs/src/gallery/geometries.md index 6699e1df4..936a96023 100644 --- a/docs/src/gallery/geometries.md +++ b/docs/src/gallery/geometries.md @@ -31,10 +31,10 @@ De = dataset("ggplot2","economics") De.Unemploy /= 10^3 plot(De, x=:Date, y=:Unemploy, Geom.line, - layer(Dp, xmin=:Start, xmax=:End, Geom.vband, color=:Party), + layer(Dp, xmin=:Start, xmax=:End, Geom.vband, color=:Party, alpha=[0.6]), Scale.color_discrete_manual("deepskyblue", "lightcoral"), Coord.cartesian(xmin=Date("1965-01-01"), ymax=12), - Guide.xlabel("Time"), Guide.ylabel("Unemployment (x10³)"), Guide.colorkey(title=""), + Guide.xlabel("Time"), Guide.ylabel("Unemployment (x10³)"), Guide.colorkey(title=""), Theme(default_color="black", key_position=:top)) ``` @@ -434,13 +434,16 @@ plot(x=[0, 1, 1, 2, 2, 3, 3, 2, 2, 1, 1, 0, 4, 5, 5, 4], ## [`Geom.rect`](@ref), [`Geom.rectbin`](@ref) ```@example -using Gadfly, Colors, DataFrames, RDatasets +using Gadfly, DataFrames set_default_plot_size(21cm, 8cm) -theme1 = Theme(default_color=RGBA(0, 0.75, 1.0, 0.5)) -D = DataFrame(x=[0.5,1], y=[0.5,1], x1=[0,0.5], y1=[0,0.5], x2=[1,1.5], y2=[1,1.5]) -pa = plot(D, x=:x, y=:y, Geom.rectbin, theme1) -pb = plot(D, xmin=:x1, ymin=:y1, xmax=:x2, ymax=:y2, Geom.rect, theme1) -hstack(pa, pb) +x1, y1, w1 = 0.5:10, rand(10), 0.09.+0.4*rand(10) +D = DataFrame(x=x1, y=rand(x1, 10), y1=y1, x2=x1.+w1, y2=y1.+w1, c=0:9) +p1 = plot(D, xmin=:x, ymin=:y1, xmax=:x2, ymax=:y2, color=[colorant"green"], + alpha=1:10, Geom.rect, Scale.alpha_discrete) +p2 = plot(D, xmin=:x, ymin=:y1, xmax=:x2, ymax=:y2, color=:c, alpha=[0.7], + Geom.rect, Guide.ylabel(nothing)) +p3 = plot(D, x=:x, y=:y, color=:c, alpha=[0.5], Geom.rectbin, Scale.color_discrete) +hstack(p1, p2, p3) ``` ```@example diff --git a/src/geom/rectbin.jl b/src/geom/rectbin.jl index 9b64363ff..48486b4d1 100644 --- a/src/geom/rectbin.jl +++ b/src/geom/rectbin.jl @@ -50,7 +50,7 @@ end default_statistic(geom::RectangularBinGeometry) = geom.default_statistic element_aesthetics(::RectangularBinGeometry) = - [:x, :y, :xmin, :xmax, :ymin, :ymax, :color] + [:x, :y, :xmin, :xmax, :ymin, :ymax, :color, :alpha] # Render rectangular bin (e.g., heatmap) geometry. # @@ -65,7 +65,8 @@ element_aesthetics(::RectangularBinGeometry) = function render(geom::RectangularBinGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthetics) default_aes = Gadfly.Aesthetics() - default_aes.color = discretize_make_ia(RGBA{Float32}[theme.default_color]) + default_aes.color = RGBA{Float32}[theme.default_color] + default_aes.alpha = Float64[theme.alphas[1]] aes = inherit(aes, default_aes) Gadfly.assert_aesthetics_defined("RectangularBinGeometry", aes, :xmin, :xmax, :ymin, :ymax) @@ -83,17 +84,17 @@ function render(geom::RectangularBinGeometry, theme::Gadfly.Theme, aes::Gadfly.A ywidths = [(y1 - y0)*cy - theme.bar_spacing for (y0, y1) in zip(aes.ymin, aes.ymax)] - if length(aes.color) == n - cs = aes.color - else - cs = Array{RGBA{Float32}}(undef, n) - for i in 1:n - cs[i] = aes.color[((i - 1) % length(aes.color)) + 1] - end + AT, CT = eltype(aes.alpha), eltype(aes.color) + aes_color = Vector{CT}(undef, n) + aes_alpha = Vector{Float64}(undef, n) + alphav = AT <: Int ? theme.alphas[aes.alpha] : aes.alpha + for (i, (_, c, a)) in enumerate(Compose.cyclezip(xmin, aes.color, alphav)) + aes_color[i] = c + aes_alpha[i] = a end allvisible = true - for c in cs + for c in aes_color if c == nothing allvisible = false break @@ -101,18 +102,19 @@ function render(geom::RectangularBinGeometry, theme::Gadfly.Theme, aes::Gadfly.A end if !allvisible - visibility = cs .!= nothing - cs = cs[visibility] + visibility = aes_color .!== nothing + aes_color = aes_color[visibility] + aes_alpha = aes_alpha[visibility] xmin = xmin[visibility] - xmax = xmax[visibility] + xwidths = xwidths[visibility] ymin = ymin[visibility] - ymax = ymax[visibility] + ywidths = ywidths[visibility] end return compose!( context(), rectangle(xmin, ymin, xwidths, ywidths, geom.tag), - fill(cs), + fill(aes_color), fillopacity(aes_alpha), stroke(nothing), svgclass("geometry"), svgattribute("shape-rendering", "crispEdges")) diff --git a/test/testscripts/rectbin.jl b/test/testscripts/rectbin.jl index 2bce23b2c..c0e6ba41e 100644 --- a/test/testscripts/rectbin.jl +++ b/test/testscripts/rectbin.jl @@ -6,6 +6,6 @@ set_default_plot_size(6inch, 3inch) # plot(dataset("Zelig", "macro"), x="Year", y="Country", color="GDP", Geom.rectbin) D = DataFrame(x=[0.5,1], y=[0.5,1], x1=[0,0.5], y1=[0,0.5], x2=[1,1.5], y2=[1,1.5]) -pa = plot(D, x=:x, y=:y, Geom.rectbin) -pb = plot(D, xmin=:x1, ymin=:y1, xmax=:x2, ymax=:y2, Geom.rect) +pa = plot(D, x=:x, y=:y, alpha=[0.5], Geom.rectbin) +pb = plot(D, xmin=:x1, ymin=:y1, xmax=:x2, ymax=:y2, alpha=[0.5], Geom.rect) hstack(pa,pb)