Skip to content

after_scale fails to find some aesthetic variables #3725

Closed

Description

@atusy

I am highlighting some points by a combination of stage() and after_scale().
I found that after_scale can find aesthetic variables, but not variables in the original data.
It is fine, if that's a design.
As a workaround, I let after_scale find the variable in the data by mapping it to an aesthetic variable.
It works in some case, but not always.
As far as I tried, it seems the error is related to discrete variables.

library(ggplot2)
g <- ggplot(mtcars) +
    aes(wt, mpg, highlight = am == 1) +
    geom_point(
        aes(colour = stage(cyl, after_scale = ifelse(highlight, colour, 'gray'))),
        shape = 21
    )

# Okay
g

# Okay
g + aes(fill = am)

# Error
g + aes(fill = as.factor(am))
#> Error in ifelse(highlight, colour, "gray"): object 'highlight' not found

# Error
ggplot(iris) +
    aes(Sepal.Length, Sepal.Width, highlight = Species == "setosa") +
    geom_point(
        aes(
            colour = stage(
                Species, after_scale = ifelse(highlight, colour, 'gray')
            )
        )
    )
#> Error in ifelse(highlight, colour, "gray"): object 'highlight' not found

Created on 2020-01-13 by the reprex package (v0.3.0.9001)

Activity

  1. yutannihilation commented on Jan 13, 2020

    @yutannihilation
    Member

    The error seems to occur on drawing legends. I don't get what's the expected behaviour here...

    library(ggplot2)
    
    g <- ggplot(mtcars) +
      aes(wt, mpg, highlight = am == 1) +
      geom_point(
        aes(colour = stage(cyl, after_scale = ifelse(highlight, colour, 'gray'))),
        shape = 21
      )
    
    g + aes(fill = as.factor(am)) + theme(legend.position = "none")

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

  2. thomasp85 commented on Jan 13, 2020

    @thomasp85
    Member

    Defining a random aesthetic (highlight) will invariantly lead to undefined behaviour. There is no way to know how the stat and geom will treat it or carry it over. This has always been true and was also the case for the old stat() and ..var.. notations

  3. yutannihilation commented on Jan 13, 2020

    @yutannihilation
    Member

    I agree there's no garantee that such random aesthetics work, but this is a different problem. The problem is that limited variables are used to draw legends. Probably this can happen with more standard aesthetics.

  4. thomasp85 commented on Jan 13, 2020

    @thomasp85
    Member

    I do t think I follow you @yutannihilation — what is the problem with the legend?

  5. yutannihilation commented on Jan 13, 2020

    @yutannihilation
    Member

    I'll try creating some example tomorrow...

  6. thomasp85 commented on Jan 13, 2020

    @thomasp85
    Member

    Ah - I see from your comment now. That is a separate issue all together. Can you open a new one?

    as for this: after_stat() has only access to columns returned by the stat. after_scale() has only access to the final aesthetics. There is no way columns from the original dataset can be carried over as the data might get changed considerably during preparations

  7. yutannihilation commented on Jan 14, 2020

    @yutannihilation
    Member

    Sure. I don't feel it's a completely different issue, but let's discuss on a new one.

  8. yutannihilation commented on Jan 14, 2020

    @yutannihilation
    Member

    @thomasp85 Here it is: #3729

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions