-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Drop plot_env from ggplot2 objects? #3994
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
Comments
Setting library(ggplot2)
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point() +
facet_wrap(~Species)
p$plot_env <- NULL
p
#> Error in exists(name, envir = env, mode = mode): use of NULL environment is defunct
p$plot_env <- rlang::new_environment()
p Created on 2020-05-13 by the reprex package (v0.3.0) It looks like the other place where the environment is used is for scale lookup. Maybe it would be better to use the global environment that is active when the plot is printed, rather than when the plot is created? That would be more predictable anyways, I assume. Lines 28 to 39 in 5a686c3
|
On the last point, just setting library(ggplot2)
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point() +
facet_wrap(~Species)
scale_colour_discrete <- scale_colour_viridis_d
p p$plot_env <- rlang::new_environment()
p Created on 2020-05-13 by the reprex package (v0.3.0) |
Thanks for investigating this. I didn't know the mechanism around At first, ggplot2 only looks up in global environment, but started to check the package environment by this commit: Then, I agree it's nice if we can get rid of library(ggplot2)
do_plot <- function() {
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point() +
facet_wrap(~Species)
scale_colour_discrete <- scale_colour_viridis_d
p
}
p <- do_plot()
p p$plot_env <- rlang::new_environment()
p Created on 2020-05-14 by the reprex package (v0.3.0) |
I get that, but the question is do we want to encourage this usage pattern? Does anybody use it? Is it even logical? See the next example. Would you have expected the plot to use the brewer color scale? library(ggplot2)
do_plot <- function() {
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point() +
facet_wrap(~Species)
scale_colour_discrete <- scale_colour_viridis_d
p
}
p <- do_plot()
scale_colour_discrete <- scale_colour_brewer
p Created on 2020-05-14 by the reprex package (v0.3.0) My sense is that using the environment that is active when the plot is being printed is totally reasonable, and that doesn't require saving the environment in the plot itself. I also feel that if people write functions that return plots, then they should explicitly add the scales they want. |
I really don't know, sorry. I didn't even know (or completely forget) that we can override the default scale in such a way... I think your claim is 99% valid, but I just don't see the context why ggplot2 needed to be changed to use parent environment instead of global environment. |
By the way, is this related to #2691? If we can set the default scale by setting the default theme, probably we don't need to tweak globals (or some environments) anymore. |
I have a vague recollection it was something to do with scale customisation. Maybe so that you could write your |
I’m pretty sure that was the reason. I’m all for a better way of allowing users to set a default scale |
Sure, but the question is: Is it sufficient to look either in the global environment or the parent environment of the In other words, in the reprex in #3994 (comment), would it be fine if the color scale being used upon printing was the brewer scale rather than the viridis scale? |
I think if we went ahead and nuked the plot_env we might as well come up with a better solution for setting scale defaults than looking for specifically named functions |
That creates a whole new set of problems, though. (#3962 (comment)) I should probably create an issue just for that. We will need some mechanism to override breaks, labels, expansion, etc. without adding a new scale function. |
After implementing the
It appears that quosures within the |
I don't think so. Quosures capture environments in nature, and layers are environment itself. @clauswilke |
That's correct. I think saving plots on disk is a fundamentally flawed workflow. I don't think it will ever work properly, and I don't see any good reason to do so in the first place. |
Thanks, I totally agree with you. |
Btw, as #3828 gets merged, I guess overriding
|
Just to be clear: I think there are two separate issues here. One is how to globally set color scales. The other is whether any scales (not just color) should be searched in the environment that was active when the plot was defined or in the current global environment. I think the latter choice (search in the current global environment, thus keeping a copy of the environment is not needed) is fine. |
I see. Sorry for my confusion. |
One use case is to avoid having to redraw large and slow plots. If I can saveRDS the ggplot list object say then I can see whether it has changed and whether I need to redraw the entire plot. Also I can track this with git and avoid having to add large plots to my git repo. However, if I understand the above discussion, it's not that simple, since the entire R environment is potentially accessed during drawing; changes to the R env may or may not affect the drawn plot; not all the information is contained in the ggplot list. |
The use case we handle with this is a Shiny app that allows users to download a plot in a format such that it can easily be "reprinted" later. Different scientific journals may have different requirements regarding dimensions, fonts, etc., and we believe storing an We certainly improved things by replacing |
@bersbersbers this only works by coincidence and not by design. We make no guarantees about the ggplot2 internals and making it possible replaying saved plots from previous versions of ggplot2 would be a huge amount of work that we do not plan to do. |
This comment #3619 (comment) prompted me to look into
plot_env
, and as far as I can see it's stored and handed around just so it can be eventually given to the functioncombine_vars()
, which then doesn't use it:ggplot2/R/facet-.r
Lines 544 to 587 in e9b9946
Is this a hold-over from the times before tidy eval? Can we remove this/or assign
NULL
here?ggplot2/R/plot.r
Line 96 in 5a686c3
It is already marked as deprecated in the documentation:
ggplot2/R/plot.r
Line 35 in 5a686c3
The text was updated successfully, but these errors were encountered: