Skip to content

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

Closed
@svigneau

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    messagesrequests for improvements to error, warning, or feedback messages

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions