Skip to content

Strip label accessor #5924

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

Merged
merged 7 commits into from
Jul 11, 2024
Merged
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ export(get_last_plot)
export(get_layer_data)
export(get_layer_grob)
export(get_panel_scales)
export(get_strip_labels)
export(get_theme)
export(gg_dep)
export(gg_par)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
are not of the correct length (#5901).
* `geom_hline()` and `geom_vline()` now have `position` argument
(@yutannihilation, #4285).
* New function `get_strip_labels()` to retrieve facet labels (@teunbrand, #4979)

# ggplot2 3.5.1

Expand Down
30 changes: 29 additions & 1 deletion R/facet-.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ Facet <- ggproto("Facet", NULL,
},
vars = function() {
character(0)
},
format_strip_labels = function(layout, params) {
return()
}
)

Expand Down Expand Up @@ -321,6 +324,31 @@ vars <- function(...) {
quos(...)
}

#' Accessing a plot's facet strip labels
#'
#' This functions retrieves labels from facet strips with the labeller applied.
#'
#' @param plot A ggplot or build ggplot object.
#'
#' @return `NULL` if there are no labels, otherwise a list of data.frames
#' containing the labels.
#' @export
#' @keywords internal
#'
#' @examples
#' # Basic plot
#' p <- ggplot(mpg, aes(displ, hwy)) +
#' geom_point()
#'
#' get_strip_labels(p) # empty facets
#' get_strip_labels(p + facet_wrap(year ~ cyl))
#' get_strip_labels(p + facet_grid(year ~ cyl))
get_strip_labels <- function(plot = get_last_plot()) {
plot <- ggplot_build(plot)
layout <- plot$layout$layout
params <- plot$layout$facet_params
plot$plot$facet$format_strip_labels(layout, params)
}

#' Is this object a faceting specification?
#'
Expand Down Expand Up @@ -779,7 +807,7 @@ render_axes <- function(x = NULL, y = NULL, coord, theme, transpose = FALSE) {
#'
#' @keywords internal
#' @export
render_strips <- function(x = NULL, y = NULL, labeller, theme) {
render_strips <- function(x = NULL, y = NULL, labeller = identity, theme) {
list(
x = build_strip(x, labeller, theme, TRUE),
y = build_strip(y, labeller, theme, FALSE)
Expand Down
38 changes: 30 additions & 8 deletions R/facet-grid-.R
Original file line number Diff line number Diff line change
Expand Up @@ -380,16 +380,11 @@ FacetGrid <- ggproto("FacetGrid", Facet,
table
},

attach_strips = function(table, layout, params, theme) {
attach_strips = function(self, table, layout, params, theme) {

col_vars <- unique0(layout[names(params$cols)])
row_vars <- unique0(layout[names(params$rows)])
attr(col_vars, "type") <- "cols"
attr(row_vars, "type") <- "rows"
attr(col_vars, "facet") <- "grid"
attr(row_vars, "facet") <- "grid"
strips <- self$format_strip_labels(layout, params)
strips <- render_strips(strips$cols, strips$rows, theme = theme)

strips <- render_strips(col_vars, row_vars, params$labeller, theme)
padding <- convertUnit(calc_element("strip.switch.pad.grid", theme), "cm")

switch_x <- !is.null(params$switch) && params$switch %in% c("both", "x")
Expand Down Expand Up @@ -432,6 +427,33 @@ FacetGrid <- ggproto("FacetGrid", Facet,

vars = function(self) {
names(c(self$params$rows, self$params$cols))
},

format_strip_labels = function(layout, params) {

labeller <- match.fun(params$labeller)

cols <- intersect(names(layout), names(params$cols))
if (length(cols) > 0) {
col_vars <- unique0(layout[cols])
attr(col_vars, "type") <- "cols"
attr(col_vars, "facet") <- "grid"
cols <- data_frame0(!!!labeller(col_vars))
} else {
cols <- NULL
}

rows <- intersect(names(layout), names(params$rows))
if (length(rows) > 0) {
row_vars <- unique0(layout[rows])
attr(row_vars, "type") <- "rows"
attr(row_vars, "facet") <- "grid"
rows <- data_frame0(!!!labeller(row_vars))
} else {
rows <- NULL
}

list(cols = cols, rows = rows)
}
)

Expand Down
33 changes: 19 additions & 14 deletions R/facet-wrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -371,22 +371,11 @@ FacetWrap <- ggproto("FacetWrap", Facet,
weave_axes(table, axes, empty)
},

attach_strips = function(table, layout, params, theme) {
attach_strips = function(self, table, layout, params, theme) {

# Format labels
if (length(params$facets) == 0) {
labels <- data_frame0("(all)" = "(all)", .size = 1)
} else {
labels <- layout[names(params$facets)]
}
attr(labels, "facet") <- "wrap"

# Render individual strips
strips <- render_strips(
x = structure(labels, type = "rows"),
y = structure(labels, type = "cols"),
params$labeller, theme
)
strips <- self$format_strip_labels(layout, params)
strips <- render_strips(strips$facets, strips$facets, theme = theme)

# Set position invariant parameters
padding <- convertUnit(calc_element("strip.switch.pad.wrap", theme), "cm")
Expand Down Expand Up @@ -457,6 +446,22 @@ FacetWrap <- ggproto("FacetWrap", Facet,
},
vars = function(self) {
names(self$params$facets)
},

format_strip_labels = function(layout, params) {
if (length(params$facets) == 0) {
labels <- data_frame0("(all)" = "(all)", .size = 1)
} else {
labels <- layout[intersect(names(params$facets), names(layout))]
}
if (empty(labels)) {
return(NULL)
}
attr(labels, "facet") <- "wrap"
attr(labels, "type") <- switch(params$strip.position, left = , right = "rows", "cols")

labeller <- match.fun(params$labeller)
list(facets = data_frame0(!!!labeller(labels)))
}
)

Expand Down
28 changes: 28 additions & 0 deletions man/get_strip_labels.Rd

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

2 changes: 1 addition & 1 deletion man/render_strips.Rd

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

18 changes: 18 additions & 0 deletions tests/testthat/test-facet-strips.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,21 @@ test_that("strip clipping can be set from the theme", {
expect_equal(strip$x$top[[1]]$layout$clip, "off")
})

test_that("strip labels can be accessed", {

expect_null(get_strip_labels(ggplot()))

expect_equal(
get_strip_labels(ggplot() + facet_wrap(vars("X", "Y"))),
list(facets = data_frame0(`"X"` = "X", `"Y"` = "Y"))
)

expect_equal(
get_strip_labels(ggplot() + facet_grid(vars("X"), vars("Y"))),
list(
cols = data_frame0(`"Y"` = "Y"),
rows = data_frame0(`"X"` = "X")
)
)
})

Loading