Skip to content
This repository has been archived by the owner on Dec 11, 2022. It is now read-only.

Bivariate mapping not working as a recipe #123

Closed
tpoisot opened this issue Oct 10, 2021 · 2 comments
Closed

Bivariate mapping not working as a recipe #123

tpoisot opened this issue Oct 10, 2021 · 2 comments

Comments

@tpoisot
Copy link
Member

tpoisot commented Oct 10, 2021

This needs add SimpleSDMLayers#interfaces-iteration to work

@mkborregaard, following your advice in #120, I tried to put the bivariate plot to a recipe. When I do this, it "works" (I get no error message), but the plot is what you get when only calling plot().

Any idea? Can it be because there is a plot(x, y) method for two layers already defined?

using Plots
using RecipesBase
using SimpleSDMLayers
using Colors, ColorBlendModes

layer1, layer2 = SimpleSDMPredictor(WorldClim, BioClim, [1,12]; left=-20.0, right=55.0, top=42.0, bottom=-36.0)

q1 = rescale(layer1, collect(LinRange(0.0, 1.0, 25)))
q2 = rescale(layer2, collect(LinRange(0.0, 1.0, 25)))

@shorthands bivariate

@recipe function f(::Type{Val{:bivariate}}, plt::AbstractPlot; classes=3, p0=colorant"#e8e8e8", p1=colorant"#64acbe", p2=colorant"#c85a5a")

    # Get the palettes
    c1 = palette([p0, p1], classes)
    c2 = palette([p0, p2], classes)
    breakpoints = LinRange(0.0, 1.0, classes+1)

    xlims --> extrema(longitudes(x))
    ylims--> extrema(latitudes(x))
    legend --> false
    aspectratio --> 1
    frame --> :box
    subplot --> 1
    seriestype --> :heatmap

    for i in 2:length(breakpoints)
        m1 = broadcast(v -> breakpoints[i - 1] <= v <= breakpoints[i], x)
        for j in 2:length(breakpoints)
            m2 = broadcast(v -> breakpoints[j - 1] <= v <= breakpoints[j], y)
            m = reduce(*, [m1, m2])
            replace!(m, false => nothing)
            replace!(m, nothing => NaN)
            @series begin
                color --> BlendMultiply(c1[i - 1], c2[j - 1])
                longitudes(m), latitudes(m), convert(Matrix{Float64}, m.grid)
            end
        end
    end
end

bivariate(q1, q2)
@tpoisot tpoisot mentioned this issue Oct 10, 2021
@tpoisot
Copy link
Member Author

tpoisot commented Oct 10, 2021

Turns out that yes, defining the bivariate map in the plot recipe solved everything

@tpoisot tpoisot closed this as completed Oct 10, 2021
@mkborregaard
Copy link
Contributor

Cool - is that the ifnal recipe?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants