TidierOrg / TidierPlots.jl

Tidier data visualization in Julia, modeled after the ggplot2 R package.
MIT License
196 stars 7 forks source link

`inherit.aes` option in geoms #96

Closed rdboyes closed 2 months ago

rdboyes commented 2 months ago

Describe how it works in R's ggplot2 inherit.aes is true by default, and when set to false, it prevents the geom from inheriting the plot default aes.

Describe any changes you think should be made to R's version None

kdpsingh commented 2 months ago

You can't have periods in Julia argument names so may need to be inherit_aes.

adknudson commented 2 months ago

One way this can be achieved is to add another field to Geom called kwargs and it basically acts as a sink for all other keywords passed into the geom. A basic example looks like this

struct Geom{K}
    f1::Type1
    f2::Type2
    ....
    kwargs::K
end

Then in the geom_function of geom_template, pass kwargs on to the build_geom function:

return build_geom(aes_dict, args_dict, 
    required_aes,
    spec_api_function,
    aes_function, 
    merge(transforms, column_transformations),
    kwargs)

Finally you can define a method that checks if kwargs has the inherit_aes keyword

inherit_aes(geom::Geom) = get(geom.kwargs, :inherit_aes, true)

This let's you keep the default inherit_aes=true while letting users pass in the option to turn it off. Additionally this sets you up to support other keywords in the future.

adknudson commented 2 months ago

If you don't want to add a type constraint on kwargs, then you can convert it to a Dict{Symbol, Any} and have that as a concrete type in the struct

rdboyes commented 2 months ago

Thanks @adknudson - I implemented a modified version of your suggestion, since geoms were already automatically absorbing any extra kwargs into their args_dict. In dev:

ggplot(penguins, aes(x = :bill_length_mm, y = :bill_depth_mm)) + 
           geom_point() +
           geom_hline(yintercept = 18, inherit_aes = false)

image