Skip to content

WIP: Register default scales via themes #3973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed

WIP: Register default scales via themes #3973

wants to merge 5 commits into from

Conversation

alanocallaghan
Copy link

Resolve #2691

Add default.scales component of themes.

R/scale-type.R Outdated
@@ -25,7 +24,10 @@ find_scale <- function(aes, x, env = parent.frame()) {
# Look for object first in parent environment and if not found, then in
# ggplot2 namespace environment. This makes it possible to override default
# scales by setting them in the parent environment.
find_global <- function(name, env, mode = "any") {
find_global <- function(name, env, mode = "any", theme_ = theme()) {
Copy link
Member

@clauswilke clauswilke Apr 29, 2020

Choose a reason for hiding this comment

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

We don't normally use argument names ending in _, as in theme_. Does theme = theme() not work here?

Copy link
Author

Choose a reason for hiding this comment

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

I got promise already under evaluation, otherwise I'd have gone with that. I can do theme=ggplot2::theme() instead. See reprex:

x <- "Hello world"
f <- function(get = get("x")) {
  print(get)
}
f()
#> Error in print(get): promise already under evaluation: recursive default argument reference or earlier problems?

Copy link
Member

Choose a reason for hiding this comment

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

As I commented elsewhere, just write theme = NULL.

R/scale-type.R Outdated
@@ -1,4 +1,4 @@
find_scale <- function(aes, x, env = parent.frame()) {
find_scale <- function(aes, x, env = parent.frame(), theme) {
Copy link
Member

Choose a reason for hiding this comment

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

I think theme should have a default here. I think theme = NULL makes sense.

@@ -44,6 +44,8 @@ ggplot_build.ggplot <- function(plot) {
out
}

plot$theme <- plot_theme(plot)
Copy link
Member

Choose a reason for hiding this comment

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

This should be commented, just like the other parts of the code in this function.

@@ -480,6 +480,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
strip.placement.y = el_def("character", "strip.placement"),
strip.switch.pad.grid = el_def("unit"),
strip.switch.pad.wrap = el_def("unit"),
default.scales = el_def("list"),
Copy link
Member

Choose a reason for hiding this comment

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

It's probably best to add a specific element class here. PR #2749 adds element_geom(), so maybe this one here could be called element_scales(). But I'd like to have input from @yutannihilation or @thomasp85 here.

Copy link
Member

Choose a reason for hiding this comment

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

Also, if we add a specific element class then we check then and there that the element values are functions and then we can avoid the mode() check in find_global() (to be moved into find_scale()).

@clauswilke
Copy link
Member

Apologies, my picture is evolving as I'm thinking about this: I don't think you should modify find_global(). That makes no sense. Instead, move the theme lookup code into find_scale(). If find_scale() doesn't find an appropriate scale in the theme, then it can try the global lookup with the unmodified find_global(). Makes much more sense.

@alanocallaghan
Copy link
Author

Thanks, yeah I figured find_global was the wrong place entirely. find_scale makes sense, there's no reason to look in the theme in general when doing lookup.

@clauswilke
Copy link
Member

@alanocallaghan I realized there's a different issue and it suggests that this PR may be premature. We don't have a good way of feeding additional parameters into an existing scale, such as breaks, break labels, or expansion parameters. Without this mechanism, setting a default scale in a theme has limited utility and can actually cause confusion, when people want to set the breaks and cannot do this while also retaining the color scheme their theme defines. Issue #3962 is related and may have to be resolved first.

@baptiste It seems to me that you're proposing an entirely new way of defining scales. Your proposal may make sense, but I'd like to see a proof-of-concept implementation to really understand what exactly the proposal is and how it'd interact with all the currently existing ggplot2 code. I'd also want to make sure that it works for shape, size, alpha, etc. scales in addition to color scales.

@alanocallaghan
Copy link
Author

Alright no worries, this isn't a particularly urgent thing for me - just something I had noticed and fancied playing with. Let me know if you get a handle on how that might progress - happy to lend a hand there too if needed. Otherwise ping this when it's ready to progress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Register default scales via themes
2 participants