Skip to content

Commit f7cf2af

Browse files
authored
Standardise get_*, set_*, update_* and replace_* prefixes. (#5802)
* standardise layer getters * standardise theme accessors * Make `get_last_plot()` * propagate changes throughout * adjust docs * add news bullet * missed a few replacements * propagate to recent PRs
1 parent f89a007 commit f7cf2af

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+456
-375
lines changed

NAMESPACE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,11 @@ export(geom_vline)
421421
export(get_alt_text)
422422
export(get_element_tree)
423423
export(get_guide_data)
424+
export(get_last_plot)
425+
export(get_layer_data)
426+
export(get_layer_grob)
427+
export(get_panel_scales)
428+
export(get_theme)
424429
export(gg_dep)
425430
export(ggpar)
426431
export(ggplot)
@@ -504,6 +509,7 @@ export(rel)
504509
export(remove_missing)
505510
export(render_axes)
506511
export(render_strips)
512+
export(replace_theme)
507513
export(reset_theme_settings)
508514
export(resolution)
509515
export(scale_alpha)
@@ -639,6 +645,7 @@ export(scale_y_sqrt)
639645
export(scale_y_time)
640646
export(sec_axis)
641647
export(set_last_plot)
648+
export(set_theme)
642649
export(sf_transform_xy)
643650
export(should_stop)
644651
export(stage)
@@ -704,6 +711,7 @@ export(unit)
704711
export(update_geom_defaults)
705712
export(update_labels)
706713
export(update_stat_defaults)
714+
export(update_theme)
707715
export(vars)
708716
export(waiver)
709717
export(wrap_dims)

NEWS.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# ggplot2 (development version)
22

3+
* Special getter and setter functions have been renamed for consistency, allowing
4+
for better tab-completion with `get_*`- and `set_*`-prefixes. The old names
5+
remain available for backward compatibility (@teunbrand, #5568).
6+
7+
| New name | Old name |
8+
| -------------------- | ----------------- |
9+
| `get_theme()` | `theme_get()` |
10+
| `set_theme()` | `theme_set()` |
11+
| `replace_theme()` | `theme_replace()` |
12+
| `update_theme()` | `theme_update()` |
13+
| `get_last_plot()` | `last_plot()` |
14+
| `get_layer_data()` | `layer_data()` |
15+
| `get_layer_grob()` | `layer_grob()` |
16+
| `get_panel_scales()` | `layer_scales()` |
17+
318
* Discrete scales now support `minor_breaks`. This may only make sense in
419
discrete position scales, where it affects the placement of minor ticks
520
and minor gridlines (#5434).

R/guides-.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ Guides <- ggproto(
787787
#' # Coord polar doesn't support proper guides, so we get a list
788788
#' polar <- p + coord_polar()
789789
#' get_guide_data(polar, "theta", panel = 2)
790-
get_guide_data <- function(plot = last_plot(), aesthetic, panel = 1L) {
790+
get_guide_data <- function(plot = get_last_plot(), aesthetic, panel = 1L) {
791791

792792
check_string(aesthetic, allow_empty = FALSE)
793793
aesthetic <- standardise_aes_names(aesthetic)

R/plot-build.R

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
#' a list of data frames (one for each layer), and a panel object, which
66
#' contain all information about axis limits, breaks etc.
77
#'
8-
#' `layer_data()`, `layer_grob()`, and `layer_scales()` are helper
8+
#' `get_layer_data()`, `get_layer_grob()`, and `get_panel_scales()` are helper
99
#' functions that return the data, grob, or scales associated with a given
1010
#' layer. These are useful for tests.
1111
#'
1212
#' @param plot ggplot object
13-
#' @param i An integer. In `layer_data()`, the data to return (in the order added to the
14-
#' plot). In `layer_grob()`, the grob to return (in the order added to the
15-
#' plot). In `layer_scales()`, the row of a facet to return scales for.
16-
#' @param j An integer. In `layer_scales()`, the column of a facet to return
13+
#' @param i An integer. In `get_layer_data()`, the data to return (in the order added to the
14+
#' plot). In `get_layer_grob()`, the grob to return (in the order added to the
15+
#' plot). In `get_panel_scales()`, the row of a facet to return scales for.
16+
#' @param j An integer. In `get_panel_scales()`, the column of a facet to return
1717
#' scales for.
1818
#' @seealso
1919
#' [print.ggplot()] and [benchplot()] for
@@ -125,13 +125,16 @@ ggplot_build.ggplot <- function(plot) {
125125

126126
#' @export
127127
#' @rdname ggplot_build
128-
layer_data <- function(plot = last_plot(), i = 1L) {
128+
get_layer_data <- function(plot = get_last_plot(), i = 1L) {
129129
ggplot_build(plot)$data[[i]]
130130
}
131+
#' @export
132+
#' @rdname ggplot_build
133+
layer_data <- get_layer_data
131134

132135
#' @export
133136
#' @rdname ggplot_build
134-
layer_scales <- function(plot = last_plot(), i = 1L, j = 1L) {
137+
get_panel_scales <- function(plot = get_last_plot(), i = 1L, j = 1L) {
135138
b <- ggplot_build(plot)
136139

137140
layout <- b$layout$layout
@@ -145,12 +148,20 @@ layer_scales <- function(plot = last_plot(), i = 1L, j = 1L) {
145148

146149
#' @export
147150
#' @rdname ggplot_build
148-
layer_grob <- function(plot = last_plot(), i = 1L) {
151+
layer_scales <- get_panel_scales
152+
153+
#' @export
154+
#' @rdname ggplot_build
155+
get_layer_grob <- function(plot = get_last_plot(), i = 1L) {
149156
b <- ggplot_build(plot)
150157

151158
b$plot$layers[[i]]$draw_geom(b$data[[i]], b$layout)
152159
}
153160

161+
#' @export
162+
#' @rdname ggplot_build
163+
layer_grob <- get_layer_grob
164+
154165
#' Build a plot with all the usual bits and pieces.
155166
#'
156167
#' This function builds all grobs necessary for displaying the plot, and

R/plot-last.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ set_last_plot <- function(value) .store$set(value)
2121
#' @seealso [ggsave()]
2222
#' @export
2323
#' @keywords internal
24-
last_plot <- function() .store$get()
24+
get_last_plot <- function() .store$get()
25+
26+
#' @export
27+
#' @rdname get_last_plot
28+
last_plot <- get_last_plot

R/save.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
#' dev.off()
9090
#'
9191
#' }
92-
ggsave <- function(filename, plot = last_plot(),
92+
ggsave <- function(filename, plot = get_last_plot(),
9393
device = NULL, path = NULL, scale = 1,
9494
width = NA, height = NA, units = c("in", "cm", "mm", "px"),
9595
dpi = 300, limitsize = TRUE, bg = NULL,

R/theme-current.R

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,39 @@ NULL
55
#' Get, set, and modify the active theme
66
#'
77
#' The current/active theme (see [theme()]) is automatically applied to every
8-
#' plot you draw. Use `theme_get()` to get the current theme, and `theme_set()` to
9-
#' completely override it. `theme_update()` and `theme_replace()` are shorthands for
8+
#' plot you draw. Use `get_theme()` to get the current theme, and `set_theme()` to
9+
#' completely override it. `update_theme()` and `replace_theme()` are shorthands for
1010
#' changing individual elements.
1111
#'
1212
#' @section Adding on to a theme:
1313
#'
1414
#' `+` and `%+replace%` can be used to modify elements in themes.
1515
#'
1616
#' `+` updates the elements of e1 that differ from elements specified (not
17-
#' NULL) in e2. Thus this operator can be used to incrementally add or modify
17+
#' `NULL`) in e2. Thus this operator can be used to incrementally add or modify
1818
#' attributes of a ggplot theme.
1919
#'
2020
#' In contrast, `%+replace%` replaces the entire element; any element of a
2121
#' theme not specified in e2 will not be present in the resulting theme (i.e.
22-
#' NULL). Thus this operator can be used to overwrite an entire theme.
22+
#' `NULL`). Thus this operator can be used to overwrite an entire theme.
2323
#'
24-
#' `theme_update()` uses the `+` operator, so that any unspecified values in the
24+
#' `update_theme()` uses the `+` operator, so that any unspecified values in the
2525
#' theme element will default to the values they are set in the theme.
26-
#' `theme_replace()` uses `%+replace%` to completely replace the element, so any
26+
#' `replace_theme()` uses `%+replace%` to completely replace the element, so any
2727
#' unspecified values will overwrite the current value in the theme with
2828
#' `NULL`.
2929
#'
30-
#' In summary, the main differences between `theme_set()`, `theme_update()`,
31-
#' and `theme_replace()` are:
32-
#' * `theme_set()` completely overrides the current theme.
33-
#' * `theme_update()` modifies a particular element of the current theme
30+
#' In summary, the main differences between `set_theme()`, `update_theme()`,
31+
#' and `replace_theme()` are:
32+
#' * `set_theme()` completely overrides the current theme.
33+
#' * `update_theme()` modifies a particular element of the current theme
3434
#' using the `+` operator.
35-
#' * `theme_replace()` modifies a particular element of the current theme
35+
#' * `replace_theme()` modifies a particular element of the current theme
3636
#' using the `%+replace%` operator.
3737
#'
3838
#' @param ... named list of theme settings
3939
#' @param e1,e2 Theme and element to combine
40-
#' @return `theme_set()`, `theme_update()`, and `theme_replace()`
40+
#' @return `set_theme()`, `update_theme()`, and `replace_theme()`
4141
#' invisibly return the previous theme so you can easily save it, then
4242
#' later restore it.
4343
#' @seealso [+.gg()]
@@ -47,25 +47,25 @@ NULL
4747
#' geom_point()
4848
#' p
4949
#'
50-
#' # Use theme_set() to completely override the current theme.
51-
#' # theme_update() and theme_replace() are similar except they
50+
#' # Use set_theme() to completely override the current theme.
51+
#' # update_theme() and replace_theme() are similar except they
5252
#' # apply directly to the current/active theme.
53-
#' # theme_update() modifies a particular element of the current theme.
53+
#' # update_theme() modifies a particular element of the current theme.
5454
#' # Here we have the old theme so we can later restore it.
5555
#' # Note that the theme is applied when the plot is drawn, not
5656
#' # when it is created.
57-
#' old <- theme_set(theme_bw())
57+
#' old <- set_theme(theme_bw())
5858
#' p
5959
#'
60-
#' theme_set(old)
61-
#' theme_update(panel.grid.minor = element_line(colour = "red"))
60+
#' set_theme(old)
61+
#' update_theme(panel.grid.minor = element_line(colour = "red"))
6262
#' p
6363
#'
64-
#' theme_set(old)
65-
#' theme_replace(panel.grid.minor = element_line(colour = "red"))
64+
#' set_theme(old)
65+
#' replace_theme(panel.grid.minor = element_line(colour = "red"))
6666
#' p
6767
#'
68-
#' theme_set(old)
68+
#' set_theme(old)
6969
#' p
7070
#'
7171
#'
@@ -82,33 +82,49 @@ NULL
8282
#' theme(text = element_text(family = "Times"))
8383
#' rep_el$text
8484
#'
85-
theme_get <- function() {
85+
get_theme <- function() {
8686
ggplot_global$theme_current
8787
}
8888

89-
#' @rdname theme_get
89+
#' @export
90+
#' @rdname get_theme
91+
theme_get <- get_theme
92+
93+
#' @rdname get_theme
9094
#' @param new new theme (a list of theme elements)
9195
#' @export
92-
theme_set <- function(new) {
96+
set_theme <- function(new) {
9397
check_object(new, is.theme, "a {.cls theme} object")
9498
old <- ggplot_global$theme_current
9599
ggplot_global$theme_current <- new
96100
invisible(old)
97101
}
98102

99-
#' @rdname theme_get
100103
#' @export
101-
theme_update <- function(...) {
102-
theme_set(theme_get() + theme(...))
104+
#' @rdname get_theme
105+
theme_set <- set_theme
106+
107+
#' @rdname get_theme
108+
#' @export
109+
update_theme <- function(...) {
110+
set_theme(get_theme() + theme(...))
103111
}
104112

105-
#' @rdname theme_get
106113
#' @export
107-
theme_replace <- function(...) {
108-
theme_set(theme_get() %+replace% theme(...))
114+
#' @rdname get_theme
115+
theme_update <- update_theme
116+
117+
#' @rdname get_theme
118+
#' @export
119+
replace_theme <- function(...) {
120+
set_theme(get_theme() %+replace% theme(...))
109121
}
110122

111-
#' @rdname theme_get
123+
#' @export
124+
#' @rdname get_theme
125+
theme_replace <- replace_theme
126+
127+
#' @rdname get_theme
112128
#' @export
113129
"%+replace%" <- function(e1, e2) {
114130
if (!is.theme(e1) || !is.theme(e2)) {

R/theme.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ validate_theme <- function(theme, tree = get_element_tree(), call = caller_env()
561561
}
562562

563563
# Combine plot defaults with current theme to get complete theme for a plot
564-
plot_theme <- function(x, default = theme_get()) {
564+
plot_theme <- function(x, default = get_theme()) {
565565
theme <- x$theme
566566

567567
# apply theme defaults appropriately if needed

man/get_guide_data.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/last_plot.Rd renamed to man/get_last_plot.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)