Skip to content

Make the input panels for Facet$draw_panesl predicatable #6407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions R/coord-.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,14 @@ Coord <- ggproto("Coord",
},

draw_panel = function(self, panel, params, theme) {
fg <- self$render_fg(params, theme)
bg <- self$render_bg(params, theme)
fg <- ensure_grob(self$render_fg(params, theme))
bg <- ensure_grob(self$render_bg(params, theme))
if (isTRUE(theme$panel.ontop)) {
panel <- list2(!!!panel, bg, fg)
panel <- gList(panel, bg, fg)
} else {
panel <- list2(bg, !!!panel, fg)
panel <- gList(bg, panel, fg)
}
gTree(
children = inject(gList(!!!panel)),
vp = viewport(clip = self$clip)
)
gTree(children = panel, vp = viewport(clip = self$clip))
}
)

Expand Down
5 changes: 1 addition & 4 deletions R/coord-radial.R
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,7 @@ CoordRadial <- ggproto("CoordRadial", Coord,
# Note that clipping path is applied to panel without coord
# foreground/background (added in parent method).
# These may contain decorations that needn't be clipped
panel <- list(gTree(
children = inject(gList(!!!panel)),
vp = viewport(clip = clip_path)
))
panel <- editGrob(panel, vp = viewport(clip = clip_path))
}
ggproto_parent(Coord, self)$draw_panel(panel, params, theme)
},
Expand Down
6 changes: 5 additions & 1 deletion R/layer.R
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,11 @@ Layer <- ggproto("Layer", NULL,
}

data <- self$geom$handle_na(data, self$computed_geom_params)
self$geom$draw_layer(data, self$computed_geom_params, layout, layout$coord)
grobs <- self$geom$draw_layer(data,
self$computed_geom_params,
layout, layout$coord
)
lapply(grobs, ensure_grob)
}
)

Expand Down
8 changes: 6 additions & 2 deletions R/layout.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ Layout <- ggproto("Layout", NULL,

# Draw individual panels, then assemble into gtable
panels <- lapply(seq_along(panels[[1]]), function(i) {
panel <- lapply(panels, `[[`, i)
panel <- c(facet_bg[i], panel, facet_fg[i])
panel <- gTree(children = inject(gList(!!!lapply(panels, `[[`, i))))
panel <- gTree(children = gList(
ensure_grob(facet_bg[[i]]),
panel,
ensure_grob(facet_fg[[i]])
))
panel <- self$coord$draw_panel(panel, self$panel_params[[i]], theme)
ggname(paste("panel", i, sep = "-"), panel)
})
Expand Down
7 changes: 7 additions & 0 deletions R/utilities-grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
#' @export
grid::arrow

# helper function to ensure the object is a grob
# This will simply return a `zeroGrob()` for non-grob object
ensure_grob <- function(x) {
if (inherits(x, "gList")) x <- gTree(children = x)
if (is.grob(x)) x else zeroGrob()

Check warning on line 11 in R/utilities-grid.R

View check run for this annotation

Codecov / codecov/patch

R/utilities-grid.R#L11

Added line #L11 was not covered by tests
}

# Name ggplot grid object
# Convenience function to name grid objects
#
Expand Down
10 changes: 6 additions & 4 deletions tests/testthat/test-geom-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ test_that("geom_sf() handles alpha properly", {
g <- get_layer_grob(p)[[1]]

# alpha affects the colour of points and lines
expect_equal(g[[1]]$gp$col, alpha(red, 0.5))
expect_equal(g[[2]]$gp$col, alpha(red, 0.5))
# geom_sf() will use `gList()` which is not a grob
# and will be wrapped into a `gTree()`.
expect_equal(g$children[[1]]$gp$col, alpha(red, 0.5))
expect_equal(g$children[[2]]$gp$col, alpha(red, 0.5))
# alpha doesn't affect the colour of polygons, but the fill
expect_equal(g[[3]]$gp$col, alpha(red, 1.0))
expect_equal(g[[3]]$gp$fill, alpha(red, 0.5))
expect_equal(g$children[[3]]$gp$col, alpha(red, 1.0))
expect_equal(g$children[[3]]$gp$fill, alpha(red, 0.5))
})

test_that("errors are correctly triggered", {
Expand Down
Loading