Skip to content

Feature request: accept data.frame as scale labels #6090

Closed

Description

@teunbrand

This is a very niche and selfish problem as I'm trying some stuff out in {gguidance}.
It somewhat relates to #6076 a bit, but I'd like the following code:

library(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")
#> Error in `scale_x_continuous()`:
#> ! `breaks` and `labels` have different lengths.

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

To return the following data.frame (note that the .label column is a nested data.frame rather than two separate .label.foo and .label.bar columns):

#>           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

The justification I have for this is that {vctrs} treats data.frames as vectors where rows are treated as elements. We don't really have a good reason to deviate from this behaviour.

Activity

  1. changed the title [-]Feature request: accept data.frame labels[/-] [+]Feature request: accept data.frame as `labels`[/+] on Sep 7, 2024
  2. changed the title [-]Feature request: accept data.frame as `labels`[/-] [+]Feature request: accept data.frame as scale `labels`[/+] on Sep 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions