Skip to content

Commit

Permalink
legends fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
rdboyes committed Sep 19, 2024
1 parent 6aacffe commit bf499cb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ KernelDensity = "0.6"
Loess = "0.6"
Makie = "0.21"
Reexport = "1.2"
TidierData = "0.15 - 1"
TidierData = "0.15"
julia = "1.9"

[extras]
Expand Down
26 changes: 14 additions & 12 deletions src/legend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ function build_legend(plot::GGPlot)

palette_function = nothing

if !(any([haskey(geom.aes, "color") || haskey(geom.aes, "color") for geom in plot.geoms]) ||
haskey(plot.default_aes, "color") || haskey(plot.default_aes, "color"))
if !(any([haskey(geom.aes, :colour) || haskey(geom.aes, :color) for geom in plot.geoms]) ||
haskey(plot.default_aes, :colour) || haskey(plot.default_aes, :color))
return nothing
end

Expand All @@ -30,15 +30,16 @@ function build_legend(plot::GGPlot)
all_aes = geom.aes
end

color_colname = haskey(all_aes, "colour") ? all_aes["colour"] :
haskey(all_aes, "color") ? all_aes["color"] :
color_colname = haskey(all_aes, :colour) ? all_aes[:colour][1] :
haskey(all_aes, :color) ? all_aes[:color][1] :
nothing

if isnothing(color_colname)
continue
end

plot_data = isnothing(geom.data) ? plot.data : geom.data
palette_function = plot.color_palette

if isnothing(palette_function)
if eltype(plot_data[!, color_colname]) <: Union{AbstractString, AbstractChar, CategoricalValue}
Expand All @@ -53,16 +54,17 @@ function build_legend(plot::GGPlot)
else
plot = plot + scale_colour_continuous(palette = :viridis)
end
#palette_function = plot.column_transformations[:color][2]
palette_function = plot.color_palette
end

if plot.legend_options[:color][:type] in ["manual", "discrete"]

plottable_data = palette_function(:color, [color_colname], plot_data)
labels = unique(plottable_data[:color].raw)
plottable_data = palette_function.(
levelcode.(CategoricalArray(plot_data[!, color_colname]))
)
labels = unique(plot_data[!, color_colname])

append!(legend, sort(DataFrame(labels = labels,
colors = unique(plottable_data[:color].makie_function(plottable_data[:color].raw)),
colors = unique(plottable_data),
options = _legend_geom_symbols[geom.args["geom_name"]],
element = _legend_geom_elements[geom.args["geom_name"]]),
:labels))
Expand All @@ -72,13 +74,13 @@ function build_legend(plot::GGPlot)

if plot.legend_options[:color][:type] in ["continuous", "binned"]

plottable_data = palette_function(:color, [color_colname], plot_data)
plottable_data = palette_function(plot_data[!, color_colname])

colorbar_kwargs[:colormap] = plot.legend_options[:color][:type] == "continuous" ? Symbol(plot.legend_options[:color][:palette]) :
cgrad(Symbol(plot.legend_options[:color][:palette]), 5, categorical = true)

lowlim = min(minimum(plottable_data[:color].raw), lowlim)
highlim = max(maximum(plottable_data[:color].raw), highlim)
lowlim = min(minimum(plot_data[!, color_colname]), lowlim)
highlim = max(maximum(plot_data[!, color_colname]), highlim)

colorbar = true
title = get(plot.legend_options[:color], :name, titlecase(string(color_colname)))
Expand Down
8 changes: 4 additions & 4 deletions test/test_geoms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@

m = Makie.plot(
Makie.SpecApi.GridLayout(
Makie.SpecApi.Axis(
[Makie.SpecApi.Axis(
plots = [
Makie.PlotSpec(
:Violin,
Expand All @@ -206,7 +206,7 @@
color = (x -> colours[x]).(levelcode.(cat_array))
)]; xticks = (unique(levelcode.(cat_array)),
unique(cat_array))
)
) TidierPlots.build_legend(t)]
)
)

Expand Down Expand Up @@ -243,7 +243,7 @@

m = Makie.plot(
Makie.SpecApi.GridLayout(
Makie.SpecApi.Axis(
[Makie.SpecApi.Axis(
plots = [
Makie.PlotSpec(
:Density,
Expand All @@ -258,7 +258,7 @@
chin.body_mass_g;
color =RGB(230/255, 159/255, 0/255))
]
)
) TidierPlots.build_legend(t)]
)
)

Expand Down
16 changes: 8 additions & 8 deletions test/test_scale_colour.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

m = Makie.plot(
Makie.SpecApi.GridLayout(
Makie.SpecApi.Axis(
[Makie.SpecApi.Axis(
plots = [
Makie.PlotSpec(
:Scatter,
Expand All @@ -19,7 +19,7 @@
color = (x -> colours[x]).(levelcode.(cat_array))
)
]
)
) TidierPlots.build_legend(t)]
)
)

Expand All @@ -37,7 +37,7 @@ end

m = Makie.plot(
Makie.SpecApi.GridLayout(
Makie.SpecApi.Axis(
[Makie.SpecApi.Axis(
plots = [
Makie.PlotSpec(
:Scatter,
Expand All @@ -46,7 +46,7 @@ end
color = (x -> colours[x]).(levelcode.(cat_array))
)
]
)
) TidierPlots.build_legend(t)]
)
)

Expand All @@ -63,7 +63,7 @@ end

m = Makie.plot(
Makie.SpecApi.GridLayout(
Makie.SpecApi.Axis(
[Makie.SpecApi.Axis(
plots = [
Makie.PlotSpec(
:Scatter,
Expand All @@ -72,7 +72,7 @@ end
color = colours
)
]
)
) TidierPlots.build_legend(t)]
)
)

Expand All @@ -90,7 +90,7 @@ end

m = Makie.plot(
Makie.SpecApi.GridLayout(
Makie.SpecApi.Axis(
[Makie.SpecApi.Axis(
plots = [
Makie.PlotSpec(
:Scatter,
Expand All @@ -99,7 +99,7 @@ end
color = ColorSchemes.colorschemes[:Purples_5][colours]
)
]
)
) TidierPlots.build_legend(t)]
)
)

Expand Down

0 comments on commit bf499cb

Please sign in to comment.