-
Notifications
You must be signed in to change notification settings - Fork 117
Deep-dive blogpost about ggplot2 themes #745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
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") | ||
) | ||
``` |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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") | ||
) | ||
``` |
There was a problem hiding this comment.
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() | ||
``` |
There was a problem hiding this comment.
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?
As discussed with @thomasp85, this blogpost is intended as a deep-dive into ggplot2 styling, covering much of
theme()
including new functionality.