Skip to content

Commit

Permalink
all tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
rdboyes committed Apr 2, 2024
1 parent 4c430d3 commit 76f4ab4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 37 deletions.
24 changes: 12 additions & 12 deletions src/draw.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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...])
Expand Down
6 changes: 4 additions & 2 deletions src/geoms/geom_text.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
geom_text = geom_template("geom_text", ["x", "y"], :Text)
geom_label = geom_template("geom_text", ["x", "y"], :Text)
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))
51 changes: 28 additions & 23 deletions src/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -60,48 +60,53 @@ 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))
)
)
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

0 comments on commit 76f4ab4

Please sign in to comment.