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

Layer aesthetic inheritance improvement #1463

Merged
merged 2 commits into from
Jul 8, 2020
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
2 changes: 1 addition & 1 deletion docs/src/gallery/statistics.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ df = DataFrame(x=x, y=y, ymin=y-ye, ymax=y+ye, g=gshift)
plot(y=[sigmoid, x->sigmoid(x+2)], xmin=[-10], xmax=[10],
Geom.line, Stat.func(100), color=[0,2], Guide.xlabel("x"),
layer(df, x=:x, y=:y, ymin=:ymin, ymax=:ymax, color=:g,
Geom.point, Geom.errorbar, Stat.x_jitter(range=1)),
Geom.point, Geom.yerrorbar, Stat.x_jitter(range=1)),
Scale.color_discrete_manual("deepskyblue","yellow3", levels=[0,2]),
Guide.colorkey(title="Function", labels=["Sigmoid(x)", "Sigmoid(x+2)"]),
Theme(errorbar_cap_length=0mm, key_position=:inside)
Expand Down
13 changes: 13 additions & 0 deletions docs/src/man/compositing.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,19 @@ plot(iris, Guide.colorkey(title=""),
layer(x=[2.0], y=[4], shape=[Shape.star1], color=[colorant"red"], size=[8pt]),
Theme(alphas=[0.7]))
```
And layers can inherit aesthetics from the plot:
```@example layer
set_default_plot_size(21cm, 8cm)
p1 = plot(iris, x=:SepalLength, y=:PetalLength,
layer(Geom.smooth(method=:loess), color=["Smooth"]),
layer(Geom.point, color=["Points"]))

p2 = plot(iris, x=:SepalLength, y=:PetalLength, color=:Species,
Geom.smooth(method=:lm), Geom.point, alpha=[0.6],
layer(Geom.smooth(method=:loess), color=[colorant"grey"], order=2))
hstack(p1, p2)
```
Note in some layers, it may be better to use specific Geoms e.g. `Geom.yerrorbar` rather than `Geom.errorbar`, since the latter will attempt to inherit aesthetics for both `Geom.xerrorbar` and `Geom.yerrobar`.

_Layer order_: the sequence in which layers are drawn, whether they overlap or not, can be
controlled with the `order` keyword. Layers with lower order numbers are
Expand Down
14 changes: 7 additions & 7 deletions src/Gadfly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -394,25 +394,25 @@ function render_prepare(plot::Plot)
# Process layers, filling inheriting mappings or data from the Plot where
# they are missing.
datas = Array{Data}(undef, length(plot.layers))
plot_aesthetics = keys(plot.mapping)
for (i, layer) in enumerate(plot.layers)
isa(layer.geom, Geom.Nil) && (layer.geom = Geom.point())
if layer.data_source === nothing && isempty(layer.mapping)
layer.data_source = plot.data_source
layer.mapping = plot.mapping
datas[i] = plot.data
else
datas[i] = Data()

if layer.data_source === nothing
layer.data_source = plot.data_source
end
layer.data_source===nothing && (layer.data_source = plot.data_source)

geom_aesthetics = intersect(element_aesthetics(layer.geom), plot_aesthetics)
geom_dict = filter(x->in(x.first, geom_aesthetics), plot.mapping)
layer.mapping = merge(geom_dict, layer.mapping)

if isempty(layer.mapping)
layer.mapping = plot.mapping
end

evalmapping!(layer.mapping, layer.data_source, datas[i])
end
if isa(layer.geom, Geom.Nil); layer.geom = Geom.point(); end # see #1062
end

# We need to process subplot layers somewhat as though they were regular
Expand Down
3 changes: 1 addition & 2 deletions src/geom/rectbin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ end

default_statistic(geom::RectangularBinGeometry) = geom.default_statistic

element_aesthetics(::RectangularBinGeometry) =
[:x, :y, :xmin, :xmax, :ymin, :ymax, :color, :alpha]
element_aesthetics(::RectangularBinGeometry) = [:xmin, :xmax, :ymin, :ymax, :color, :alpha]

# Render rectangular bin (e.g., heatmap) geometry.
#
Expand Down
8 changes: 6 additions & 2 deletions src/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,7 @@ For confidence bands, use `Stat.smooth()` with `Geom.ribbon`.
"""
const smooth = SmoothStatistic

function Stat.apply_statistic(stat::SmoothStatistic,
function apply_statistic(stat::SmoothStatistic,
scales::Dict{Symbol, Gadfly.ScaleElement},
coord::Gadfly.CoordinateElement,
aes::Gadfly.Aesthetics)
Expand All @@ -1158,7 +1158,11 @@ function Stat.apply_statistic(stat::SmoothStatistic,
aes_color = colorflag ? aes.color : fill(nothing, length(aes.x))

uc = unique(aes_color)
groups = Dict(c=>(xs[aes_color.==c], ys[aes_color.==c], aes.x[aes_color.==c]) for c in uc)
groups = if length(uc)==1
Dict(c=>(xs, ys, aes.x) for c in uc)
else
Dict(c=>(xs[aes_color.==c], ys[aes_color.==c], aes.x[aes_color.==c]) for c in uc)
end

# For aes.y returning a Float is ok if `y` is an Int or a Float
# There does not seem to be strong demand for other types of `y`
Expand Down