Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable 2D structures as aesthetics #6076

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Sep 4, 2024

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
Copy link
Collaborator Author

I'm unsure why the coverage CI is failing

@teunbrand
Copy link
Collaborator Author

teunbrand commented Sep 10, 2024

TODO: check errors in scales
EDIT: It says: "Discrete values supplied to continuous scale.", which is a little bit off but not terrible.

@teunbrand
Copy link
Collaborator Author

teunbrand commented Sep 20, 2024

Also incorporated #6090 as it is thematically related in the sense of "ggplot2 should give vctrs treatment to input".

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

p <- ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  scale_x_continuous(
    breaks = 2:7,
    labels = data.frame(foo = LETTERS[1:6], bar = 1:6)
  )

get_guide_data(p, aesthetic = "x")
#>           x .value .label.foo .label.bar y
#> 1 0.1127946      2          A          1 0
#> 2 0.2811448      3          B          2 0
#> 3 0.4494949      4          C          3 0
#> 4 0.6178451      5          D          4 0
#> 5 0.7861953      6          E          5 0
#> 6 0.9545455      7          F          6 0

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: accept data.frame as scale labels Feature request: support for tibble aesthetics
1 participant