Skip to content

Conversation

teunbrand
Copy link
Contributor

As discussed with @thomasp85, this blogpost is intended as a deep-dive into ggplot2 styling, covering much of theme() including new functionality.

Comment on lines +298 to +311
In addition to these elements in ggplot2, extension packages can also define custom elements.
Generally speaking, these elements are variants of the elements listed above and often have slightly different properties and are rendered differently.
For example `ggtext::element_markdown()` is a subclass of `element_text()`, but interprets the provided text as markdown.
It applies some html tags like `<b>` for bold and `<i>` for italic when rendering the text.
Another example is `ggh4x::element_part_rect()` that can draw a subset of rectangle borders.

```{r element_markdown}
p +
labs(title = "<b>Fuel</b> <i>efficiency</i>") +
theme(
plot.title = ggtext::element_markdown(),
strip.background = ggh4x::element_part_rect(colour = "black", side = "b")
)
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use marquee to avoid recommending a package we consider superseded

Travelling from roots to leaves, properties of theme elements are inherited from parent to child.
Some inheritance is very direct, where leaves directly inherit from roots (for example `legend.text`).
Other times, inheritance is more arduous, like for `axis.minor.ticks.y.left`: it inherits from `axis.ticks.y.left`, which inherits from `axis.ticks.y`, which inherits from `axis.ticks`, which finally inherits from `line`.
Most often, elements only have a single parent, but there are exceptions so the inheritance of theme elements is not strictly a directed acyclic graph.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not really true. A DAG can have multiple inheritance as long as it is not cyclic. So the theme is a DAG but not a tree


The `?theme` documentation often tells you how the elements inherit and `calc_element()` will resolve it for you.
If, for some reason, you need programmatic access to the inheritance tree, you can use `get_element_tree()`.
Let's say you want to find out exactly why theme inheritance is not a directed acyclic graph.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same DAG nitpick as above

text = element_text(margin = margin(l = 6, unit = "mm")),
margin = margin(l = 6, unit = "mm")
)
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe mention here how you can make the bar stretch the full height/width of the plot

}

p + my_theme()
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe mention the requirement to use system fonts-compatible graphics devices?

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.

2 participants