Skip to content

<rel> and <unit> theme inheritance - #5403

Merged
teunbrand merged 7 commits into
tidyverse:mainfrom
teunbrand:rel_inheritance
Sep 12, 2023
Merged

teunbrand merged 7 commits into
tidyverse:mainfrom
teunbrand:rel_inheritance

Conversation

@teunbrand

Copy link
Copy Markdown
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
Copy Markdown
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

@thomasp85 thomasp85 left a comment

Copy link
Copy Markdown
Member

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

Comment thread R/theme.R Resolved
@teunbrand
teunbrand merged commit 67bb3bb into tidyverse:main Sep 12, 2023
@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