-
Notifications
You must be signed in to change notification settings - Fork 2k
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: set common scale parameters outside of the scale function #4271
base: main
Are you sure you want to change the base?
Conversation
Note to self: The way to fix the priority issue is to add a |
Looks fine to me. For the interface, I prefer something in ggplot(mtcars, aes(disp, mpg, color = hp)) +
geom_point() +
scales_x_auto("hello world", breaks = c(111, 222, 333, 444)) +
scales_colour_auto("test") However, as it's not really a scale, the naming might be a bit confusing. So, I'm not confident if this is better than yours. # Will the color scale of this be viridis? Or should it fall back to the default?
ggplot(mtcars, aes(disp, mpg, color = hp)) +
geom_point() +
scale_colour_viridis_c(option = "C") +
scale_colour_auto() For the implementation, what about creating a placeholder Scale so that we can merge it with the previous scale here? Lines 20 to 37 in b76fa96
Something like: if (any(prev_aes)) {
if (is_placeholder_scale(scale)) {
prev_aes$update_params(scale$params)
return()
}
... |
I hadn't thought about the interface yet. Not sure I understand the idea of the placeholder scale. Are you suggesting to use a new type of |
Oh, I see.
Yes, I don't know yet if my approach is even doable, but it might be a choice. I'll try to make a quick implemention so that we can discuss it further... |
Sounds good. I'm not wedded to my approach, it just seemed the most straightforward to me. |
Okay, here's my proof-of-concept implementation (only for continuous x/y scales): master...yutannihilation:poc/pr-4271-parameter-only-scale The basic idea is that if we treat a scale parameter object as a
But, I found my approach is not very clean. devtools::load_all("~/repo/ggplot2")
#> Loading ggplot2
p <- ggplot(mtcars, aes(disp, mpg, color = hp)) +
geom_point() +
scale_x_auto("hello world", breaks = c(111, 222, 333, 444)) +
scale_x_auto("displacement") +
scale_y_auto("fuel efficiency", expand = expansion(add = 50))
p # Parameters are ignored if a new scale is added
p +
scale_y_continuous("test") Created on 2020-11-22 by the reprex package (v0.3.0) |
Your approach does have the benefit though of not introducing yet another object type with separate logic. Is there anything in the current two proof-of-concept implementations that differs in terms of features? Something my implementation can do that yours can't or vice versa? To handle the cases correctly when explicit scale functions are added, we may have to start adding code to the scale constructors that checks whether arguments are missing or not, and keeps track of which parameters should be replaced based on that. This is the same issue for both approaches. |
I think the ggplot(mtcars, aes(disp, mpg, color = hp)) +
geom_point() +
scale_colour_viridis_c(option = "C") +
scale_colour_settings(name = "horse power") |
The advantage of this approach would be that once we move to a place where scales are settable via themes, the code looks clean again: ggplot(mtcars, aes(disp, mpg, color = hp)) +
geom_point() +
scale_x_settings(limits = c(100, 300)) +
scale_colour_settings(name = "horse power") +
theme_viridis(option = "C") |
It will just be a bit confusing which parameters can and cannot be set via |
Yes. To be fair, this might impose developers some additional care about different types of I don't find any feature differences yet.
I picked
Agreed. For example, if I were a user, I might wonder if I could tweak even the type of the scale: p +
scale_colour_settings(type = "viridis") This is another reason why I chose |
I think the idea to set parameters of scales without committing to any particular one is great. Is there anything I could do to help get this idea moving? |
Hi @clauswilke and @yutannihilation, I'm really liking the direction of this PR. Is there any way in which I could be of any assistance? Apologies for the reminder. |
@teunbrand I think the first question is whether @yutannihilation's approach or my approach is preferable. I don't have a strong opinion one way or another and am not actively thinking about this right now. From my perspective it would be best if you just started your own PR, taking whichever ideas/code from me are useful. |
Same here! |
Here is an attempt at fixing #4269. The PR creates a new
scales_params
object type that can be added to ggplot2 objects to collect parameter values that will be added to scales at plot build time. In this way, we can set properties such as expansion or breaks or labels without knowing which scales object is being used for which aesthetic.Reprex:
library(ggplot2)
Created on 2020-11-19 by the reprex package (v0.3.0)
One current downside is that parameters directly provided to a scale object have lower priority than parameters added previously (see the y expansion in the example). This can be fixed but will add a bit of complication and clumsiness. Not sure if it's worth it but happy to receive feedback.
@yutannihilation @thomasp85 @hadley Any thoughts?