Skip to content
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

Invisibly return previous defaults when using update_(geom/stat)_default #5616

Closed
aarongraybill opened this issue Dec 28, 2023 · 3 comments
Closed

Comments

@aarongraybill
Copy link

Requested Feature

I think the outgoing default aesthetic should be returned invisibly when the user runs update_geom_default. The syntax could look something like:

library(ggplot2)
# invisibly capture previous state of defaults
old_aes <- update_geom_defaults("Point", aes(colour="red"))

# old_aes should essentially be the output of GeomPoint$default_aes before the previous line was run

# easily reset to previous state
update_geom_defaults("Point",old_aes)

Justification

update_geom_default and update_stat_default are useful for applying consistent styles across multiple plots. However, there is (to my knowledge) no easy to way to reset all of the geometries and stats to their original aesthetics. For themes we have reset_theme_settings which returns the current theme to the original theme_gray. What's nice about reset_theme_settings is that the user does not need to know what the actual default values of theme_gray are. I believe similar functionality should be available for the modifications to Geom default aesthetics.

Currently, the user would have do something like this to safely reset the default aesthetic:

library(ggplot2)

old_aes <- GeomPoint$default_aes
update_geom_defaults("Point",aes(colour="red"))
ggplot(cars,aes(x=speed,y=dist))+geom_point()

update_geom_defaults("Point",old_aes)
ggplot(cars,aes(x=speed,y=dist))+geom_point()

Created on 2023-12-28 with reprex v2.0.2

And while that definitely works, it requires the user to learn about the Geoms, and the extra line of code feels like it introduces a degree of separation that's not necessary

Implementation

The current version of update_geom_default and update_stat_default (as of 04a5ef2, code available here) appear to mostly have this functionality built in. The functions already create variables called old <- g$default_aes which store the outgoing default values, and then the functions end with invisible() (so not returning anything). I believe, swapping those lines out with invisible(old) would implement this suggestion. I would be happy to submit a PR if the implementation is as such.

@teunbrand
Copy link
Collaborator

Thanks Aaron for this suggestion. I agree with you that update_{geom/stat}_defaults() should return their previous value. This has been suggested before in #4993 (comment). The suggestion is also included in #5098. The PR currently awaits review.

@aarongraybill
Copy link
Author

Great thanks! Looking forward to this feature.

@teunbrand
Copy link
Collaborator

I'm closing this issue in favour of #4993.

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

No branches or pull requests

2 participants