Add a colorbar to a viz in GeoStats

So it turns out to be easy to persuade viz to use a consistent colour range between plots.

I am mapping a set of n areas based on an n-row GeoTable. I give the data that viz uses to map colour using the color= kwarg. This takes a vector of data that is used to scale to the specified colorscheme. The color vector can be longer than n values and, in this case, viz will just use the first n values to define the colours of the n areas. Extra values are ignored as far as the map is concerned but they are used in the scaling of the data to the colorscheme. I can simply add two extra values to the vector, so there are n+2 values, where the extra two values are the minimum and maximum data values I want to use for the colour range. In my example, I can use the extrema of the national dataset:

        natave = sum(skipmissing(combo.Total_Awarded)) / sum(skipmissing(combo.subpop))
        combo.rat .= combo.Total_Awarded ./ combo.subpop ./ natave
        combo.col .= log10.(combo.rat)
        cr = extrema(combo.col)

And then I can plot the maps like this:

function citymap(gt, df, cr)
    fig = Mke.Figure()
    ax = []
    for (i, met) in enumerate(eachrow(df))
        longt = gt |> Filter(row -> !ismissing(row.newLocalAuthority) && row.newLocalAuthority in met.boroughs)
        crange = longt.col
        append!(crange, cr)
        push!(ax, Mke.Axis(fig[div(i - 1, 2)+1, mod(i - 1, 2)+1], title=met.metname, titlesize=10.0f0, 
                    limits=(met.xaxismin, met.xaxismax, met.yaxismin, met.yaxismax),
                    xlabel="BNG Easting",  xlabelsize=8.0f0, xtickformat="{:.0f}", xticklabelsize=6.0f0,
                    ylabel="BNG Northing", ylabelsize=8.0f0, ytickformat="{:.0f}", yticklabelsize=6.0f0))
        viz!(ax[i], longt.geometry, color=crange, colorscheme=:hawaii, facetcolor="black", showfacets=true, segmentsize=0.3f0)
        ax[i].aspect = Mke.DataAspect()
    end
    Mke.Colorbar(fig[1:2, 3], limits=extrema(gt.rat), colormap="hawaii", scale=log10, tickformat="{:.1f}",
        ticklabelsize=8.0f0, label="Multiple of national average funding per capita", labelsize=10.0f0)
    Mke.display(fig)
end

And this will produce four maps that all share the same colormap!

1 Like