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

slope_aes #1201

Merged
merged 3 commits into from
Oct 3, 2018
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
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
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 0.7.1
# Version 0.8.1
* `Geom.abline`: add support for nonlinear `Scale` transformations (#1201)

# Version 0.8.0
* Add `linestyle` aesthetic (#1181)
* Add `Guide.shapekey` (#1156)
* `Geom.contour`: add support for `DataFrame` (#1150)
Expand Down
27 changes: 18 additions & 9 deletions docs/src/gallery/geometries.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@
## [`Geom.abline`](@ref)

```@example
using Gadfly, RDatasets, Compose
set_default_plot_size(14cm, 8cm)
plot(dataset("ggplot2", "mpg"),
using Gadfly, RDatasets, Compose, Random
Random.seed!(123)
set_default_plot_size(21cm, 8cm)

p1 = plot(dataset("ggplot2", "mpg"),
x="Cty", y="Hwy", label="Model", Geom.point, Geom.label,
intercept=[0], slope=[1], Geom.abline(color="red", style=:dash),
Guide.annotation(compose(context(), text(6,4, "y=x", hleft, vtop), fill("red"))))

x = [20*rand(20); exp(-3)]
D = DataFrame(x=x, y= exp.(-0.5*asinh.(x).+5) .+ 2*randn(length(x)))
abline = Geom.abline(color="red", style=:dash)
p2 = plot(D, x=:x, y=:y, Geom.point, Scale.x_asinh, Scale.y_log,
intercept=[148], slope=[-0.5], abline)
hstack(p1, p2)
```


Expand Down Expand Up @@ -145,9 +154,9 @@ hstack(pa,pb)
## [`Geom.errorbar`](@ref)

```@example
using Gadfly, RDatasets, Distributions
using Gadfly, RDatasets, Distributions, Random
set_default_plot_size(14cm, 8cm)
srand(1234)
Random.seed!(1234)
sds = [1, 1/2, 1/4, 1/8, 1/16, 1/32]
n = 10
ys = [mean(rand(Normal(0, sd), n)) for sd in sds]
Expand Down Expand Up @@ -262,11 +271,11 @@ hstack(p1,p2)
## [`Geom.path`](@ref)

```@example
using Gadfly
using Gadfly, Random
set_default_plot_size(21cm, 8cm)

n = 500
srand(1234)
Random.seed!(1234)
xjumps = rand(n)-.5
yjumps = rand(n)-.5
p1 = plot(x=cumsum(xjumps),y=cumsum(yjumps),Geom.path)
Expand Down Expand Up @@ -380,9 +389,9 @@ hstack(p1,p2)
## [`Geom.step`](@ref)

```@example
using Gadfly
using Gadfly, Random
set_default_plot_size(14cm, 8cm)
srand(1234)
Random.seed!(1234)
plot(x=rand(25), y=rand(25), Geom.step)
```

Expand Down
30 changes: 15 additions & 15 deletions docs/src/gallery/scales.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ hstack(p1,p2)
```@example
using Gadfly, Colors
set_default_plot_size(21cm, 8cm)
x = repeat(collect(1:10)-0.5, inner=[10])
y = repeat(collect(1:10)-0.5, outer=[10])
x = repeat(collect(1:10).-0.5, inner=[10])
y = repeat(collect(1:10).-0.5, outer=[10])
p1 = plot(x=x, y=y, color=x+y, Geom.rectbin,
Scale.color_continuous(colormap=p->RGB(0,p,0)))
p2 = plot(x=x, y=y, color=x+y, Geom.rectbin,
Expand All @@ -30,9 +30,9 @@ hstack(p1,p2,p3)
## [`Scale.color_discrete_hue`](@ref)

```@example
using Gadfly, Colors, RDatasets
using Gadfly, Colors, RDatasets, Random
set_default_plot_size(14cm, 8cm)
srand(1234)
Random.seed!(1234)

function gen_colors(n)
cs = distinguishable_colors(n,
Expand All @@ -51,9 +51,9 @@ plot(iris, x=:SepalLength, y=:SepalWidth, color=:Species,
```

```@example
using Gadfly, Colors, RDatasets
using Gadfly, Colors, RDatasets, Random
set_default_plot_size(21cm, 8cm)
srand(1234)
Random.seed!(1234)
xdata, ydata = rand(12), rand(12)
p1 = plot(x=xdata, y=ydata, color=repeat([1,2,3], outer=[4]))
p2 = plot(x=xdata, y=ydata, color=repeat([1,2,3], outer=[4]), Scale.color_discrete)
Expand All @@ -64,8 +64,8 @@ hstack(p1,p2)
## [`Scale.color_discrete_manual`](@ref)

```@example
using Gadfly
srand(12345)
using Gadfly, Random
Random.seed!(12345)
set_default_plot_size(14cm, 8cm)
plot(x=rand(12), y=rand(12), color=repeat(["a","b","c"], outer=[4]),
Scale.color_discrete_manual("red","purple","green"))
Expand Down Expand Up @@ -105,7 +105,7 @@ using StatsBase: winsor
set_default_plot_size(18cm, 8cm)

labs = [ "exp", "sqrt", "log", "winsor", "linear"]
funcs = [ x->60*(1-exp.(-0.2*x)), x->sqrt.(x)*10, x->log.(x)*10, x->winsor(x, prop=0.15), x->x*0.6 ]
funcs = [ x->60*(1.0.-exp.(-0.2*x)), x->sqrt.(x)*10, x->log.(x)*10, x->winsor(x, prop=0.15), x->x*0.6 ]
x = [1.0:30;]
D = vcat([DataFrame(x=x, y=f(x), linev=l) for (f,l) in zip(funcs, labs)]...)
D[134:136,:y] = NaN
Expand All @@ -122,29 +122,29 @@ hstack(p1,p2)
## [`Scale.x_continuous`](@ref), [`Scale.y_continuous`](@ref)

```@example
using Gadfly
using Gadfly, Random, Printf
set_default_plot_size(21cm, 8cm)
srand(1234)
Random.seed!(1234)
p1 = plot(x=rand(10), y=rand(10), Scale.x_continuous(minvalue=-10, maxvalue=10))
p2 = plot(x=rand(10), y=rand(10), Scale.x_continuous(format=:scientific))
p3 = plot(x=rand(10), y=rand(10), Scale.x_continuous(labels=x -> @sprintf("%0.4f", x)))
hstack(p1,p2,p3)
```

```@example
using Gadfly
using Gadfly, Random
set_default_plot_size(14cm, 8cm)
srand(1234)
Random.seed!(1234)
plot(x=rand(10), y=rand(10), Scale.x_log)
```


## [`Scale.x_discrete`](@ref), [`Scale.y_discrete`](@ref)

```@example
using Gadfly, DataFrames
using Gadfly, DataFrames, Random
set_default_plot_size(14cm, 8cm)
srand(1234)
Random.seed!(1234)
# Treat numerical x data as categories
p1 = plot(x=rand(1:3, 20), y=rand(20), Scale.x_discrete)
# To perserve the order of the columns in the plot when plotting a DataFrame
Expand Down
16 changes: 8 additions & 8 deletions docs/src/gallery/statistics.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ hstack(p1,p2)
## [`Stat.qq`](@ref)

```@example
using Gadfly, Distributions
using Gadfly, Distributions, Random
set_default_plot_size(21cm, 8cm)
srand(1234)
Random.seed!(1234)
p1 = plot(x=rand(Normal(), 100), y=rand(Normal(), 100), Stat.qq, Geom.point)
p2 = plot(x=rand(Normal(), 100), y=Normal(), Stat.qq, Geom.point)
hstack(p1,p2)
Expand All @@ -50,28 +50,28 @@ hstack(p1,p2)
## [`Stat.step`](@ref)

```@example
using Gadfly
using Gadfly, Random
set_default_plot_size(14cm, 8cm)
srand(1234)
Random.seed!(1234)
plot(x=rand(25), y=rand(25), Stat.step, Geom.line)
```


## [`Stat.x_jitter`](@ref), [`Stat.y_jitter`](@ref)

```@example
using Gadfly, Distributions
using Gadfly, Distributions, Random
set_default_plot_size(14cm, 8cm)
srand(1234)
Random.seed!(1234)
plot(x=rand(1:4, 500), y=rand(500), Stat.x_jitter(range=0.5), Geom.point)
```


## [`Stat.xticks`](@ref), [`Stat.yticks`](@ref)

```@example
using Gadfly
using Gadfly, Random
set_default_plot_size(14cm, 8cm)
srand(1234)
Random.seed!(1234)
plot(x=rand(10), y=rand(10), Stat.xticks(ticks=[0.0, 0.1, 0.9, 1.0]), Geom.point)
```
4 changes: 2 additions & 2 deletions docs/src/man/plotting.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ spy(M::AbstractMatrix, elements...; mapping...) -> Plot
For example:

```@setup 1
using Gadfly
using Gadfly, Random
set_default_plot_size(21cm, 8cm)
srand(12345)
Random.seed!(12345)
```

```@example 1
Expand Down
4 changes: 2 additions & 2 deletions src/Gadfly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1128,14 +1128,14 @@ const default_aes_scales = Dict{Symbol, Dict}(
:xmin => Scale.x_continuous(),
:xmax => Scale.x_continuous(),
:xintercept => Scale.x_continuous(),
:xslope => Scale.x_continuous(),
:xend => Scale.x_continuous(),
:yend => Scale.y_continuous(),
:y => Scale.y_continuous(),
:ymin => Scale.y_continuous(),
:ymax => Scale.y_continuous(),
:yintercept => Scale.y_continuous(),
:yslope => Scale.y_continuous(),
:slope => Scale.slope_continuous(),
:intercept => Scale.y_continuous(),
:middle => Scale.y_continuous(),
:upper_fence => Scale.y_continuous(),
:lower_fence => Scale.y_continuous(),
Expand Down
4 changes: 2 additions & 2 deletions src/geom/hvabline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ ABLineGeometry(; color=nothing, size=nothing, style=nothing, tag::Symbol=empty_t
Geom.abline[(; color=nothing, size=nothing, style=nothing)]

For each corresponding pair of elements in the `intercept` and `slope` aesthetics,
draw the lines `y = slope * x + intercept` across the plot canvas.
draw the lines `T(y) = slope * T(x) + intercept` across the plot canvas, where `T(⋅)` defaults to the identity function.
If unspecified, `intercept` defaults to [0] and `slope` to [1].

This geometry currently does not support non-linear `Scale` transformations.
This geometry also works with nonlinear `Scale` transformations of the `y` and/or `x` variable, with one caveat: for log transformations of the `x` variable, the `intercept` is the `y`-value at `x=1` rather than at `x=0`.
"""
const abline = ABLineGeometry

Expand Down
11 changes: 9 additions & 2 deletions src/scale.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ function make_labeler(scale::ContinuousScale)
end
end

const x_vars = [:x, :xmin, :xmax, :xintercept, :intercept, :xviewmin, :xviewmax, :xend]
const y_vars = [:y, :ymin, :ymax, :yintercept, :slope, :middle, :upper_fence, :lower_fence,
const x_vars = [:x, :xmin, :xmax, :xintercept, :xviewmin, :xviewmax, :xend]
const y_vars = [:y, :ymin, :ymax, :yintercept, :intercept, :middle, :upper_fence, :lower_fence,
:upper_hinge, :lower_hinge, :yviewmin, :yviewmax, :yend]

element_aesthetics(scale::ContinuousScale) = scale.vars
Expand Down Expand Up @@ -161,6 +161,13 @@ end
"""
const size_continuous = continuous_scale_partial([:size], identity_transform)

"""
slope_continuous[(; minvalue=nothing, maxvalue=nothing, labels=nothing,
format=nothing, minticks=2, maxticks=10, scalable=true)]
"""
const slope_continuous = continuous_scale_partial([:slope], identity_transform)


function apply_scale(scale::ContinuousScale,
aess::Vector{Gadfly.Aesthetics}, datas::Gadfly.Data...)
for (aes, data) in zip(aess, datas)
Expand Down