Skip to content

Static expressions #6294

Closed
Closed
@teunbrand

Description

@teunbrand

This problem was identified in #6287 and affects 8 packages.

Generally, ggplot2 does not accept expressions as aesthetics.

library(ggplot2)

df <- data.frame(x = 1:2, y = 2:3)

ggplot(df, aes(x, y)) +
  geom_text(aes(label = expression(foo, bar)))
#> Don't know how to automatically pick scale for object of type <expression>.
#> Defaulting to continuous.
#> Error in `geom_text()`:
#> ! Problem while computing aesthetics.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `compute_aesthetics()`:
#> ! Aesthetics are not valid data columns.
#> ✖ The following aesthetics are invalid:
#> ✖ `label = expression(foo, bar)`
#> ℹ Did you mistype the name of a data column or forget to add `after_stat()`?

However, with the current CRAN ggplot2, you can circumvent this limitation by passing the label outside the mapping.

ggplot(df, aes(x, y)) +
  geom_text(label = expression(foo, bar))
#> Warning in is.na(x): is.na() applied to non-(list or vector) of type
#> 'expression'

In the dev version, we also reject expressions outside the mapping.

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

ggplot(df, aes(x, y)) +
  geom_text(label = expression(foo, bar))
#> Error in `geom_text()`:
#> ! Problem while setting up geom aesthetics.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `list_sizes()` at ggplot2/R/geom-.R:273:3:
#> ! `x$label` must be a vector, not an expression vector.

Created on 2025-01-22 with reprex v2.1.1

Our increased strictness becomes a problem for reverse dependencies. I'm unsure wether ggplot2 should accommodate or the dependencies should follow ggplot2's conventions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions