Description
I'm developing a ggplot2 extension where it would be helpful to pass a tibble column as an aesthetic.
As a simple motivating example, you could imagine a geom_box()
layer with a bounds
aesthetic that expects a tibble with columns xmin, ymin, xmax and ymax. Then I could simply do geom_box(bounds = bbox)
and the layer will use the nested columns to draw the box.
To do this, we need to make use of nested tibbles (i.e. a tibble column within a tibble).
library(tidyverse)
dat <- tibble(a = tibble(x = 1:5, y = 6:10))
# works fine
ggplot(dat) +
geom_point(aes(x = a$x, y = a$y))
So far, so good. But these aesthetics are still just vectors. Let's try using the tibble column a
.
(Obviously, geom_point()
doesn't expect a tibble column for its x
aesthetic, but it does generate the relevant error.)
ggplot(dat) +
geom_point(aes(x = a, y = a$y))
#> Don't know how to automatically pick scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous.
#> Error: Aesthetics must be either length 1 or the same as the data (5): x
The warning can be removed with a new scale_type
. But the error is generated by this line:
Line 211 in 813d0bd
The length of a data frame is the number of columns (2) instead of the number of rows (5), so we get this error. This is the same problem that vctrs::vec_size()
addresses.
Would it be possible to simply avoid this error by replacing length()
with nrow()
, NROW()
or vec_size()
? Or would there be other repercussions?