tidyverse / tidyverse/ggplot2

Method to detect whether a user has set a global theme

Open
#6,821 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

themes :dancer:
Dominant language
R
Stars
7k
Forks
2.1k
Avg merge
59m
Merged PRs (30d)
2

Description

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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining the behavior of get_theme(), theme_gray(), theme_minimal(), and theme_set() in the ggplot2 codebase. Reproduce the case where a user explicitly sets theme_gray() and determine what observable state distinguishes it from the default; done means a supported detection method handles both that case and other global themes.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.