Skip to content

Conversation

@teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Jan 5, 2024

This PR aims to fix #5622.

Briefly, this PR adds spacing and margins theme elements that act as the 'root' element for other spacing and margin settings in the plot. For example, the legend.box.spacing inherits from spacing and legend.margin inherits from margins. The default looks of themes are preserved.

As a detail, functions like theme_gray() already sort of applied the same logic as this PR applies via the half_line variable. This PR implicitly ensures that the half_line variable also gets stored in the spacing element.

Regretfully, I could not find an elegant way to have text elements inherit from the margins element, so these remain as-is.

I also made the legend.ticks.length inherit from legend.key.size. Currently legend.ticks.lenght was set by default to "npc" units, but that might have no clear meaning depending on the type of legend. Inheriting from the key size preserves the look but is more clear what the length will be without knowing the internal detail that the 'npc' units relate to the dimensions of a particular cell in the gtable.

Some examples.

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

p <- ggplot(mpg, aes(displ, hwy, colour = factor(cyl))) +
  geom_point() +
  facet_grid(~ drv)

p + theme(spacing = unit(1, "cm"))

p + theme(margins = margin(1, 1, 1, 1, "cm"))

Created on 2024-01-05 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.

LGTM

teunbrand added 2 commits May 20, 2024 11:32
Merge branch 'main' into theme_spacing

# Conflicts:
#	R/theme-defaults.R
#	R/theme-elements.R
#	R/theme.R
#	man/theme.Rd
@teunbrand teunbrand merged commit b3d5071 into tidyverse:main May 20, 2024
@teunbrand teunbrand deleted the theme_spacing branch May 20, 2024 09:54
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.

Feature request: spacing and margins as 'root' theme elements

2 participants