Skip to content

Commit

Permalink
geom_contour axis ranges fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
rdboyes committed Apr 2, 2024
1 parent 467cec8 commit c56c900
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 34 deletions.
39 changes: 9 additions & 30 deletions src/draw.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ function Makie.SpecApi.Axis(plot::GGPlot)
# make a master list of all possible accepted optional aesthetics and args
ggplot_to_makie_geom = merge(ggplot_to_makie, geom.special_aes)

# which optional aesthetics were given?
optional_aes_given = [k for (k, v) in aes_dict if !(k in required_aes)]
visual_optional_aes = Dict{Symbol, Any}()
# which aesthetics were given?
given_aes = Dict{Symbol, PlottableData}()

for a in optional_aes_given
for a in keys(aes_dict)
# the name of the aes is translated to the makie term if needed
aes = haskey(ggplot_to_makie_geom, a) ? Symbol(ggplot_to_makie_geom[a]) : Symbol(a)

Expand All @@ -74,12 +73,12 @@ function Makie.SpecApi.Axis(plot::GGPlot)
end

# if the transform has a label associated with it, pass that into axis_options
if !isnothing(plottable_data[aes].label_target)
if haskey(plottable_data, aes) && !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[aes].makie_function(plottable_data[aes].raw))
merge!(given_aes, plottable_data)
end

# which ones were given as arguments?
Expand All @@ -103,32 +102,12 @@ function Makie.SpecApi.Axis(plot::GGPlot)
end
end
end

# make a Tuple that contains the columns from the data in their required order to pass to PlotSpec
visual_args_list = []

for a in required_aes
aes = Symbol(a)
# 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)
source_cols = [aes_dict[String(source)] for source in geom.column_transformations[aes][1]]
plottable_data = geom.column_transformations[aes][2](aes, source_cols, plot_data)
elseif eltype(plot_data[!, aes_dict[a]]) <: Union{AbstractString, AbstractChar}
plottable_data = cat_inseq(aes, [aes_dict[a]], plot_data)
else
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].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].makie_function(plottable_data[aes].raw))
end
required_aes_data = [p.makie_function(p.raw) for p in [given_aes[a] for a in Symbol.(required_aes)]]
optional_aes_data = [a => p.makie_function(p.raw) for (a, p) in given_aes if !(String(a) in required_aes)]

args = Tuple([geom.visual, visual_args_list...])
kwargs = merge(args_dict_makie, visual_optional_aes)
args = Tuple([geom.visual, required_aes_data...])
kwargs = merge(args_dict_makie, Dict(optional_aes_data))

# push completed PlotSpec (type, args, and kwargs) to the list of plots

Expand Down
9 changes: 7 additions & 2 deletions src/geoms/geom_contour.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
function stat_density_2d(aes_dict::Dict{String, Symbol},
args_dict, required_aes::Vector{String}, plot_data::DataFrame)

new_required_aes = ["z"]
new_required_aes = ["x", "y", "z"]

aes_dict["z"] = :z

return (aes_dict, args_dict, new_required_aes, plot_data)
end

geom_tile = geom_template("geom_tile", ["x", "y", "z"], :Heatmap)
geom_contour = geom_template("geom_contour", ["x", "y"], :Contour;
aes_function = stat_density_2d,
column_transformations = Dict{Symbol, Pair{Vector{Symbol}, Function}}(:z => [:x, :y] => kernel_density_2d))
column_transformations = Dict{Symbol, Pair{Vector{Symbol}, Function}}(
:x => [:x]=>discard,
:y => [:y]=>discard,
:z => [:x, :y]=>kernel_density_2d))
12 changes: 10 additions & 2 deletions src/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,18 @@ function kernel_density_2d(target::Symbol, source::Vector{Symbol}, data::DataFra

k = kde((data[!, source[1]], data[!, source[2]]))

return Dict{Symbol, PlottableData}(
target => PlottableData(k.density,
return_dict = Dict{Symbol, PlottableData}(
:x => PlottableData(k.x, identity, nothing, nothing),
:y => PlottableData(k.y, identity, nothing, nothing),
:z => PlottableData(k.density,
identity,
nothing,
nothing)
)

return return_dict
end

function discard(target::Symbol, source::Vector{Symbol}, data::DataFrame)
return Dict{Symbol, PlottableData}()
end
1 change: 1 addition & 0 deletions test/test_geoms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@
plots = [
Makie.PlotSpec(
:Contour,
k.x, k.y,
k.density
)
]
Expand Down

2 comments on commit c56c900

@rdboyes
Copy link
Member Author

@rdboyes rdboyes commented on c56c900 Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/104099

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.1 -m "<description of version>" c56c900251adc1803fcad33e798527430cc45e0c
git push origin v0.6.1

Please sign in to comment.