Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* A function can be provided to `labs(alt = <...>)` that takes the plot as input
and returns text as output (@teunbrand, #4795).
* Fixed spurious warning when `weight` aesthetic was used in `stat_smooth()`
(@teunbrand based on @clauswilke's suggestion, #5053).
* The `lwd` alias now correctly replaced by `linewidth` instead of `size`
Expand Down
15 changes: 11 additions & 4 deletions R/labels.r
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ update_labels <- function(p, labels) {
#' @param tag The text for the tag label which will be displayed at the
#' top-left of the plot by default.
#' @param alt,alt_insight Text used for the generation of alt-text for the plot.
#' See [get_alt_text] for examples.
#' See [get_alt_text] for examples. `alt` can also be a function that
#' takes the plot as input and returns text as output. `alt` also accepts
#' rlang [lambda][rlang::as_function()] function notation.
#' @param ... A list of new name-value pairs. The name should be an aesthetic.
#' @export
#' @examples
Expand Down Expand Up @@ -73,7 +75,8 @@ labs <- function(..., title = waiver(), subtitle = waiver(), caption = waiver(),
tag = waiver(), alt = waiver(), alt_insight = waiver()) {
# .ignore_empty = "all" is needed to allow trailing commas, which is NOT a trailing comma for dots_list() as it's in ...
args <- dots_list(..., title = title, subtitle = subtitle, caption = caption,
tag = tag, alt = alt, alt_insight = alt_insight, .ignore_empty = "all")
tag = tag, alt = allow_lambda(alt), alt_insight = alt_insight,
.ignore_empty = "all")

is_waive <- vapply(args, is.waive, logical(1))
args <- args[!is_waive]
Expand Down Expand Up @@ -137,11 +140,15 @@ get_alt_text <- function(p, ...) {
}
#' @export
get_alt_text.ggplot <- function(p, ...) {
p$labels[["alt"]] %||% ""
alt <- p$labels[["alt"]] %||% ""
p$labels[["alt"]] <- NULL
if (is.function(alt)) alt(p) else alt
}
#' @export
get_alt_text.ggplot_built <- function(p, ...) {
p$plot$labels[["alt"]] %||% ""
alt <- p$plot$labels[["alt"]] %||% ""
p$plot$labels[["alt"]] <- NULL
if (is.function(alt)) alt(p$plot) else alt
}
#' @export
get_alt_text.gtable <- function(p, ...) {
Expand Down
4 changes: 3 additions & 1 deletion man/labs.Rd

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

7 changes: 7 additions & 0 deletions tests/testthat/_snaps/labels.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# alt text can take a function

Code
get_alt_text(p)
Output
[1] "A plot showing class on the x-axis and count on the y-axis using a bar layer"

6 changes: 6 additions & 0 deletions tests/testthat/test-labels.r
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ test_that("alt text is returned", {
expect_equal(get_alt_text(p), "An alt text")
})

test_that("alt text can take a function", {
p <- ggplot(mpg, aes(class)) +
geom_bar() +
labs(alt = ~ generate_alt_text(.x))
expect_snapshot(get_alt_text(p))
})

# Visual tests ------------------------------------------------------------

Expand Down