@@ -276,39 +276,50 @@ element_grob.element_line <- function(element, x = 0:1, y = 0:1,
276
276
# ' The function `register_theme_elements()` provides the option to globally register new
277
277
# ' theme elements with ggplot2. In general, for each new theme element both an element
278
278
# ' 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`.
281
290
# ' @param ... Element specifications
282
291
# ' @param element_tree Addition of or modification to the element tree, which specifies the
283
292
# ' inheritance relationship of the theme elements. The element tree must be provided as
284
293
# ' a list of named element definitions created with el_def().
285
294
# ' @param complete If `TRUE` (the default), elements are set to inherit from blank elements.
286
295
# ' @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
288
304
# ' coord_annotate <- function(label = "panel annotation") {
289
305
# ' ggproto(NULL, CoordCartesian,
290
306
# ' limits = list(x = NULL, y = NULL),
291
307
# ' expand = TRUE,
292
308
# ' default = FALSE,
293
309
# ' clip = "on",
294
310
# ' render_fg = function(panel_params, theme) {
295
- # ' element_render(theme, "panel.annotation", label = label)
311
+ # ' element_render(theme, "ggxyz. panel.annotation", label = label)
296
312
# ' }
297
313
# ' )
298
314
# ' }
299
315
# '
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
306
317
# ' df <- data.frame(x = 1:3, y = 1:3)
307
318
# ' ggplot(df, aes(x, y)) +
308
319
# ' geom_point() +
309
320
# ' coord_annotate("annotation in blue")
310
321
# '
311
- # ' # revert to original ggplot2 settings
322
+ # ' # Revert to the original ggplot2 settings
312
323
# ' reset_theme_settings()
313
324
# ' @keywords internal
314
325
# ' @export
0 commit comments