Skip to content

Legends should not use after_scale()ed values #3729

Closed
@yutannihilation

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions