Closed dgkf closed 4 years ago
Hi @cttobin, I haven't seen any follow up regarding this pull request. Please let me know if there's any concern over the the changes.
I've substantially modified the package further in my fork and would love to get your thoughts on those changes as well, but would like to make sure this is in line with your views before moving any further down that path.
Most notably, I've changed the way that ggplot2
exported functions are masked, and instead now backup the ggplot2
namespace within ggthemr
, then modify the functions within the attached ggplot2
namespace. I know this is a radically different approach and might disregard some best practices, but it avoids a bloated global namespace.
Gradient specification improvements
The existing method of specifying a gradient only allows arguments passed to
scale_*_gradient
which accepts two colors for gradient low and high. However, it's often desirable to usescale_*_gradient2
for divergent gradients orscale_*_gradientn
(for example a rainbow gradient) for multi-color gradients as the default forscale_*_continuous
. To accommodate this, I've updated thedefine_palette
function to accept a list of arguments which is used to select the appropriatescale_*
function.Here are some example use cases to highlight the new functionality:
define_palette(gradient = c("#FF0000", "#00FF00"))
as before, if a character vector of length 2 is provided, it will create gradients spanning from the first value to the last (in practice, it now usesscale_*_gradientn
, but is functionally identical)define_palette(gradient = c("#FF0000", "#00FF00", "#0000FF")
a benefit of usingscale_*_gradientn
by default it can now scale to more than 2 values, which will result in an equal-spacing gradient using all values.define_palette(gradient = list(low = "#FF0000", high = "#00FF00"))
if a list is provided, it first checks to see if the arguments can satisfy any of thescale_*
functions, and then tries to smartly select the appropriate one.list(colours = <character vector>)
scale_*_gradientn
is usedscale_*_gradient2
is usedscale_*_gradient
is usedThis gives a lot more flexibility to the gradient specification, and preserves backwards compatibility with the existing character vector input.
Non-aesthetic Geom Defaults
Additionally, I added handling for overwriting non-aesthetic defaults. One example where I repeatedly want to do this is for overwriting the default behavior of
geom_bar
to useposition = position_dodge()
by default.This isn't possible using
update_geom_defaults
as it's not an aesthetic parameter, so it creates a copy of the function in the target environment with the formal argument defaults set to the specified values. I added a new argument,geom_updates
which expects a list with named values for eachgeom_*
function to update with values as an updated argument list.In practice, it looks like this:
This will use
update_geom_defaults
to set thealpha
value forgeom_bar
and will create a copy ofgeom_bar
in the target environment withposition
set with a new default. Ifggthemr
is called again without updates togeom_bar
, it will removegeom_bar
from the target environment and revert back to theggplot2
default.