Skip to content
Merged
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* `get_layer_data()` and `get_layer_grob()` now accept layer names as index
(@lgaborini, #6724)

# ggplot2 4.0.1

This is a smaller patch release focussed on fixing regressions from 4.0.0 and
Expand Down
20 changes: 16 additions & 4 deletions R/plot-build.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#' layer. These are useful for tests.
#'
#' @param plot ggplot object
#' @param i An integer. In `get_layer_data()`, the data to return (in the order added to the
#' @param i An integer or a name of a layer. In `get_layer_data()`, the data to return (in the order added to the
#' plot). In `get_layer_grob()`, the grob to return (in the order added to the
#' plot). In `get_panel_scales()`, the row of a facet to return scales for.
#' plot). In `get_panel_scales()` (only integers allowed), the row of a facet to return scales for.
#' @param j An integer. In `get_panel_scales()`, the column of a facet to return
#' scales for.
#' @param ... Not currently in use.
Expand Down Expand Up @@ -142,8 +142,15 @@ build_ggplot <- S7::method(ggplot_build, class_ggplot) <- function(plot, ...) {
#' @export
#' @rdname ggplot_build
get_layer_data <- function(plot = get_last_plot(), i = 1L) {
ggplot_build(plot)@data[[i]]
b <- ggplot_build(plot)
idx <- vec_as_location2(
i = i,
n = vec_size(b@plot@layers),
names = names(b@plot@layers)
)
b@data[[idx]]
}

#' @export
#' @rdname ggplot_build
layer_data <- get_layer_data
Expand Down Expand Up @@ -171,7 +178,12 @@ layer_scales <- get_panel_scales
get_layer_grob <- function(plot = get_last_plot(), i = 1L) {
b <- ggplot_build(plot)

b@plot@layers[[i]]$draw_geom(b@data[[i]], b@layout)
idx <- vec_as_location2(
i = i,
n = vec_size(b@plot@layers),
names = names(b@plot@layers)
)
b@plot@layers[[idx]]$draw_geom(b@data[[idx]], b@layout)
}

#' @export
Expand Down
4 changes: 2 additions & 2 deletions man/ggplot_build.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions tests/testthat/test-layer.R
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,30 @@ test_that("layer_data returns a data.frame", {
expect_snapshot_error(l$layer_data(mtcars))
})

test_that("get_layer_data works with layer names", {
p <- ggplot() +
annotate("point", x = 1, y = 1, name = "foo") +
annotate("line", x = 1:2, y = 1:2, name = "bar")

# name has higher precedence than index
expect_identical(
get_layer_data(p, i = "bar"),
get_layer_data(p, i = 2L)
)
})

test_that("get_layer_grob works with layer names", {
p <- ggplot() +
annotate("point", x = 1, y = 1, name = "foo") +
annotate("line", x = 1:2, y = 1:2, name = "bar")

# name has higher precedence than index
named <- get_layer_grob(p, i = "bar")
nummed <- get_layer_grob(p, i = 2L)
named[[1]]$name <- nummed[[1]]$name <- NULL # ignore grid's unique names
expect_identical(named, nummed)
})

test_that("data.frames and matrix aesthetics survive the build stage", {
df <- data_frame0(
x = 1:2,
Expand Down