Skip to content

margin_part seems not to work in legend.box.margin yet #6536

Closed
@Breeze-Hu

Description

@Breeze-Hu

Hi, Teun!

I was trying to adjust the distance of the legend part from the left border of the plot when I accidentally discovered that margin_part() seems not to work like margin() now:

library(ggplot2)

ggplot(iris, aes(Species, Sepal.Length, color = Species)) +
  geom_boxplot() +
  theme(legend.position = 'bottom',
        legend.box.background = element_rect(color = 'red'),
        legend.justification = c(0.3,1),
        legend.box.margin  = margin(t = 0, r = 0, b=0, l = 30, unit = 'pt'))

  • The left line of the legend box also does not match the initial width. (This is of course not the core problem)

But trying a partial adjustment to use margin_part() instead of margin() reported an error:

ggplot(iris, aes(Species, Sepal.Length, color = Species)) +
  geom_boxplot() +
  theme(legend.position = 'bottom',
        legend.box.background = element_rect(color = 'red'),
        legend.justification = 'left',
        legend.box.margin  = margin_part(t = NA, r = NA, b=NA, l = 30, unit = 'pt'))
#> Error in `plot_theme()`:
#> ! Can't merge the `legend.box.margin` theme element.
#> Caused by error in `as.unit()`:
#> ! object is not coercible to a unit

Created on 2025-06-28 with reprex v2.1.1

As I know you are busy with the issue of reverse dependencies for the next version update, THANK YOU FOR YOUR WORK!
Hope this doesn't disturb you.

Best wishes,
Hu

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions