Skip to content
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

<rel> and <unit> theme inheritance #5403

Merged
merged 7 commits into from
Sep 12, 2023
Merged

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #3951.

Essentially, anywhere one can specify a unit() in the theme for an element that is not a root element, one can now use rel() to inherit from the parent unit object with modification.

In the example below:

  • Legend keys are 2x as long as they're wide
  • Top x-axis ticks are 2x as long as default
  • Bottom x-axis ticks are 4x as long as default, because inheritance passes through 2 <rel> objects.
devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

ggplot(mpg, aes(displ, hwy, colour = factor(cyl))) +
  geom_point() +
  guides(x.sec = "axis") +
  theme(
    legend.key.height = rel(2),
    axis.ticks.length.x = rel(2),
    axis.ticks.length.x.bottom = rel(2)
  )

Created on 2023-08-30 with reprex v2.0.2

@thomasp85 thomasp85 self-requested a review September 12, 2023 09:17
@teunbrand
Copy link
Collaborator Author

As a note: it also works on margin()s but I don't think any theme margin settings inherit something.

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2
combine_elements(rel(2), margin(5,5,5,5))
#> [1] 10points 10points 10points 10points

Created on 2023-09-12 with reprex v2.0.2

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor thing but otherwise good to go

R/theme.R Outdated Show resolved Hide resolved
@teunbrand teunbrand merged commit 67bb3bb into tidyverse:main Sep 12, 2023
11 of 12 checks passed
@teunbrand teunbrand deleted the rel_inheritance branch September 12, 2023 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggestion: rel-class can inherit value from unit-class theme elements
2 participants