-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
WIP: Register default scales via themes #3973
Conversation
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()) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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"), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()
).
Apologies, my picture is evolving as I'm thinking about this: I don't think you should modify |
Thanks, yeah I figured |
@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. |
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. |
Resolve #2691
Add
default.scales
component of themes.