Skip to content

Feature request: (sub)theme argument for guide_legend() and friends #5348

Closed
@teunbrand

Description

@teunbrand

Of the 21 arguments that guide_legend() has, more than half (11) are proxies for theme settings.

  • title.theme proxies theme(legend.title)
  • title.hjust proxies theme(legend.title = element_text(hjust))
  • title.hjust proxies theme(legend.title = element_text(vjust))
  • label proxies theme(legend.text = element_text/blank())
  • label.theme proxies theme(legend.text = element_text())
  • label.hjust proxies theme(legend.text = element_text(hjust))
  • label.vjust proxies theme(legend.text = element_text(vjust))
  • keywidth/barwidth proxies theme(legend.key.width)
  • keyheight/barheight proxies theme(legend.key.height)
  • direction proxies theme(legend.direction).
  • default.unit is just syntactic sugar for the keywidth and keyheight arguments.

These are great for customising one legend in particular. However, it is a bit of a burden on the code to have this messy, manual layer of inheritance underneath the regular theme's inheritance. In addition, there are also theme settings that apply to all legends at once and cannot be customised per legend, even though you might want to.

You can set the following in theme() but not at the legend level:

  • legend.background
  • legend.margin
  • legend.spacing(.{x/y})
  • legend.key

As an aside, there is also the inverse case where styling options are provided in the guide_*() function but not in the theme:

  • guide_colourbar(frame, frame.colour, frame.linewidth, frame.linetype, ticks, ticks.colour, ticks.linewidth, ticks.length)
  • same for guide_coloursteps()
  • guide_bins(axis.colour, axis.linewidth, axis.arrow, ticks, ticks.length)

Adding these "missing" theme settings to guides makes their formals even more complicated than they already are.

So I'm proposing an alternative: get rid of all (or most of) these theme arguments in legends, and instead add one subtheme (or just theme) argument to guides that takes a (incomplete) theme object. This then:

  1. Adds the flexibility of using the "missing" theme arguments on a per-guide basis, hassle free.
  2. Reduces the complexity of arguments to guide_*() functions.
  3. Makes for better maintainable code, as one could simply do actual_theme <- plot_theme + subtheme once instead of meticulously curating inheritance.

Moreover, it would synergise well with the syntactic sugar of #5301, where we then could specify subtheme = theme_legend(...) and be spared the hassle to write out all the theme arguments in full.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions