Skip to content

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

Closed
@aarongraybill

Description

@aarongraybill

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions