Skip to content

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

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

Closed
bwiernik opened this issue Jun 29, 2021 · 4 comments
Closed

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

bwiernik opened this issue Jun 29, 2021 · 4 comments
Labels
feature a feature request or enhancement layers 📈 themes 💃

Comments

@bwiernik
Copy link
Contributor

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

@clauswilke
Copy link
Member

There's a longstanding PR (#2749) that I believe would provide the features you need (though using a somewhat different approach). It was never completed because there are some difficult technical issues that we couldn't resolve and where any resolution we could think of would cause a breaking change. Hopefully some day we can get this done.

@bwiernik
Copy link
Contributor Author

That would get most the way there, but wouldn't for example, permit the first example there (make the lines of the histogram bars the same color as the panel background)

@thomasp85
Copy link
Member

Agree that it is only partly what you ask for, but it is very akin in spirit and its unlikely we will add one thing without the other... when it may happen is still up in the air, though

@thomasp85 thomasp85 added feature a feature request or enhancement layers 📈 themes 💃 labels Oct 29, 2021
@hadley
Copy link
Member

hadley commented Apr 19, 2022

Closing because I don't think there's much point thinking about it until the first upgrade to theme() is implemented.

@hadley hadley closed this as completed Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement layers 📈 themes 💃
Projects
None yet
Development

No branches or pull requests

4 participants