Skip to content

Method to detect whether a user has set a global theme #6821

Description

@jack-davison

Hello folks,

Correct me if I'm wrong, but I don't believe there is a way to detect whether a user has a global ggplot2 theme set. The closest I can get, I think, is something like:

user_has_set_theme <- function() {
  !identical(ggplot2::get_theme(), ggplot2::theme_gray())
}

This covers almost all cases, except where the user specifically wants theme_gray()!

My use case is writing a package which produces ggplot2 figures. By default we choose a nice theme that is appropriate for each plot type, but I'm keen that advanced users can escape this default theming if they do have a global theme set. The above user_has_theme_set() function does the job in all cases - e.g., if they set theme_minimal() or some other theme as their global theme - except that where the user really wants to use that default gray theme.

The only discussion I can find on this topic is https://forum.posit.co/t/how-to-check-if-the-user-has-set-a-global-ggplot-theme-with-theme-set-or-not/129162/4 where this "check against theme_grey" is advised.

Cheers!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions