Skip to content

Should theme inheritance prefer the 'richer' element? #5457

@teunbrand

Description

@teunbrand

Sometimes, one thinks that one has set a theme element, but it turns out some downstream theme element overrides your settings.
In the example below, I want the markdown element to display the x-axis title in bold. However, it doesn't because axis.title.x already is a defined theme element in the default theme, which overrules the element_markdown().

library(ggplot2)

p <- ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  labs(x = "**displacement**")

p + theme(axis.title = ggtext::element_markdown())

Of course, there are plenty of ways to display the axis title in bold, such as setting a more appropriate theme element.

p + theme(axis.title.x = ggtext::element_markdown())

Since <element_markdown> is a subclass of <element_text>, I think it would be appropriate that the calculation below returns an <element_markdown> object.

calc_element(
  "axis.text.x.top",
  theme_get() + theme(axis.text = ggtext::element_markdown(colour = "red"))
)
#> List of 11
#>  $ family       : chr ""
#>  $ face         : chr "plain"
#>  $ colour       : chr "red"
#>  $ size         : num 8.8
#>  $ hjust        : num 0.5
#>  $ vjust        : num 0
#>  $ angle        : num 0
#>  $ lineheight   : num 0.9
#>  $ margin       : 'margin' num [1:4] 0points 0points 2.2points 0points
#>   ..- attr(*, "unit")= int 8
#>  $ debug        : logi FALSE
#>  $ inherit.blank: logi TRUE
#>  - attr(*, "class")= chr [1:2] "element_text" "element"

Created on 2023-10-05 with reprex v2.0.2

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