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

Legends for continuously styled variables #91

Closed
wants to merge 6 commits into from
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
1 change: 1 addition & 0 deletions src/AlgebraOfGraphics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ include("analysis/frequency.jl")
include("analysis/histogram.jl")
include("legend.jl")
include("dodge.jl")
include("colorbar.jl")
include("draw.jl")

end # module
18 changes: 18 additions & 0 deletions src/colorbar.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import AbstractPlotting.MakieLayout.LColorbar

function AbstractPlotting.MakieLayout.LColorbar(parent, plots::Vector{<: AbstractPlot}; kwargs...)
colormap = plots[1].colormap
# compute colorrange
min = minimum(p.colorrange[][1] for p in plots)
max = maximum(p.colorrange[][2] for p in plots)
colorrange = (min, max)

for p in plots
p.colorrange = colorrange
end

LColorbar(parent,
colormap = plots[1].colormap,
limits = colorrange;
kwargs...)
end
58 changes: 57 additions & 1 deletion src/draw.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ function layoutplot!(scene, layout, ts::ElementOrList)
end
hidexdecorations!.(axs[1:end-1, :], grid = false)
hideydecorations!.(axs[:, 2:end], grid = false)

for_colormap = []
colorname = nothing

for_markersize = []
markersizename = nothing

legend = Legend()
level_dict = Dict{Symbol, Any}()
encountered = Set()
Expand All @@ -119,6 +125,7 @@ function layoutplot!(scene, layout, ts::ElementOrList)
P isa Symbol && (P = getproperty(AbstractPlotting, P))
args, kwargs = split(options)
names, args = extract_names(args)
kwnames, _ = extract_names(kwargs)
attrs = Attributes(kwargs)
apply_alpha_transparency!(attrs)
x_pos = pop!(attrs, :layout_x, 1) |> to_value |> rank
Expand All @@ -134,6 +141,22 @@ function layoutplot!(scene, layout, ts::ElementOrList)
attrs.width = w
end
current = AbstractPlotting.plot!(ax, P, attrs, args...)
if hasproperty(style.value, :color)
push!(for_colormap, current)
if isnothing(colorname)
colorname = kwnames.color
else
@assert colorname == kwnames.color
end
end
if hasproperty(style.value, :markersize)
push!(for_markersize, extrema(current[:markersize][]))
if isnothing(markersizename)
markersizename = kwnames.markersize
else
@assert markersizename == kwnames.markersize
end
end
set_axis_labels!(ax, names)
set_axis_ticks!(ax, ticks)
for (k, v) in pairs(pkeys)
Expand All @@ -153,13 +176,46 @@ function layoutplot!(scene, layout, ts::ElementOrList)
end
end
end

# this holds the legends (one entrygroup for markersize, one for color, ...)
entrygroups = Vector{EntryGroup}()

if length(for_markersize) > 0
vmin = minimum(first.(for_markersize))
vmax = maximum(last.(for_markersize))
markersizes = MakieLayout.locateticks(vmin, vmax, 4)

group_size = [MakieLayout.MarkerElement(marker = :circle, color = :black, strokecolor = :transparent, markersize = ms * AbstractPlotting.px) for ms in markersizes]

append!(entrygroups, create_entrygroups([group_size], [string.(markersizes)], [string(markersizename)]))
end

legend_layout = layout[1, end+1] = GridLayout(tellheight = false)

if !isempty(legend.sections)
try
layout[1, 2] = create_legend(scene, legend)
append!(entrygroups, create_entrygroups(legend))
catch e
@warn "Automated legend was not possible due to $e"
end
end

haslegend = length(entrygroups) > 0
if haslegend
leg = legend_layout[1, 1] = MakieLayout.LLegend(scene, Node(entrygroups))
leg.framevisible[] = false
leg.tellheight[] = true
end

if length(for_colormap) > 0
T = typeof(for_colormap[1])
cbar_index = haslegend + 1
cbar = legend_layout[cbar_index, 1] = MakieLayout.LColorbar(scene, T[for_colormap...], title=string(colorname), titlevisible=false, width=30, height=120)
legend_layout[cbar_index, 1, Top()] = LText(scene, string(colorname), padding = (15,15,15,15))
end

MakieLayout.trim!(legend_layout)
MakieLayout.trim!(layout)

layout_x_levels = get(level_dict, :layout_x, nothing)
layout_y_levels = get(level_dict, :layout_y, nothing)
Expand Down
33 changes: 30 additions & 3 deletions src/legend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ function add_entry!(legend::Legend, entry::String; title::String="")
return add_entry!(names, sections, entry; default=LegendSection(title))
end

function create_legend(scene, legend::Legend)
function create_entrygroups(legend::Legend)
legend = remove_duplicates(legend)
sections = legend.sections
MakieLayout.LLegend(
scene,
create_entrygroups(
getproperty.(sections, :plots),
getproperty.(sections, :names),
# LLegend needs `nothing` to remove the space for a missing title
Expand Down Expand Up @@ -66,3 +65,31 @@ function unique_indices(x; keep)
inds_keep = findall(==(keep), x)
sort!(union!(inds_keep, first_inds))
end

using AbstractPlotting.MakieLayout: Optional, LegendEntry, EntryGroup
function create_entrygroups(contents::AbstractArray,
labels::AbstractArray{String},
title::Optional{String} = nothing)

if length(contents) != length(labels)
error("Number of elements not equal: $(length(contents)) content elements and $(length(labels)) labels.")
end

entries = [LegendEntry(label, content) for (content, label) in zip(contents, labels)]
entrygroups = Vector{EntryGroup}([(title, entries)])
end

function create_entrygroups(contentgroups::AbstractArray{<:AbstractArray},
labelgroups::AbstractArray{<:AbstractArray},
titles::AbstractArray{<:Optional{String}})

if !(length(titles) == length(contentgroups) == length(labelgroups))
error("Number of elements not equal: $(length(titles)) titles, $(length(contentgroups)) content groups and $(length(labelgroups)) label groups.")
end

entries = [[LegendEntry(l, pg) for (l, pg) in zip(labelgroup, contentgroup)]
for (labelgroup, contentgroup) in zip(labelgroups, contentgroups)]

entrygroups = Vector{EntryGroup}([(t, en) for (t, en) in zip(titles, entries)])
end