You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a warning in geom_bar when mapping of a continuous variable to the fill aesthetic is not possible? #4394
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:
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.
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.
"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?
@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).
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.
@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).
I made another pull request #4394. Now it gives the following result.
devtools::load_all("~/GitHub/ggplot2/")
#> ℹ Loading ggplot2d<-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?
Activity
yutannihilation commentedon Jul 23, 2022
When the
fillcolumns 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
Created on 2022-07-23 by the reprex package (v2.0.1)
clauswilke commentedon Jul 23, 2022
@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).
yutannihilation commentedon Jul 23, 2022
@clauswilke
Yes.
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
clauswilke commentedon Jul 23, 2022
@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).
yutannihilation commentedon Jul 23, 2022
I see, agreed.
yutannihilation commentedon Jul 24, 2022
I made another pull request #4394. Now it gives the following result.
Created on 2022-07-24 by the reprex package (v2.0.1)