Skip to content

.theme pronoun for accessing theme elements (e.g., colors) #4535

@bwiernik

Description

@bwiernik

In a package I'm developing, I would occasionally like to set geom parameters, such as colors, to have the same values as theme elements. For example, to use the same color for geom_line() as for the axis lines or the panel background. I would like to be able to set this while still allowing the theme to be changed later, with the geom parameters changing to match. It would be really helpful to have a .theme pronoun that could refer to whatever theme is set for a plot.

Example:

library(ggplot2)

# set line colors to match panel backgound
ggplot(mtcars) +
  aes(x = mpg) +
  geom_histogram(
    color = .theme$panel.background$fill
    # currently must be:
    # color = theme_bw()$panel.background$fill
  ) +
  theme_bw()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# set line color to match theme line colors
ggplot(mtcars) +
  aes(x = disp, y = mpg) +
  geom_point() +
  geom_smooth(
    se = FALSE, 
    color = .theme$line$colour
    # currently must be:
    # color = ggthemes::theme_economist()$line$colour
  ) +
  ggthemes::theme_economist()
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Potentially related to #2691

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