Skip to content
Open
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
Expand Up @@ -2,6 +2,8 @@

### Bug fixes

* `geom_text()` and `geom_label()` accept expressions as the `label` aesthetic
(@teunbrand, #6638)
* Fixed regression where `draw_key_rect()` stopped using `fill` colours
(@mitchelloharawild, #6609).
* Fixed regression where `scale_{x,y}_*()` threw an error when an expression
Expand Down
1 change: 1 addition & 0 deletions R/geom-label.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ GeomLabel <- ggproto("GeomLabel", Geom,
if (parse) {
lab <- parse_safe(as.character(lab))
}
lab <- validate_labels(lab)

data <- coord$transform(data, panel_params)
data$vjust <- compute_just(data$vjust, data$y, data$x, data$angle)
Expand Down
1 change: 1 addition & 0 deletions R/geom-text.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ GeomText <- ggproto(
if (parse) {
lab <- parse_safe(as.character(lab))
}
lab <- validate_labels(lab)

data <- coord$transform(data, panel_params)

Expand Down
16 changes: 16 additions & 0 deletions R/layer.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ layer <- function(geom = NULL, stat = NULL,
if (check.aes && length(extra_aes) > 0) {
cli::cli_warn("Ignoring unknown aesthetics: {.field {extra_aes}}", call = call_env)
}
aes_params$label <- normalise_label(aes_params$label)

# adjust the legend draw key if requested
geom <- set_draw_key(geom, key_glyph %||% params$key_glyph)
Expand Down Expand Up @@ -552,6 +553,7 @@ Layer <- ggproto("Layer", NULL,

# Evaluate aesthetics
evaled <- eval_aesthetics(aesthetics, data)
evaled$label <- normalise_label(evaled$label)
plot@scales$add_defaults(evaled, plot@plot_env)

# Check for discouraged usage in mapping
Expand Down Expand Up @@ -963,3 +965,17 @@ cleanup_mismatched_data <- function(data, n, fun) {
data[failed] <- NULL
data
}

normalise_label <- function(label) {
if (is.null(label)) {
return(NULL)
}
if (obj_is_list(label)) {
label[lengths(label) == 0] <- ""
labels <- lapply(labels, `[`, i)
}
if (is.expression(label)) {
label <- unclass(as.list(label))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unclass() idea is from #6639.
It counters length 1 expression subclasses, which is admittedly a niche problem.

}
label
}
24 changes: 3 additions & 21 deletions R/scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -1182,18 +1182,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
call = self$call
)
}

if (obj_is_list(labels)) {
# Guard against list with empty elements
labels[lengths(labels) == 0] <- ""
# Make sure each element is scalar
labels <- lapply(labels, `[`, 1)
}
if (is.expression(labels)) {
labels <- as.list(labels)
}

labels
normalise_label(labels)
},

clone = function(self) {
Expand Down Expand Up @@ -1436,11 +1425,7 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
# Need to ensure that if breaks were dropped, corresponding labels are too
labels <- labels[attr(breaks, "pos")]
}

if (is.expression(labels)) {
labels <- as.list(labels)
}
labels
normalise_label(labels)
},

clone = function(self) {
Expand Down Expand Up @@ -1688,10 +1673,7 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
call = self$call
)
}
if (is.expression(labels)) {
labels <- as.list(labels)
}
labels
normalise_label(labels)
},

clone = function(self) {
Expand Down
64 changes: 64 additions & 0 deletions tests/testthat/_snaps/geom-text/geom-text-with-expressions.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions tests/testthat/test-geom-text.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ test_that("geom_text() rejects exotic units", {
)
})

test_that("geom_text() can display expressions", {

df <- data_frame0(x = 1:2, y = 1:2)
df$exp <- expression(alpha + beta^2, gamma * sqrt(delta))

expect_doppelganger(
"geom_text with expressions",
ggplot(df, aes(x, y, label = exp)) +
geom_text()
)
})

# compute_just ------------------------------------------------------------

test_that("vertical and horizontal positions are equivalent", {
Expand Down