Mikata-Project / ggthemr

Themes for ggplot2.
886 stars 107 forks source link

More general palette gradient specification & non-aesthetic geom defaults #34

Closed dgkf closed 4 years ago

dgkf commented 5 years ago

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 use scale_*_gradient2 for divergent gradients or scale_*_gradientn (for example a rainbow gradient) for multi-color gradients as the default for scale_*_continuous. To accommodate this, I've updated the define_palette function to accept a list of arguments which is used to select the appropriate scale_* function.

Here are some example use cases to highlight the new functionality:

This 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 use position = 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 each geom_* function to update with values as an updated argument list.

In practice, it looks like this:

ggthemr(
  geom_updates = list(
    geom_bar = list(
      position = position_dodge(width = 0.8),
      alpha = 0.9)))

This will use update_geom_defaults to set the alpha value for geom_bar and will create a copy of geom_bar in the target environment with position set with a new default. If ggthemr is called again without updates to geom_bar, it will remove geom_bar from the target environment and revert back to the ggplot2 default.

dgkf commented 5 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.