diff --git a/src/transformations/histogram.jl b/src/transformations/histogram.jl index a7fff0f69..aacb88454 100644 --- a/src/transformations/histogram.jl +++ b/src/transformations/histogram.jl @@ -45,23 +45,34 @@ function (h::HistogramAnalysis)(input::ProcessedLayer) datalimits = h.datalimits === automatic ? defaultdatalimits(input.positional) : h.datalimits options = valid_options(; datalimits, h.bins, h.closed, h.normalization) + N = length(input.positional) + default_plottype = categoricalplottypes[N] + plottype = Makie.plottype(input.plottype, default_plottype) + output = map(input) do p, n hist = _histogram(Tuple(p); pairs(n)..., pairs(options)...) edges, weights = hist.edges, hist.weights - named = length(edges) == 1 ? (; width=diff(first(edges))) : (;) - return (map(midpoints, edges)..., weights), named + named = plottype == BarPlot ? (; width=diff(first(edges))) : (;) + positional = if plottype == Stairs + edges = only(edges) + phantomedge = edges[end] # to bring step back to baseline + edges = vcat(edges, phantomedge) + z = zero(eltype(weights)) + heights = vcat(z, weights, z) + (edges, heights) + else + (map(midpoints, edges)..., weights) + end + return positional, named end - N = length(input.positional) label = h.normalization == :none ? "count" : string(h.normalization) labels = set(output.labels, N+1 => label) - attributes = if N == 1 + attributes = if plottype == BarPlot set(output.attributes, :gap => 0, :dodge_gap => 0) else output.attributes end - default_plottype = categoricalplottypes[N] - plottype = Makie.plottype(output.plottype, default_plottype) return ProcessedLayer(output; plottype, labels, attributes) end diff --git a/test/reference_tests.jl b/test/reference_tests.jl index 1d3b89226..fb2492357 100644 --- a/test/reference_tests.jl +++ b/test/reference_tests.jl @@ -27,7 +27,7 @@ reftest("barplot cat color palette") do draw(scales(Color = (; palette = :Set1_3))) end -reftest("barplot layout") do +reftest("barplot layout") do data((; x = ["A", "B", "C"], y = 1:3, z = ["X", "Y", "Z"])) * mapping(:x, :y; layout = :z) * visual(BarPlot) |> draw end @@ -244,6 +244,18 @@ reftest("histogram stack") do draw(specs) end +reftest("histogram stairs cat color") do + df = (x=[sin.(1:500); sin.(1:500) .* 2], z=repeat(["a", "b"], inner = 500)) + specs = data(df) * mapping(:x, layout=:z, color = :z) * visual(Stairs) * histogram() + draw(specs) +end + +reftest("histogram scatter cat color") do + df = (x=[sin.(1:500); sin.(1:500) .* 2], z=repeat(["a", "b"], inner = 500)) + specs = data(df) * mapping(:x, layout=:z, color = :z) * visual(Scatter) * histogram() + draw(specs) +end + reftest("histogram 2d") do df = (x=sin.(1:300), y=cos.(1:300)) specs = data(df) * mapping(:x, :y) * histogram() @@ -736,7 +748,7 @@ function presorted_plot(; with_missing::Bool) end group = ["2", "3", "1", "1", "3", "2"] some_value = sort(exp.(sin.(1:6))) - + df = (; countries, group, some_value) m1 = mapping(:countries, :some_value, color = :group) @@ -744,7 +756,7 @@ function presorted_plot(; with_missing::Bool) m3 = mapping(:countries => presorted, :some_value, color = :group => presorted) base = data(df) * visual(BarPlot, direction = :x) - + f = Figure() fg1 = draw!(f[1, 1], base * m1) fg2 = draw!(f[2, 1], base * m2) @@ -795,7 +807,7 @@ reftest("categorical color from continuous") do f end -reftest("title subtitle footnotes") do +reftest("title subtitle footnotes") do spec = pregrouped( fill(1:5, 6), fill(11:15, 6), @@ -816,7 +828,7 @@ reftest("title subtitle footnotes") do ) end -reftest("title subtitle footnotes single unconstrained facet") do +reftest("title subtitle footnotes single unconstrained facet") do spec = data((; x = 1:10, y = 11:20)) * mapping(:x, :y) * visual(Scatter) draw( spec; @@ -831,7 +843,7 @@ reftest("title subtitle footnotes single unconstrained facet") do ) end -reftest("title") do +reftest("title") do spec = pregrouped( fill(1:5, 6), fill(11:15, 6), @@ -847,7 +859,7 @@ reftest("title") do ) end -reftest("title subtitle footnotes settings") do +reftest("title subtitle footnotes settings") do spec = pregrouped( fill(1:5, 6), fill(11:15, 6), @@ -877,10 +889,10 @@ reftest("title subtitle footnotes settings") do ), axis = (; width = 100, height = 100) ) - + end -reftest("title subtitle footnotes fontsize inherit") do +reftest("title subtitle footnotes fontsize inherit") do spec = pregrouped( fill(1:5, 6), fill(11:15, 6), @@ -902,7 +914,7 @@ reftest("title subtitle footnotes fontsize inherit") do ) end -reftest("dodge barplot with errorbars") do +reftest("dodge barplot with errorbars") do f = Figure() df = ( x = [1, 1, 2, 2], @@ -960,7 +972,7 @@ reftest("dodge scatter with rangebars") do yhigh = cos.(range(0, 2pi, length = 20)) .+ 0.3, dodge = repeat(["A", "B"], 10) ) - + f = Figure() spec1 = data(df) * (mapping(:x, :y, dodge_x = :dodge, color = :dodge) * visual(Scatter) + mapping(:x, :ylow, :yhigh, dodge_x = :dodge, color = :dodge) * visual(Rangebars)) spec2 = data(df) * (mapping(:y, :x, dodge_y = :dodge, color = :dodge) * visual(Scatter) + mapping(:x, :ylow, :yhigh, dodge_y = :dodge, color = :dodge) * visual(Rangebars, direction = :x)) @@ -974,10 +986,10 @@ end reftest("manual legend labels in visual") do df_subjects = (; x = repeat(1:10, 10), y = cos.(1:100), id = repeat(1:10, inner = 10)) df_func = (; x = range(1, 10, length = 20), y = cos.(range(1, 10, length = 20))) - + spec1 = data(df_subjects) * mapping(:x, :y, group = :id => nonnumeric) * visual(Lines, linestyle = :dash, color = (:black, 0.2), label = "Subject data") spec2 = data(df_func) * mapping(:x, :y) * (visual(Lines, color = :tomato) + visual(Scatter, markersize = 12, color = :tomato, strokewidth = 2)) * visual(label = L"\cos(x)") - + draw(spec1 + spec2) end @@ -986,7 +998,7 @@ reftest("manual legend order") do spec1 = data(df) * mapping(:x, :y, color = :group) * visual(Lines) spec2 = data((; x = 1:10, y = cos.(1:10) .+ 2)) * mapping(:x, :y) * visual(Scatter, color = :purple, label = "Scatter") - + f = Figure() fg = draw!(f[1, 1], spec1 + spec2) legend!(f[1, 2], fg) diff --git a/test/reference_tests/histogram scatter cat color ref.png b/test/reference_tests/histogram scatter cat color ref.png new file mode 100644 index 000000000..9d0da343c Binary files /dev/null and b/test/reference_tests/histogram scatter cat color ref.png differ diff --git a/test/reference_tests/histogram stairs cat color ref.png b/test/reference_tests/histogram stairs cat color ref.png new file mode 100644 index 000000000..5a80979b7 Binary files /dev/null and b/test/reference_tests/histogram stairs cat color ref.png differ