-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Enable user-defined theme elements by making element tree part of the theme. #2784
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
Conversation
@hadley @thomasp85 As part of the performance improvements, I'd like to suggest that there's an opportunity for optimization that is related to this PR (though not currently implemented in this PR). The main things this PR does is move the theme validation step to right before plot build, rather than when I'm happy to look into implementing such a theme caching mechanism. The main question is whether we're Ok with not validating themes when |
I didn't review the code, but the overall justification sounds good and I'm happy for this code to be merged (after someone has taken a closer look at it) |
This comment has been minimized.
This comment has been minimized.
One thing Id like to have somehow with a new theme setup is the ability of extension packages to register new theme elements and defaults/inheritance for these during loading. An example is the indicator element in |
This comment has been minimized.
This comment has been minimized.
Agreed... We'll have a go after 3.2.0 |
@thomasp85 I just saw the 3.3.0 milestone. Do we have a feature-freeze date for that milestone? Is it still possible to finalize this PR for that milestone? I know I've been sitting on this for a long time but I should be able to get this done over the next two weeks. |
We don’t have an immediate feature freeze date. If you think you can get this finalised in October/ start November it should be fine |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Can you give an example of using the |
I provided an example at the end of the very first post on this issue, here: Arguably this particular example is contrived. The real use is for packages like ggtern that need to define their own theme elements. For example, let's say somebody writes a package that can create facets with individual captions. Then, they could add a theme element + theme(
facet_caption = element_text(size = 10),
element_tree = list(
facet_caption = el_def("element_text", "text")
) The element tree gets merged into the existing tree, so there's no need to repeat any definitions that are already in the default theme. |
@thomasp85 I made some updates to the PR that make it easier for extension developers to provide functionality that requires new theme elements. As an example, see the reprex below, which implements a new coord that places an annotation into the plot panel and uses a new theme element library(ggplot2)
# define a new coord that adds an annotation label
coord_annotate <- function(label = "panel annotation") {
ggproto(NULL, CoordCartesian,
limits = list(x = NULL, y = NULL),
expand = TRUE,
default = FALSE,
clip = "on",
render_fg = function(panel_params, theme) {
element_render(theme, "panel.annotation", label = label)
}
)
}
df <- data.frame(x = 1:10, y = 1:10)
# doesn't work, element `panel.annotation` hasn't been defined yet
ggplot(df, aes(x, y)) +
geom_point() +
coord_annotate()
#> Theme element `panel.annotation` missing # element can be defined via theme() on the fly
ggplot(df, aes(x, y)) +
geom_point() +
coord_annotate() +
theme(
panel.annotation = element_text(color = "blue", hjust = 0.95, vjust = 0.05),
element_tree = list(panel.annotation = el_def("element_text", "text"))
) # or the default theme can be updated; this would be recommended for
# extension packages
old <- theme_update(
panel.annotation = element_text(color = "blue", hjust = 0.95, vjust = 0.05),
element_tree = list(panel.annotation = el_def("element_text", "text"))
)
ggplot(df, aes(x, y)) +
geom_point() +
coord_annotate() # this approach works also with alternative themes
ggplot(df, aes(x, y)) +
geom_point() +
coord_annotate() +
theme_classic() # reverting back to old theme restores the old element tree
theme_set(old)
ggplot(df, aes(x, y)) +
geom_point() +
coord_annotate() +
theme_classic()
#> Theme element `panel.annotation` missing Created on 2019-10-23 by the reprex package (v0.3.0) |
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.
LGTM
This PR addresses issue #2540, enabling of user-defined theme elements.
I have added the option to change the element tree of the theme through the theme itself. The main change I had to make to make this happen is that I had to move theme validation from the
theme()
function to plot rendering. This means that with this PR thetheme()
function takes pretty much anything without complaint, the error happens only once we have the entire theme for rendering:Created on 2018-07-28 by the reprex package (v0.2.0).
Now, as an example of how this works, we create a new element type and use it instead of
element_text()
for the x axis title:Created on 2018-07-28 by the reprex package (v0.2.0).