ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Partial margins

Open teunbrand opened this issue 1 year ago • 3 comments

This PR aims to fix #6115.

Briefly it allows NA-units in margin() that get back-filled from parent elements. The function part_margin() is a margin() wrapper that by default has NA-units.

A demonstration. Note no margins except bottom:

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

p <- ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  theme(plot.background = element_rect(linetype = 2, colour = "black", fill = NA))

p + 
  theme(plot.margin = margin(b = 2, unit = "cm")) +
  ggtitle("`margin()`")

Note natural margins are preserved, except bottom:

p + 
  theme(plot.margin = part_margin(b = 2, unit = "cm")) +
  ggtitle("`part_margin()`")

Created on 2024-09-19 with reprex v2.1.1

teunbrand avatar Sep 19 '24 13:09 teunbrand

Great work, @teunbrand

I think it might be helpful for users, if this was named with a margin prefix? This would organise the two functions, so that auto-complete would allow users to easily use the normal one - or switch to the partial one.

So margin_part (or margin_partial)

davidhodge931 avatar Sep 19 '24 21:09 davidhodge931

Thanks David, I like this suggestion :)

teunbrand avatar Sep 20 '24 06:09 teunbrand

Thanks for considering my FR. Looking forward to seeing this implemented. Just wanted to let you know the NEWS file references the wrong issue ID (6155 when it should be 6115).

arnaudgallou avatar Oct 10 '24 16:10 arnaudgallou

margin_auto() that behaves like CSS

I like this idea

teunbrand avatar Oct 29 '24 14:10 teunbrand