Skip to content

WIP: Register default scales via themes - #3973

Closed
alanocallaghan wants to merge 5 commits into
tidyverse:mainfrom
alanocallaghan:2691-default-scales-via-themes
Closed

alanocallaghan wants to merge 5 commits into
tidyverse:mainfrom
alanocallaghan:2691-default-scales-via-themes

Conversation

@alanocallaghan

Copy link
Copy Markdown

Resolve #2691

Add default.scales component of themes.

Comment thread R/scale-type.R Outdated
# 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()) {

@clauswilke clauswilke Apr 29, 2020 •

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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.

Comment thread R/scale-type.R Resolved
Comment thread 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
Copy Markdown
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.

Comment thread R/scale-type.R Resolved
Comment thread R/scale-type.R Resolved
Comment thread R/scale-type.R Resolved
Comment thread R/plot-build.r
out
}

plot$theme <- plot_theme(plot)

Copy link
Copy Markdown
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.

Comment thread R/theme-elements.r
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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