Skip to content

Legends should not use after_scale()ed values #3729

Closed

Description

@yutannihilation

(Originally commented at #3725 (comment))

Currently, legends reflect the value of after_scale(). But, it's not always possible because the expression of after_scale() might refer to the variable unavailable at the stage of drawing legends.

For example, I think it is a proper usage of stage() to modify scaled colours by combining other aesthetics. But, the following example has two problems:

  1. x is not available on drawing legends
  2. even if we manage to make x available, it's not clear what legends should be drawn because the scaled colours are discrete but the after_scale()ed values are continuous.

Especially because of the problem 2., I think legends should use the raw scaled values, not the after_scale()ed ones.

library(ggplot2)

set.seed(88)
d <- data.frame(x = runif(9), y = runif(9), g = rep(c("a", "b", "c"), each = 3))
p <- ggplot(d) +
  geom_point(aes(x, y, colour = stage(g, after_scale = alpha(colour, x))), size = 10)

# fail
p
#> Error in alpha(colour, x): object 'x' not found

# ok
p + theme(legend.position = "none")

Created on 2020-01-14 by the reprex package (v0.3.0)

Activity

  1. yutannihilation commented on Jan 14, 2020

    @yutannihilation
    MemberAuthor

    Another example. While this doesn't fail, the legends for colour lose their colours. I think they should be coloured.

    I bet this is just a corner case and not a blocking issue, so I don't include this in the v3.3.0 milestones. But, I hope we can reach to some conclusion about whether my claim is valid or not before this feature is released.

    library(ggplot2)
    
    set.seed(88)
    d <- data.frame(x = runif(9), y = runif(9), g = rep(c("a", "b", "c"), each = 3))
    ggplot(d) +
      geom_point(aes(x, y, size = x + y,
                     colour = stage(g, after_scale = ifelse(size > 4, colour, "grey"))))

    Created on 2020-01-14 by the reprex package (v0.3.0)

  2. thomasp85 commented on Jan 15, 2020

    @thomasp85
    Member

    yeah - I think you are right... I didn't think these implications through when I added the legend modification code...

    on the other hand, there are valid reasons for having the legend reflect the transformation as well... Maybe we should add a switch?

  3. yutannihilation commented on Jan 15, 2020

    @yutannihilation
    MemberAuthor

    there are valid reasons for having the legend reflect the transformation as well

    Ah, agreed. While I'm raising this issue, I also feel the example in your post is the case when it's visually more sound when the legends reflect the transformation. I have no idea what interface the switch should be, but probably it's needed here.

    library(ggplot2)
    
    ggplot(mpg) + 
      geom_bar(
        aes(
          x = drv, 
          colour = stage(start = drv, after_scale = alpha(colour, 0.5))
        ), 
        fill = NA, size = 4
      )

    Created on 2020-01-15 by the reprex package (v0.3.0)

  4. added
    feature
    on Jan 21, 2020
  5. added this to the ggplot2 3.3.0 milestone on Jan 21, 2020
  6. yutannihilation commented on Feb 6, 2020

    @yutannihilation
    MemberAuthor

    For reference, while we closed this, this case is not fixed. We decided this is not worth fixing compared to the complexity. Let's revisit here when it's necessary.

    library(ggplot2)
    
    set.seed(88)
    d <- data.frame(x = runif(9), y = runif(9), g = rep(c("a", "b", "c"), each = 3))
    ggplot(d) +
      geom_point(aes(x, y, size = x + y,
                     colour = stage(g, after_scale = ifelse(size > 4, colour, "grey"))))

    Created on 2020-01-14 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureguides 📏

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions