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

HBandGeometry and VBandGeometry #1176

Closed
Closed
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
79 changes: 41 additions & 38 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,66 @@
This is a log of major changes in Gadfly between releases. It is not exhaustive.
Each release typically has a number of minor bug fixes beyond what is listed here.

# Version 1.1.0
* Add `Geom.vband` and `Geom.hband`.
* Add RectangularGeometry.
* RectangularBinGeometry now uses RectangularGeometry for rendering.

# Version 0.9.0
* conditionally depend on DataFrames (#1204)
* `Geom.abline`: add support for nonlinear `Scale` transformations (#1201)
* drop support for Julia 0.6 (#1189)
* conditionally depend on DataFrames. (#1204)
* `Geom.abline`: add support for nonlinear `Scale` transformations. (#1201)
* drop support for Julia 0.6. (#1189)

# Version 0.8.0
* Add `linestyle` aesthetic (#1181)
* Add `Guide.shapekey` (#1156)
* `Geom.contour`: add support for `DataFrame` (#1150)
* Add `linestyle` aesthetic. (#1181)
* Add `Guide.shapekey`. (#1156)
* `Geom.contour`: add support for `DataFrame`. (#1150)

# Version 0.7.0

* Support DataFrames.jl v0.11+ (#1090, #1129, #1131)
* Change `Theme(grid_strokedash=)` to `Theme(grid_line_style=)` and include in docs (#1106)
* Add `Geom.ellipse` (#1103)
* Improved SVG interactivity (#1037)
* Support DataFrames.jl v0.11+. (#1090, #1129, #1131)
* Change `Theme(grid_strokedash=)` to `Theme(grid_line_style=)` and include in docs. (#1106)
* Add `Geom.ellipse`. (#1103)
* Improved SVG interactivity. (#1037)

# Version 0.6.5

* ColorKeys inside plot panel (#1087)
* Add `Geom.hair` (#1076)
* Shape module (#1050)
* Boxplot improvements
* ColorKeys inside plot panel. (#1087)
* Add `Geom.hair`. (#1076)
* Shape module. (#1050)
* Boxplot improvements.

# Version 0.6.4

* Regression testing tools (#1020)
* Regression testing tools. (#1020)

# Version 0.6.3

* Wide format data (#1013)
* Wide format data. (#1013)

# Version 0.6.2

* Add `Geom.rect` (#993)
* Add `Geom.vectorfield` (#992)
* Unified size, color, shape aesthetics for Geom.point (#991)
* {h,v}line point shapes
* Add `Geom.rect`. (#993)
* Add `Geom.vectorfield`. (#992)
* Unified size, color, shape aesthetics for Geom.point. (#991)
* {h,v}line point shapes.

# Version 0.6.1

* Add `Stat.smooth` (#983)
* Add `Stat.smooth`. (#983)

# Version 0.6.0

* Dramatically speed up precompilation by removing old, duplicate code (#958)
* Add `Geom.abline` (#957)
* Add `Geom.density2d` (#959)
* Drop support for Julia 0.4 (#954)
* Dramatically speed up precompilation by removing old, duplicate code. (#958)
* Add `Geom.abline`. (#957)
* Add `Geom.density2d`. (#959)
* Drop support for Julia 0.4. (#954)

# Version 0.5.3

* Support for size aesthetic for `Geom.point` (#952, @tlnagy & @Mattriks)
* Various doc improvements (#923, #933, #943)
* Improved Juno support (#920, @MikeInnes)
* Support for size aesthetic for `Geom.point`. (#952, @tlnagy & @Mattriks)
* Various doc improvements. (#923, #933, #943)
* Improved Juno support. (#920, @MikeInnes)

# Version 0.4.1

Expand All @@ -74,13 +79,13 @@ Each release typically has a number of minor bug fixes beyond what is listed her

* Switch from the Color package to the new Colors package.

* Add `Geom.beeswarm`
* Add `Geom.beeswarm`.

# Version 0.3.13

* Add a `push!` function allowing plots to be built incrementally.

* Add `Stat.x_jitter` and `Stat.y_jitter`
* Add `Stat.x_jitter` and `Stat.y_jitter`.

* Fix a missing key with `colorkey_swatch_shape=:circle`.

Expand All @@ -90,7 +95,7 @@ Each release typically has a number of minor bug fixes beyond what is listed her

# Version 0.3.12

* Add `Geom.polygon`
* Add `Geom.polygon`.

* Add `Geom.violin`.

Expand Down Expand Up @@ -121,18 +126,18 @@ Each release typically has a number of minor bug fixes beyond what is listed her

# Version 0.3.10

* Support for [Patchwork.jl](https://github.com/shashi/Patchwork.jl)
* Support for [Patchwork.jl](https://github.com/shashi/Patchwork.jl).

* Several fixes for plotting dates.

# Version 0.3.9

* Transition from DateTime.jl to Dates.jl (or Base.Dates in Julia 0.4)
* Transition from DateTime.jl to Dates.jl (or Base.Dates in Julia 0.4).

* Several fixes for `Stat.contour`
* Several fixes for `Stat.contour`.

* Split the interface for pretty printing scales into a separate package:
[Showoff.jl](https://github.com/dcjones/Showoff.jl)
[Showoff.jl](https://github.com/dcjones/Showoff.jl).

# Version 0.3.8

Expand Down Expand Up @@ -220,5 +225,3 @@ Each release typically has a number of minor bug fixes beyond what is listed her
keys are wrapped automatically.

* Default Theme changes.


37 changes: 33 additions & 4 deletions docs/src/gallery/geometries.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,35 @@ p2 = plot(D, x=:x, y=:y, Geom.point, Scale.x_asinh, Scale.y_log,
hstack(p1, p2)
```

## [`Geom.band`](@ref), [`Geom.hband`](@ref), [`Geom.vband`](@ref)


```@example
using Colors, Gadfly, RDatasets

Dp = dataset("ggplot2","presidential")[3:end,:]
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),
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=""),
Theme(default_color="black", key_position=:top))
```

```@example
using Gadfly, RDatasets

p1 = plot(dataset("datasets", "iris"), x="SepalLength", y="SepalWidth", Geom.point,
layer(xmin=[5.0, 7.0], xmax=[6.5, 8.0] , Geom.vband, Theme(default_color="green")));

p2 = plot(dataset("datasets", "iris"), x="SepalLength", y="SepalWidth", Geom.point,
layer(ymin=[2.5, 3.6], ymax=[3.0, 4.0], Geom.hband, Theme(default_color="red")));

hstack(p1, p2)
```

## [`Geom.bar`](@ref)

Expand Down Expand Up @@ -111,7 +140,7 @@ using Gadfly, RDatasets, Distributions
set_default_plot_size(14cm, 8cm)
dist = MixtureModel(Normal, [(0.5, 0.2), (1, 0.1)])
xs = rand(dist, 10^5)
plot(layer(x=xs, Geom.density, Theme(default_color="orange")),
plot(layer(x=xs, Geom.density, Theme(default_color="orange")),
layer(x=xs, Geom.density(bandwidth=0.0003), Theme(default_color="green")),
layer(x=xs, Geom.density(bandwidth=0.25), Theme(default_color="purple")),
Guide.manual_color_key("bandwidth", ["auto", "bw=0.0003", "bw=0.25"],
Expand Down Expand Up @@ -483,16 +512,16 @@ using Gadfly, RDatasets
set_default_plot_size(21cm, 8cm)

coord = Coord.cartesian(xmin=-2, xmax=2, ymin=-2, ymax=2)
p1 = plot(coord, z=(x,y)->x*exp(-(x^2+y^2)),
xmin=[-2], xmax=[2], ymin=[-2], ymax=[2],
p1 = plot(coord, z=(x,y)->x*exp(-(x^2+y^2)),
xmin=[-2], xmax=[2], ymin=[-2], ymax=[2],
# or: x=-2:0.25:2.0, y=-2:0.25:2.0,
Geom.vectorfield(scale=0.4, samples=17), Geom.contour(levels=6),
Scale.x_continuous(minvalue=-2.0, maxvalue=2.0),
Scale.y_continuous(minvalue=-2.0, maxvalue=2.0),
Guide.xlabel("x"), Guide.ylabel("y"), Guide.colorkey(title="z"))

volcano = Matrix{Float64}(dataset("datasets", "volcano"))
volc = volcano[1:4:end, 1:4:end]
volc = volcano[1:4:end, 1:4:end]
coord = Coord.cartesian(xmin=1, xmax=22, ymin=1, ymax=16)
p2 = plot(coord, z=volc, x=1.0:22, y=1.0:16,
Geom.vectorfield(scale=0.05), Geom.contour(levels=7),
Expand Down
4 changes: 2 additions & 2 deletions src/coord.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function apply_coordinate(coord::Cartesian, aess::Vector{Gadfly.Aesthetics},
for var in coord.xvars
for aes in aess
vals = getfield(aes, var)
vals === nothing && continue
(vals === nothing || eltype(vals) <: Measure) && continue

if !isa(vals, AbstractArray)
vals = [vals]
Expand All @@ -173,7 +173,7 @@ function apply_coordinate(coord::Cartesian, aess::Vector{Gadfly.Aesthetics},
for var in coord.yvars
for aes in aess
vals = getfield(aes, var)
vals === nothing && continue
(vals === nothing || eltype(vals) <: Measure) && continue

# Outliers is an odd aesthetic that needs special treatment.
if var == :outliers
Expand Down
28 changes: 28 additions & 0 deletions src/geom/hvband.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Geom.band[(; orientation=:vertical)]

Draw bands across the plot canvas with a horizontal span specifed by `xmin` and `xmax` if `orientation` is `:vertical`, or a vertical span specified by `ymin` and `ymax` if the `orientation` is `:horizontal`.

This geometry is equivalent to [`Geom.rect`](@ref) with [`Stat.band`](@ref).
"""
band(;orientation=:vertical) = RectangularGeometry(Stat.band(orientation))


"""
Geom.hband[()]

Draw horizontal bands across the plot canvas with a vertical span specified by `ymin` and `ymax` aesthetics.

This geometry is equivalent to [`Geom.band`](@ref) with `orientation` set to `:vertical`.
"""
hband() = band(orientation=:horizontal)


"""
Geom.vband[()]

Draw vertical bands across the plot canvas with a horizontal span specified by `xmin` and `xmax` aesthetics.

This geometry is equivalent to [`Geom.band`](@ref).
"""
const vband = band
111 changes: 111 additions & 0 deletions src/geom/rect.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using Compose: x_measure, y_measure

struct RectangularGeometry <: Gadfly.GeometryElement
default_statistic::Gadfly.StatisticElement
tag::Symbol
end

function RectangularGeometry(
default_statistic::Gadfly.StatisticElement=Gadfly.Stat.identity();
tag=empty_tag)
RectangularGeometry(default_statistic, tag)
end

"""
Geom.rect

Draw colored rectangles with the corners specified by the
`xmin`, `xmax`, `ymin` and `ymax` aesthetics. Optionally
specify their `color`.
"""
const rect = RectangularGeometry

default_statistic(geom::RectangularGeometry) = geom.default_statistic

element_aesthetics(::RectangularGeometry) =
[:xmin, :xmax, :ymin, :ymax, :color]

# Render rectangle geometry.
#
# Args:
# geom: rect geometry
# theme: the plot's theme
# aes: some aesthetics
#
# Returns
# A compose form.
#
function render(geom::RectangularGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthetics)

default_aes = Gadfly.Aesthetics()
default_aes.color = discretize_make_ia(RGBA{Float32}[theme.default_color])
aes = inherit(aes, default_aes)

Gadfly.assert_aesthetics_defined("RectangularGeometry", aes, :xmin, :xmax, :ymin, :ymax)
Gadfly.assert_aesthetics_equal_length("RectangularGeometry", aes, :xmin, :xmax, :ymin, :ymax)

xmin = aes.xmin
xmax = aes.xmax
ymin = aes.ymin
ymax = aes.ymax

n = length(xmin)

if length(aes.color) == n
cs = aes.color
else
cs = Vector{RGBA{Float32}}(undef, n)
for i in 1:n
cs[i] = aes.color[((i - 1) % length(aes.color)) + 1]
end
end

allvisible = true
for c in cs
if c == nothing
allvisible = false
break
end
end

if !allvisible
visibility = cs .!= nothing
cs = cs[visibility]
xmin = xmin[visibility]
xmax = xmax[visibility]
ymin = ymin[visibility]
ymax = ymax[visibility]
end

# xwidths = [(x1 - x0)*cx
# for (x0, x1) in zip(xmin, xmax)]
#
# ywidths = [(y1 - y0)*cy
# for (y0, y1) in zip(ymin, ymax)]
#
# return compose!(
# context(),
# rectangle(xmin, ymin, xwidths, ywidths, geom.tag),
# fill(cs),
# stroke(nothing),
# svgclass("geometry"),
# svgattribute("shape-rendering", "crispEdges"))

# TODO: determine performance hit of using polygons.
polys = Vector{Vector{Tuple{Measure, Measure}}}(undef, length(xmin))
for i in 1:length(xmin)
x0 = x_measure(xmin[i])
x1 = x_measure(xmax[i])
y0 = y_measure(ymin[i])
y1 = y_measure(ymax[i])
polys[i] = Tuple{Measure, Measure}[(x0, y0), (x0, y1), (x1, y1), (x1, y0)]
end

return compose!(
context(),
Compose.polygon(polys),
fill(cs),
stroke(nothing),
svgclass("geometry"),
svgattribute("shape-rendering", "crispEdges"))
end
Loading