From 76f4ab4930691192cc35f99da2b04615d4144a37 Mon Sep 17 00:00:00 2001 From: Randy Boyes Date: Tue, 2 Apr 2024 10:05:06 -0400 Subject: [PATCH] all tests fixed --- src/draw.jl | 24 ++++++++++---------- src/geoms/geom_text.jl | 6 +++-- src/transforms.jl | 51 +++++++++++++++++++++++------------------- 3 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/draw.jl b/src/draw.jl index 3899354..371ffdc 100644 --- a/src/draw.jl +++ b/src/draw.jl @@ -65,20 +65,20 @@ function Makie.SpecApi.Axis(plot::GGPlot) # if there is a specified column transformation, use it # otherwise use cat_inseq for string-like columns and as_is for everything else if haskey(geom.column_transformations, aes) - plottable_data = geom.column_transformations[aes](aes, plot_data) + plottable_data = geom.column_transformations[aes](aes, [aes_dict[a]], plot_data) elseif eltype(plot_data[!, aes_dict[a]]) <: Union{AbstractString, AbstractChar} - plottable_data = cat_inseq(aes, plot_data) + plottable_data = cat_inseq(aes, [aes_dict[a]], plot_data) else - plottable_data = as_is(aes, plot_data) + plottable_data = as_is(aes, [aes_dict[a]], plot_data) end # if the transform has a label associated with it, pass that into axis_options - if !isnothing(plottable_data.label_target) - axis_options[label_target] = plottable_data.label_function(plottable_data.raw) + if !isnothing(plottable_data[aes].label_target) + axis_options[plottable_data[aes].label_target] = plottable_data[aes].label_function(plottable_data[aes].raw) end # add the transformed data to list to eventually be passed to the plots kwargs - push!(visual_optional_aes, aes => plottable_data.makie_function(plottable_data.raw)) + push!(visual_optional_aes, aes => plottable_data[aes].makie_function(plottable_data[aes].raw)) end # which ones were given as arguments? @@ -111,18 +111,18 @@ function Makie.SpecApi.Axis(plot::GGPlot) # if there is a specified column transformation, use it # otherwise use cat_inseq for string-like columns and as_is for everything else if haskey(geom.column_transformations, aes) - plottable_data = geom.column_transformations[aes](aes_dict[a], plot_data) + plottable_data = geom.column_transformations[aes](aes, [aes_dict[a]], plot_data) elseif eltype(plot_data[!, aes_dict[a]]) <: Union{AbstractString, AbstractChar} - plottable_data = cat_inseq(aes_dict[a], plot_data) + plottable_data = cat_inseq(aes, [aes_dict[a]], plot_data) else - plottable_data = as_is(aes_dict[a], plot_data) + plottable_data = as_is(aes, [aes_dict[a]], plot_data) end # if the transform has a label associated with it, pass that into axis_options - if !isnothing(plottable_data[aes_dict[a]].label_target) - axis_options[plottable_data[aes_dict[a]].label_target] = plottable_data[aes_dict[a]].label_function(plottable_data[aes_dict[a]].raw) + if !isnothing(plottable_data[aes].label_target) + axis_options[plottable_data[aes].label_target] = plottable_data[aes].label_function(plottable_data[aes].raw) end - push!(visual_args_list, plottable_data[aes_dict[a]].makie_function(plottable_data[aes_dict[a]].raw)) + push!(visual_args_list, plottable_data[aes].makie_function(plottable_data[aes].raw)) end args = Tuple([geom.visual, visual_args_list...]) diff --git a/src/geoms/geom_text.jl b/src/geoms/geom_text.jl index e2da9e4..a161b7c 100644 --- a/src/geoms/geom_text.jl +++ b/src/geoms/geom_text.jl @@ -1,2 +1,4 @@ -geom_text = geom_template("geom_text", ["x", "y"], :Text) -geom_label = geom_template("geom_text", ["x", "y"], :Text) \ No newline at end of file +geom_text = geom_template("geom_text", ["x", "y"], :Text; + column_transformations = Dict{Symbol, Function}(:text => verbatim)) +geom_label = geom_template("geom_label", ["x", "y"], :Text; + column_transformations = Dict{Symbol, Function}(:text => verbatim)) \ No newline at end of file diff --git a/src/transforms.jl b/src/transforms.jl index b666e20..590dcc7 100644 --- a/src/transforms.jl +++ b/src/transforms.jl @@ -17,10 +17,10 @@ end # simplest one is as_is, which just gets a column # exactly as it is in the DataFrame -function as_is(target::Symbol, data::DataFrame) +function as_is(target::Symbol, source::Vector{Symbol}, data::DataFrame) return Dict{Symbol, PlottableData}( target => PlottableData( - data[!, target], # get the column out of the dataframe + data[!, source[1]], # get the column out of the dataframe identity, # do nothing to it nothing, nothing @@ -31,10 +31,10 @@ end # verbatim has a similar goal, but for String columns function convert_to(type::Type) - return function(target::Symbol, data::DataFrame) + return function(target::Symbol, source::Vector{Symbol}, data::DataFrame) return Dict{Symbol, PlottableData}( target => PlottableData( - type.(data[!, target]), + type.(data[!, source[1]]), identity, nothing, nothing @@ -45,10 +45,10 @@ end verbatim = convert_to(String) -function number_on_axis(target::Symbol, data::DataFrame) +function number_on_axis(target::Symbol, source::Vector{Symbol}, data::DataFrame) return Dict{Symbol, PlottableData}( target => PlottableData( - data[!, target], # get the column out of the dataframe + data[!, source[1]], # get the column out of the dataframe identity, # do nothing to it Symbol(String(target) * "ticks"), # the axis label it will have, e.g. :xticks Makie.automatic # calculate those ticks automatically @@ -60,28 +60,38 @@ end # categorical array handling options for String columns -function cat_inorder(target::Symbol, data::DataFrame) - cat_column = data[!, target] +function cat_inorder(target::Symbol, source::Vector{Symbol}, data::DataFrame) + cat_column = data[!, source[1]] cat_array = CategoricalArray(cat_column, levels = unique(cat_column), ordered = true) + + label_target = target == :x ? :xticks : + target == :y ? :yticks : + nothing + return Dict{Symbol, PlottableData}( target => PlottableData( cat_array, x -> levelcode.(x), - Symbol(String(target) * "ticks"), + label_target, x -> (1:length(levels(x)), levels(x)) ) ) end -function cat_inseq(target::Symbol, data::DataFrame) - cat_array = CategoricalArray(data[!, target]) +function cat_inseq(target::Symbol, source::Vector{Symbol}, data::DataFrame) + cat_array = CategoricalArray(data[!, source[1]]) + + label_target = target == :x ? :xticks : + target == :y ? :yticks : + nothing + return Dict{Symbol, PlottableData}( target => PlottableData( cat_array, x -> levelcode.(x), - Symbol(String(target) * "ticks"), + label_target, x -> (1:length(levels(x)), levels(x)) ) ) @@ -89,19 +99,14 @@ end # kernel density estimation for geom_contour -function kernel_density_2d(x::Symbol, y::Symbol, data::DataFrame) +function kernel_density_2d(target::Symbol, source::Vector{Symbol}, data::DataFrame) - k = kde((data[!, x], data[!, y])) + k = kde((data[!, source[1]], data[!, source[2]])) return Dict{Symbol, PlottableData}( - :x => PlottableData(k.x, identity, - :xticks, - Makie.automatic), - :y => PlottableData(k.y, identity, - :yticks, - Makie.automatic), - :z => PlottableData(k.density, identity, - :zticks, - Makie.automatic) + target => PlottableData(k.density, + identity, + nothing, + nothing) ) end