Skip to content

Commit 194d8c9

Browse files
lgaboriniteunbrand
andauthored
get_layer_data(), get_layer_grob() can use layer names (#6724)
* get_layer_data(), get_layer_grob() accept layer names * Add layer name tests for get_layer_data(), get_layer_grob() * Man * Fix tests * Fix tests * Revert formatting changes * get_layer_data(), get_layer_grob() now accept integers or characters * remove error snapshots * ensure differences between layers, so test fails when appropriate * add news bullet --------- Co-authored-by: Teun van den Brand <tahvdbrand@gmail.com>
1 parent fd2dff0 commit 194d8c9

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

NEWS.md

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

3+
* `get_layer_data()` and `get_layer_grob()` now accept layer names as index
4+
(@lgaborini, #6724)
5+
36
# ggplot2 4.0.1
47

58
This is a smaller patch release focussed on fixing regressions from 4.0.0 and

R/plot-build.R

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
#' layer. These are useful for tests.
1212
#'
1313
#' @param plot ggplot object
14-
#' @param i An integer. In `get_layer_data()`, the data to return (in the order added to the
14+
#' @param i An integer or a name of a layer. In `get_layer_data()`, the data to return (in the order added to the
1515
#' plot). In `get_layer_grob()`, the grob to return (in the order added to the
16-
#' plot). In `get_panel_scales()`, the row of a facet to return scales for.
16+
#' plot). In `get_panel_scales()` (only integers allowed), the row of a facet to return scales for.
1717
#' @param j An integer. In `get_panel_scales()`, the column of a facet to return
1818
#' scales for.
1919
#' @param ... Not currently in use.
@@ -142,8 +142,15 @@ build_ggplot <- S7::method(ggplot_build, class_ggplot) <- function(plot, ...) {
142142
#' @export
143143
#' @rdname ggplot_build
144144
get_layer_data <- function(plot = get_last_plot(), i = 1L) {
145-
ggplot_build(plot)@data[[i]]
145+
b <- ggplot_build(plot)
146+
idx <- vec_as_location2(
147+
i = i,
148+
n = vec_size(b@plot@layers),
149+
names = names(b@plot@layers)
150+
)
151+
b@data[[idx]]
146152
}
153+
147154
#' @export
148155
#' @rdname ggplot_build
149156
layer_data <- get_layer_data
@@ -171,7 +178,12 @@ layer_scales <- get_panel_scales
171178
get_layer_grob <- function(plot = get_last_plot(), i = 1L) {
172179
b <- ggplot_build(plot)
173180

174-
b@plot@layers[[i]]$draw_geom(b@data[[i]], b@layout)
181+
idx <- vec_as_location2(
182+
i = i,
183+
n = vec_size(b@plot@layers),
184+
names = names(b@plot@layers)
185+
)
186+
b@plot@layers[[idx]]$draw_geom(b@data[[idx]], b@layout)
175187
}
176188

177189
#' @export

man/ggplot_build.Rd

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

tests/testthat/test-layer.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,30 @@ test_that("layer_data returns a data.frame", {
226226
expect_snapshot_error(l$layer_data(mtcars))
227227
})
228228

229+
test_that("get_layer_data works with layer names", {
230+
p <- ggplot() +
231+
annotate("point", x = 1, y = 1, name = "foo") +
232+
annotate("line", x = 1:2, y = 1:2, name = "bar")
233+
234+
# name has higher precedence than index
235+
expect_identical(
236+
get_layer_data(p, i = "bar"),
237+
get_layer_data(p, i = 2L)
238+
)
239+
})
240+
241+
test_that("get_layer_grob works with layer names", {
242+
p <- ggplot() +
243+
annotate("point", x = 1, y = 1, name = "foo") +
244+
annotate("line", x = 1:2, y = 1:2, name = "bar")
245+
246+
# name has higher precedence than index
247+
named <- get_layer_grob(p, i = "bar")
248+
nummed <- get_layer_grob(p, i = 2L)
249+
named[[1]]$name <- nummed[[1]]$name <- NULL # ignore grid's unique names
250+
expect_identical(named, nummed)
251+
})
252+
229253
test_that("data.frames and matrix aesthetics survive the build stage", {
230254
df <- data_frame0(
231255
x = 1:2,

0 commit comments

Comments
 (0)