@@ -276,39 +276,50 @@ element_grob.element_line <- function(element, x = 0:1, y = 0:1,
276276# ' The function `register_theme_elements()` provides the option to globally register new
277277# ' theme elements with ggplot2. In general, for each new theme element both an element
278278# ' definition and a corresponding entry in the element tree should be provided. See
279- # ' examples for details. For extension package that use this functionality, it is
280- # ' recommended to call `register_theme_elements()` from the `.onLoad()` function.
279+ # ' examples for details. This function is meant primarily for developers of extension
280+ # ' packages, who are strongly urged to adhere to the following best practices:
281+ # '
282+ # ' 1. Call `register_theme_elements()` from the `.onLoad()` function of your package, so
283+ # ' that the new theme elements are available to anybody using functions from your package,
284+ # ' irrespective of whether the package has been attached (with `library()` or `require()`)
285+ # ' or not.
286+ # ' 2. For any new elements you create, prepend them with the name of your package, to avoid
287+ # ' name clashes with other extension packages. For example, if you are working on a package
288+ # ' **ggxyz**, and you want it to provide a new element for plot panel annotations (as demonstrated
289+ # ' in the Examples below), name the new element `ggxyz.panel.annotation`.
281290# ' @param ... Element specifications
282291# ' @param element_tree Addition of or modification to the element tree, which specifies the
283292# ' inheritance relationship of the theme elements. The element tree must be provided as
284293# ' a list of named element definitions created with el_def().
285294# ' @param complete If `TRUE` (the default), elements are set to inherit from blank elements.
286295# ' @examples
287- # ' # define a new coord that includes a panel annotation
296+ # ' # Let's assume a package `ggxyz` wants to provide an easy way to add annotations to
297+ # ' # plot panels. To do so, it registers a new theme element `ggxyz.panel.annotation`
298+ # ' register_theme_elements(
299+ # ' ggxyz.panel.annotation = element_text(color = "blue", hjust = 0.95, vjust = 0.05),
300+ # ' element_tree = list(ggxyz.panel.annotation = el_def("element_text", "text"))
301+ # ' )
302+ # '
303+ # ' # Now the package can define a new coord that includes a panel annotation
288304# ' coord_annotate <- function(label = "panel annotation") {
289305# ' ggproto(NULL, CoordCartesian,
290306# ' limits = list(x = NULL, y = NULL),
291307# ' expand = TRUE,
292308# ' default = FALSE,
293309# ' clip = "on",
294310# ' render_fg = function(panel_params, theme) {
295- # ' element_render(theme, "panel.annotation", label = label)
311+ # ' element_render(theme, "ggxyz. panel.annotation", label = label)
296312# ' }
297313# ' )
298314# ' }
299315# '
300- # ' # register a new theme element `panel.annotation`
301- # ' register_theme_elements(
302- # ' panel.annotation = element_text(color = "blue", hjust = 0.95, vjust = 0.05),
303- # ' element_tree = list(panel.annotation = el_def("element_text", "text"))
304- # ' )
305- # '
316+ # ' # Example plot with this new coord
306317# ' df <- data.frame(x = 1:3, y = 1:3)
307318# ' ggplot(df, aes(x, y)) +
308319# ' geom_point() +
309320# ' coord_annotate("annotation in blue")
310321# '
311- # ' # revert to original ggplot2 settings
322+ # ' # Revert to the original ggplot2 settings
312323# ' reset_theme_settings()
313324# ' @keywords internal
314325# ' @export
0 commit comments