Skip to content

Commit 34d0bd5

Browse files
committed
Empty layers need to generate grob per layer.
Also add layer_grob() helper. Fixes #1402
1 parent 7560b48 commit 34d0bd5

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ export(labs)
330330
export(last_plot)
331331
export(layer)
332332
export(layer_data)
333+
export(layer_grob)
333334
export(lims)
334335
export(map_data)
335336
export(margin)

R/layer.r

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,10 @@ Layer <- ggproto("Layer", NULL,
217217
},
218218

219219
draw_geom = function(self, data, panel, coord) {
220-
if (empty(data)) return(list(zeroGrob()))
220+
if (empty(data)) {
221+
n <- nrow(panel$layout)
222+
return(rep(list(zeroGrob()), n))
223+
}
221224

222225
data <- self$geom$handle_na(data, self$geom_params)
223226
self$geom$draw_layer(data, self$geom_params, panel, coord)

R/plot-build.r

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#' to produce an object that can be rendered. This function outputs two pieces:
55
#' a list of data frames (one for each layer), and a panel object, which
66
#' contain all information about axis limits, breaks etc. \code{layer_data}
7-
#' is a helper function which returns the data for a given layer.
7+
#' and \code{layer_grob} are helper functions which returns the data or grob
8+
#' associated with a given layer.
89
#'
910
#' @param plot ggplot object
1011
#' @seealso \code{\link{print.ggplot}} and \code{\link{benchplot}} for
@@ -94,6 +95,14 @@ layer_data <- function(plot, i = 1L) {
9495
ggplot_build(plot)$data[[i]]
9596
}
9697

98+
#' @export
99+
#' @rdname ggplot_build
100+
layer_grob <- function(plot, i = 1L) {
101+
b <- ggplot_build(plot)
102+
103+
b$plot$layers[[i]]$draw_geom(b$data[[i]], b$panel, b$plot$coordinates)
104+
}
105+
97106
#' Build a plot with all the usual bits and pieces.
98107
#'
99108
#' This function builds all grobs necessary for displaying the plot, and

man/ggplot_build.Rd

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

tests/testthat/test-empty-data.r

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,14 @@ test_that("layer inherits data from plot when data = NULL", {
8484
geom_point(data = NULL)
8585
expect_equal(nrow(layer_data(d)), nrow(mtcars))
8686
})
87+
88+
test_that("empty layers still generate one grob per panel", {
89+
df <- data.frame(x = 1:3, y = c("a", "b", "c"))
90+
91+
d <- ggplot(df, aes(x, y)) +
92+
geom_point(data = df[0, ]) +
93+
geom_point() +
94+
facet_wrap(~y)
95+
96+
expect_equal(length(layer_grob(d)), 3)
97+
})

0 commit comments

Comments
 (0)