ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Enable 2D structures as aesthetics

Open teunbrand opened this issue 1 year ago • 3 comments

This PR aims to fix #4189 and fix #6090.

Briefly, this PR would allow people to use 2D structures like matrices, data.frames and tibbles as aesthetics. We just had to adopt the {vctrs} way of measure sizes and setting names at select places for this to. Importantly, this does not implement appropriate scales for such aesthetics, but it removes a barrier for extension developers to implement them.

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

df <- mtcars
df$tib <- tibble::tibble(mpg = df$mpg, disp = df$disp)
df$mtx <- cbind(mpg = df$mpg, disp = df$disp)

p <- ggplot(df, aes(cyl, disp, dummy = tib)) +
  geom_point()

layer_data(p)$dummy |> head()
#> Don't know how to automatically pick scale for object of type
#> <tbl_df/tbl/data.frame>. Defaulting to continuous.
#> # A tibble: 6 × 2
#>     mpg  disp
#>   <dbl> <dbl>
#> 1  21     160
#> 2  21     160
#> 3  22.8   108
#> 4  21.4   258
#> 5  18.7   360
#> 6  18.1   225

layer_data(p + aes(dummy = mtx))$dummy |> head()
#>       mpg disp
#> [1,] 21.0  160
#> [2,] 21.0  160
#> [3,] 22.8  108
#> [4,] 21.4  258
#> [5,] 18.7  360
#> [6,] 18.1  225

Created on 2024-09-04 with reprex v2.1.1

teunbrand avatar Sep 04 '24 13:09 teunbrand