Skip to content

Add a warning in geom_bar when mapping of a continuous variable to the fill aesthetic is not possible? #4394

Closed

Description

@svigneau

Consider the following plot:

demo <- diamonds[1:10, ] 
ggplot(data = demo, mapping = aes(x = cut, fill = price)) +
  geom_bar()

Among the 10 diamonds in the "demo" datasets, there is only one diamond with a "Fair" cut and one diamond with an "Ideal" cut. Each of these diamonds has a price and the corresponding bar is colored based on this price using a color scale, in agreement with the aes_colour_fill_alpha documentation. Bars for the other cuts, however, represent several diamonds which all have different prices, making such mapping impossible, and the corresponding bars are consequently plotted in the default gray color.

This behavior tends to be confusing to the user as there is no explanation for why the mapping of prices to the fill aesthetics happens for some bars but not others. This gets even more confusing when no mapping happens at all, as for instance with:

ggplot(data = diamonds, mapping = aes(x = cut, fill = price)) +
  geom_bar()

I think usability could be improved by printing an informative warning message in situations where continuous variables cannot be mapped to the fill aesthetics, and by updating the aes_colour_fill_alpha documentation to indicate the expected behavior in such situations.

Activity

  1. added
    messages
    on Oct 29, 2021
  2. yutannihilation commented on Jul 23, 2022

    @yutannihilation
    Member

    When the fill columns get dropped in all the groups, it's captured by this logic introduced in #4866. But, when there's any undropped one, it fails to capture.

    ggplot2/R/stat-.r

    Lines 135 to 145 in a979ffd

    # The above code will drop columns that are not constant within groups and not
    # carried over/recreated by the stat. This can produce unexpected results,
    # and hence we warn about it.
    dropped <- base::setdiff(names(data), base::union(self$dropped_aes, names(data_new)))
    if (length(dropped) > 0) {
    cli::cli_warn(c(
    "The following aesthetics were dropped during statistical transformation: {.field {glue_collapse(dropped, sep = ', ')}}",
    "i" = "This can happen when ggplot fails to infer the correct grouping structure in the data.",
    "i" = "Did you forget to specify a {.code group} aesthetic or to convert a numerical variable into a factor?"
    ))
    }

    library(ggplot2)
    
    d <- data.frame(
      id = c("a", "a", "b", "b", "c"),
      attr = 1:5
    )
    
    ggplot(d[1:4, ], aes(id, fill = attr)) +
      geom_bar()
    #> Warning: The following aesthetics were dropped during statistical transformation: fill
    #> ℹ This can happen when ggplot fails to infer the correct grouping structure in
    #>   the data.
    #> ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
    #>   variable into a factor?

    ggplot(d, aes(id, fill = attr)) +
      geom_bar()

    Created on 2022-07-23 by the reprex package (v2.0.1)

  3. clauswilke commented on Jul 23, 2022

    @clauswilke
    Member

    @yutannihilation I'm not sure I understand what's happening in your second example. Is the issue that aesthetics gets dropped on a per-group basis, and so it can happen that they get dropped only for some groups? If I understand correctly then I think the question is whether the dropping algorithm makes sense. That's maybe a different issue than the one raised here (which has been addressed, I would argue).

  4. yutannihilation commented on Jul 23, 2022

    @yutannihilation
    Member

    @clauswilke

    Is the issue that aesthetics gets dropped on a per-group basis, and so it can happen that they get dropped only for some groups?

    Yes.

    If I understand correctly then I think the question is whether the dropping algorithm makes sense.

    Ah, are you suggesting the aesthetic should dropped when it gets dropped in any of the groups? If so, I can agree, but it would be a breaking change.

    I think your implementation was perfect, and I was just wondering if this issue can be resolved by adding a few more tweaks on it. Here's my proof-of-concept implementation.

    #4916

  5. clauswilke commented on Jul 23, 2022

    @clauswilke
    Member

    @yutannihilation I don't have a strong opinion one way or another, but dropping aesthetics for some groups and not others seems fundamentally flawed to me (as we can also see in the plot you made above, which doesn't make sense).

  6. yutannihilation commented on Jul 23, 2022

    @yutannihilation
    Member

    I see, agreed.

  7. yutannihilation commented on Jul 24, 2022

    @yutannihilation
    Member

    I made another pull request #4394. Now it gives the following result.

    devtools::load_all("~/GitHub/ggplot2/")
    #> ℹ Loading ggplot2
    
    d <- data.frame(
      id = c("a", "a", "b", "b", "c"),
      attr = 1:5
    )
    
    ggplot(d, aes(id, fill = attr)) +
      geom_bar()
    #> Warning: The following aesthetics were dropped during statistical transformation: fill
    #> ℹ This can happen when ggplot fails to infer the correct grouping structure in
    #>   the data.
    #> ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
    #>   variable into a factor?

    Created on 2022-07-24 by the reprex package (v2.0.1)

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

    messages

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions